getline和EOF问题 [英] getline and EOF question

查看:119
本文介绍了getline和EOF问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将整个文件作为单个字符串读取,使用getline()

函数,如下例所示。我不知道我做错了什么。

尝试了g ++ 3.2,3.4和4.0。谢谢!

#include< iostream>

#include< fstream>

#include< cstdlib>

#include< string>


使用命名空间std;


int main(int argc,char * argv [])

{

const int MAX_SIZE = 100000;

string s;

char chars [MAX_SIZE];


fstream IN(垃圾,ios :: in);


//这2个还可以。

IN .getline(chars,MAX_SIZE,EOF); //有效,所以EOF确定

getline(IN,s,''x''); //使用字符串,没有EOF


// g ++不喜欢这个。

getline(IN,s,EOF); //什么是错的?


返回0;

}

解决方案

Amadeus WM写道:

我正在尝试使用getline()
函数将整个文件作为单个字符串读取,如下例所示。我不知道我做错了什么。
尝试了g ++ 3.2,3.4和4.0。谢谢!

#include< iostream>
#include< fstream>
#include< cstdlib>
#include< string>

使用命名空间std;
int main(int argc,char * argv [])
{
const int MAX_SIZE = 100000;
string s;
char chars [MAX_SIZE];

fstream IN(" junk",ios :: in);

//这两个都可以。
IN.getline(chars,MAX_SIZE,EOF); //有效,所以EOF ok
getline(IN,s,''x''); //使用字符串,没有EOF

// g ++不喜欢这个。
getline(IN,s,EOF); //什么是错的?

返回0;
}




不幸的是,有一个std :: getline ()声明如下:


template< class charT,class traits,class Allocator>

basic_istream< charT,traits>& getline(basic_istream< charT,traits>& is,

basic_string< charT,traits,Allocator>& str,charT delim);


它想要charT与EOF的类型不同。尝试在IN上调用

getline。我自己没有尝试过,所以我不知道它是否会给b
帮助。

-

如果我们的假设是关于什么,而不是一些或多个特定的东西,然后我们的推论构成数学。因此,数学可能被定义为我们永远不知道我们所讨论的是什么,以及我们所说的是否属实的主题.- Bertrand Russell


On Fri,2005年7月1日05:33:08 -0400,Steven T. Hatton写道:

Amadeus WM写道:

我正在尝试使用getline()
函数将整个文件作为单个字符串读取,如下例所示。我不知道我做错了什么。
尝试了g ++ 3.2,3.4和4.0。谢谢!

#include< iostream>
#include< fstream>
#include< cstdlib>
#include< string>

使用命名空间std;
int main(int argc,char * argv [])
{
const int MAX_SIZE = 100000;
string s;
char chars [MAX_SIZE];

fstream IN(" junk",ios :: in);

//这两个都可以。
IN.getline(chars,MAX_SIZE,EOF); //有效,所以EOF ok
getline(IN,s,''x''); //使用字符串,没有EOF

// g ++不喜欢这个。
getline(IN,s,EOF); //什么是错的?

返回0;
}



不幸的是,有一个std :: getline()声明为如下:

模板<类charT,类特征,类Allocator>
basic_istream< charT,traits>& getline(basic_istream< charT,traits>& is,
basic_string< charT,traits,Allocator>& str,charT delim);

它想要一个不相同的charT输入EOF。尝试在IN上调用
getline。我自己没试过,所以我不知道它是否会帮助。




我做了,在我发布的程序中,有效。我很好奇为什么

一个字符串不起作用。


还有一个bit / basic_string.h中的getline()/ br / >

模板< typename _CharT,typename _Traits,typename _Alloc>

basic_istream< _CharT,_Traits>&

getline(basic_istream< _CharT, _Traits>& __is,

basic_string< _CharT,_Traits,_Alloc>& __str,_CharT __delim);


所以在这个版本中,类型分隔符必须与basic_istream和basic_string中的_CharT

相同。对于istream

版本的getline也是如此。所以我不明白为什么一个人有效,另一个人不知道b $ b。 EOF的类型是什么?无论什么类型,它都应该工作,

或两者都不起作用。




Amadeus WM ; <是******* @ cablespeed.com>在消息中写道

news:pa **************************** @ cablespeed.com ... < blockquote class =post_quotes>我正在尝试使用getline()
函数将整个文件作为单个字符串读取,如下例所示。我不知道我做错了什么。
尝试了g ++ 3.2,3.4和4.0。谢谢!

#include< iostream>
#include< fstream>
#include< cstdlib>
#include< string>

使用命名空间std;
int main(int argc,char * argv [])
{
const int MAX_SIZE = 100000;
string s;
char chars [MAX_SIZE];

fstream IN(" junk",ios :: in);

//这两个都可以。
IN.getline(chars,MAX_SIZE,EOF); //有效,所以EOF ok
getline(IN,s,''x''); //使用字符串,没有EOF

// g ++不喜欢这个。
getline(IN,s,EOF); //什么是错的?

返回0;
}




这是一个小方法的例子阅读整个文件。

回答你问题的关键是检查流的状态。一旦你有了
读取文件的最后一行,就是下一次调用getline(),这将是
设置ifl的eof标志。


#include< fstream>

#include< iostream>

#include< string>

使用namespace std;

int main(int argc,char * argv [])

{

ifstream ifl(" junk");

string s,temp;

while(1)

{

getline(ifl,temp);

如果(ifl.eof())中断;

s + = temp +" \\\
" ;; //如果你想保持换行符号

}

cout<< " //从下一行开始\ n";

cout<< s;

cout<< " //在最后一行结束了\ n" ;;

返回0;

}


