在 C++ 中读取到文件末尾
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13544245/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
reading until the end of file in C++
提问by melpomene
I'm trying to read till the end of a file for a phonebook app that im converting from C to C++. When I print the the results from the file i get this:
我正在尝试阅读直到我从 C 转换为 C++ 的电话簿应用程序的文件末尾。当我从文件打印结果时,我得到了这个:
johnny smith
(Home)3
(Cell)4
x?> x?>
(Home)4
(Cell)4
it should print:
它应该打印:
johnny smith
(Home)3
(Cell)4
Right now I'm using while(!infile.eof())
which i've read is a poor practice, but when I use infile.getline()
I get a repeat of the first and last name, and the format is all Hymaned up. Is there anyway(or another way) to get rid of the junk at the end of the input or another way to read till the end of file in C++ that fixes this. I've been reading about different solutions, but the one a lot of sites seem to agree on is fgets
, which is what I had with the original C version, but obviously fgets
doesn't work with ifstream
which is what I'm using. here is the code:
现在我正在使用while(!infile.eof())
我读过的一种不好的做法,但是当我使用时,infile.getline()
我得到了重复的名字和姓氏,并且格式都被抬高了。无论如何(或另一种方式)是否可以摆脱输入末尾的垃圾或另一种在 C++ 中读取到文件末尾的方法来解决这个问题。我一直在阅读不同的解决方案,但很多网站似乎都同意的是fgets
,这是我在原始 C 版本中所拥有的,但显然fgets
不适ifstream
用于我正在使用的。这是代码:
void contacts:: readfile(contacts*friends ,int* counter, int i,char buffer[],char user_entry3[])
{
ifstream read;
read.open(user_entry3,ios::in);
int len;
contacts temp;
*counter=0;
i=0;
while (!read.eof()) {
temp.First_Name=(char*)malloc(36);
temp.Last_Name=(char*)malloc(36);
read>>temp.First_Name>>temp.Last_Name;
read>>buffer;
len=strlen(buffer);
if(buffer[len-1]=='\n')
buffer[len-1]='string a, b, c;
while (true) {
if (!(in >> a)) break;
if (!(in >> b)) break;
if (!(in >> c)) break;
do_stuff_with(a, b, c);
}
';
temp.home=(char*)malloc(20);
strcpy(temp.home, buffer);
read>>buffer;
len=strlen(buffer);
if(buffer[len-1]=='\n')
buffer[len-1]='##代码##';
temp.cell=(char*)malloc(20);
strcpy(temp.cell, buffer);
friends[i].First_Name=(char*)malloc(MAXNAME);
friends[i].Last_Name=(char*)malloc(MAXNAME);
friends[i].home=(char*)malloc(MAXPHONE);
friends[i].cell=(char*)malloc(MAXPHONE);
//adds file content to the structure
strcpy(friends[*counter].First_Name,temp.First_Name);
strcpy(friends[*counter].Last_Name,temp.Last_Name);
strcpy(friends[*counter].home,temp.home);
strcpy(friends[*counter].cell,temp.cell);
(*counter)++;
i++;
}
//closes file and frees memory
read.close();
free(temp.Last_Name);
free(temp.First_Name);
free(temp.home);
free(temp.cell);
}
回答by Dietmar Kühl
Do notuse eof()
to determine if you reached end of file. Instead, read what you want to read and thencheck if you successfully read the data. Obce reading failed you may use eof()
to determine if the error is down to having reached the end of the file before producing an error report about a format error.
千万不能使用eof()
,以确定是否到达文件末尾。相反,阅读您想要阅读的内容,然后检查您是否成功读取了数据。Obce reading failed 您可以用来eof()
在生成有关格式错误的错误报告之前确定错误是否已到达文件末尾。
Since you mentioned that you read that using !infile.eof()
is good practice: Can you point us at the source of this wrong information? This information need correction.
既然你提到你读到使用!infile.eof()
是一种很好的做法:你能指出我们这个错误信息的来源吗?此信息需要更正。
回答by melpomene
Don't use
!eof()
. It checks whether the lastread failure was due to reaching the end of the file. It does not predict the future.Don't use
malloc
in C++. If you do, check the return value for errors!Don't use
operator>>
forchar *
. There's no size check so that's just asking for buffer overflows.The
'\n'
check on buffer is useless.operator>>
for strings stops at whitespace.You're blindly
strcpy
ing a string of unknown length intotemp.home
of size 20. That's another buffer overflow.... I kind of stopped reading there. If you want to read stuff from a file but stop on eof/error, you can do something like this:
不要使用
!eof()
. 它检查上次读取失败是否是由于到达文件末尾。它不预测未来。不要
malloc
在 C++ 中使用。如果这样做,请检查返回值是否有错误!不要
operator>>
用于char *
. 没有大小检查,所以只是要求缓冲区溢出。在
'\n'
上缓冲检查是无用的。operator>>
对于字符串在空格处停止。您盲目地
strcpy
将未知长度的字符串转换temp.home
为大小为 20的字符串。这是另一个缓冲区溢出。......我有点停止在那里阅读。如果您想从文件中读取内容但在 eof/error 时停止,您可以执行以下操作:
.
.
##代码##