ifstream的/函数getline [英] ifstream/getline
问题描述
愚蠢的米今天盯上了......什么,如果有的话,是错的
,代码如下?
std :: ifstream f(" myfile.txt");
if(!f){
cerr<< 无法打开文件\ n;
}
而(getline(f,s)){
cout< ;< s<< ''\\ n'$;
}
-
Christopher Benson-Manica |我*应该*知道我在说什么 - 如果我
ataru(at)cyberspace.org |不,我需要知道。火焰欢迎。
The dumb-o-meter''s pegging out today... What, if anything, is wrong
with the following code?
std::ifstream f( "myfile.txt" );
if( !f ) {
cerr << "Couldn''t open file\n";
}
while( getline(f,s) ) {
cout << s << ''\n'';
}
--
Christopher Benson-Manica | I *should* know what I''m talking about - if I
ataru(at)cyberspace.org | don''t, I need to know. Flames welcome.
推荐答案
2004年4月1日星期四14:57:37 +0000(UTC),Christopher Benson-Manica
< at *** @ nospam.cyberspace.org>写道:
On Thu, 1 Apr 2004 14:57:37 +0000 (UTC), Christopher Benson-Manica
<at***@nospam.cyberspace.org> wrote:
愚蠢的米今天盯着看......如果有的话,错误的是什么?
使用以下代码?
std :: ifstream f(" myfile.txt");
if(!f){
cerr<< 无法打开文件\ n;
}
而(getline(f,s)){
cout<< s<< ''\\ n';;
}
The dumb-o-meter''s pegging out today... What, if anything, is wrong
with the following code?
std::ifstream f( "myfile.txt" );
if( !f ) {
cerr << "Couldn''t open file\n";
}
while( getline(f,s) ) {
cout << s << ''\n'';
}
这是面试问题吗? ;-)
为什么你不告诉我们为什么你认为它有什么问题。我b $ b可以列举它没有编译的所有原因,但那只是
a浪费我们两个时间......
-leor
-
Leor Zolman --- BD软件--- www.bdsoft.com
C / C ++,Java,Perl和Unix的现场培训
C ++用户:下载BD Software的免费STL错误消息解密器:
www。 bdsoft.com/tools/stlfilt.html
Christopher Benson-Manica写道:
Christopher Benson-Manica wrote:
愚蠢的-meter'今天盯住了......如果有的话,用以下代码做错了什么?
#include< iostream>
#include< fstream>
#include< string>
使用命名空间std;
int main()
{
std :: string s;
std :: ifstream f(" myfile.txt");
if(!f){
cerr<< 无法打开文件\ n;
}
而(getline(f,s)){
cout<< s<< ''\\ n';;
}
The dumb-o-meter''s pegging out today... What, if anything, is wrong
with the following code?
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main()
{
std::string s;
std::ifstream f( "myfile.txt" );
if( !f ) {
cerr << "Couldn''t open file\n";
}
while( getline(f,s) ) {
cout << s << ''\n'';
}
返回0;
}
现在它应该编译。
return 0;
}
Now it should compile.
Leor Zolman< le ** @ bdsoft.com>这样说:
Leor Zolman <le**@bdsoft.com> spoke thus:
(我写的)
std :: ifstream f(" myfile.txt");
if(!f) {
cerr<< 无法打开文件\ n;
}
而(getline(f,s)){
cout<< s<< ''\\ n';;
}
std::ifstream f( "myfile.txt" );
if( !f ) {
cerr << "Couldn''t open file\n";
}
while( getline(f,s) ) {
cout << s << ''\n'';
}
这是面试问题吗? ;-)
不,只是一个今天我很蠢!问题,正如我所指出的那样。
为什么你不告诉我们为什么你认为它有问题。
因为什么都没有打印出来?我知道文件存在,我知道我可以打开它。
可以打开它。我不明白我做错了什么(显然*叹气*)。我创建了一个ifstream,看看它是否打开然后从中读取。
getline(f,s)返回f,这是真的,除非文件结尾已经达到了
,对吗?
我可以列举它没有编译的所有原因,
如果它不应该编译,我有问题,因为它b / b
编译干净。
但这只会浪费我们两个时间......
Is this an interview question? ;-)
Nah, just an "I''m dumb today!" question, as I indicated.
Why don''t you tell us why you think there''s something wrong with it.
Because nothing gets printed? I know the file exists, and I know I
can open it. I don''t see what I''m doing wrong (obviously *sigh*). I
create an ifstream, see whether it''s open and then read from it.
getline(f,s) returns f, and that''s true unless end of file has been
reached, right?
I
could enumerate all the reasons it doesn''t compile,
If it isn''t supposed to compile, I''ve got problems, because it
compiles cleanly.
but that would just be a waste of both of our time...
我很惊讶你还在回答我的帖子,坦率地说;)
-
Christopher Benson-Manica |我*应该*知道我在说什么 - 如果我
ataru(at)cyberspace.org |不,我需要知道。火焰欢迎。
I''m amazed you''re still answering my posts, quite frankly ;)
--
Christopher Benson-Manica | I *should* know what I''m talking about - if I
ataru(at)cyberspace.org | don''t, I need to know. Flames welcome.
这篇关于ifstream的/函数getline的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!