另外,如果你只想阅读文件的全部内容,

或许可以考虑在二进制文件流上使用read()。您可以在文件末尾的

处打开它,以推断文件的大小(以字节为单位)。然后回到

到开头并调用read()。


谢谢,

Kyle

I''m trying to read a whole file as a single string, using the getline()
function, as in the example below. I can''t tell what I''m doing wrong.
Tried g++ 3.2, 3.4 and 4.0. Thanks!
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <string>

using namespace std;

int main(int argc, char * argv[])
{
const int MAX_SIZE=100000;
string s;
char chars[MAX_SIZE];

fstream IN("junk", ios::in);

// These 2 are ok.
IN.getline(chars, MAX_SIZE, EOF); // works, so EOF ok
getline(IN, s, ''x''); // works with string, no EOF

// g++ doesn''t like this one.
getline(IN, s, EOF); // what''s wrong??

return 0;
}

解决方案

Amadeus W. M. wrote:

I''m trying to read a whole file as a single string, using the getline()
function, as in the example below. I can''t tell what I''m doing wrong.
Tried g++ 3.2, 3.4 and 4.0. Thanks!
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <string>

using namespace std;

int main(int argc, char * argv[])
{
const int MAX_SIZE=100000;
string s;
char chars[MAX_SIZE];

fstream IN("junk", ios::in);

// These 2 are ok.
IN.getline(chars, MAX_SIZE, EOF); // works, so EOF ok
getline(IN, s, ''x''); // works with string, no EOF

// g++ doesn''t like this one.
getline(IN, s, EOF); // what''s wrong??

return 0;
}



Unfortunately, there is a std::getline() declared as follows:

template<class charT, class traits, class Allocator>
basic_istream<charT,traits>& getline(basic_istream<charT,traits>& is,
basic_string<charT,traits,Allocator>& str, charT delim);

It wants a charT which is not of the same type as EOF. Try calling the
getline on IN. I have not tried that myself, so I don''t know if it will
help.
--
If our hypothesis is about anything and not about some one or more
particular things, then our deductions constitute mathematics. Thus
mathematics may be defined as the subject in which we never know what we
are talking about, nor whether what we are saying is true.-Bertrand Russell


On Fri, 01 Jul 2005 05:33:08 -0400, Steven T. Hatton wrote:

Amadeus W. M. wrote:

I''m trying to read a whole file as a single string, using the getline()
function, as in the example below. I can''t tell what I''m doing wrong.
Tried g++ 3.2, 3.4 and 4.0. Thanks!
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <string>

using namespace std;

int main(int argc, char * argv[])
{
const int MAX_SIZE=100000;
string s;
char chars[MAX_SIZE];

fstream IN("junk", ios::in);

// These 2 are ok.
IN.getline(chars, MAX_SIZE, EOF); // works, so EOF ok
getline(IN, s, ''x''); // works with string, no EOF

// g++ doesn''t like this one.
getline(IN, s, EOF); // what''s wrong??

return 0;
}



Unfortunately, there is a std::getline() declared as follows:

template<class charT, class traits, class Allocator>
basic_istream<charT,traits>& getline(basic_istream<charT,traits>& is,
basic_string<charT,traits,Allocator>& str, charT delim);

It wants a charT which is not of the same type as EOF. Try calling the
getline on IN. I have not tried that myself, so I don''t know if it will
help.



I did, in the very program I posted, and it worked. I was curious why the
one on string wouldn''t work.

There is also a getline() in bits/basic_string.h

template<typename _CharT, typename _Traits, typename _Alloc>
basic_istream<_CharT,_Traits>&
getline(basic_istream<_CharT, _Traits>& __is,
basic_string<_CharT, _Traits, _Alloc>& __str, _CharT __delim);

So in this version, the type of the delimiter must be the same _CharT
as in basic_istream and basic_string. The same is true for the istream
version of getline. So I don''t understand why one works and the other
doesn''t. What''s the type of EOF? Whatever the type, it should both work,
or both not work.



"Amadeus W. M." <am*******@cablespeed.com> wrote in message
news:pa****************************@cablespeed.com ...

I''m trying to read a whole file as a single string, using the getline()
function, as in the example below. I can''t tell what I''m doing wrong.
Tried g++ 3.2, 3.4 and 4.0. Thanks!
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <string>

using namespace std;

int main(int argc, char * argv[])
{
const int MAX_SIZE=100000;
string s;
char chars[MAX_SIZE];

fstream IN("junk", ios::in);

// These 2 are ok.
IN.getline(chars, MAX_SIZE, EOF); // works, so EOF ok
getline(IN, s, ''x''); // works with string, no EOF

// g++ doesn''t like this one.
getline(IN, s, EOF); // what''s wrong??

return 0;
}



Here is a small example of one way to read the entire file. The key to
answering your question is checking the state of the stream. Once you have
read the last line of the file, it is the next call to getline() that will
set the eof flag of ifl.

#include <fstream>
#include <iostream>
#include <string>
using namespace std;
int main(int argc, char* argv[])
{
ifstream ifl("junk");
string s, temp;
while ( 1 )
{
getline(ifl, temp);
if (ifl.eof()) break;
s += temp + "\n"; // if you want to maintain line breaks
}
cout << "// starts on next line\n";
cout << s;
cout << "// ended on last line\n";
return 0;
}

On an aside, if you just want to read the entire contents of the file,
perhaps consider using read() on a binary file stream. You can open it at
the end of the file to deduce the size in bytes of the file. Then seek back
to the beginning and call read().

Thanks,
Kyle


这篇关于getline和EOF问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