ifstream getline()问题 [英] ifstream getline() problem

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

问题描述

大家好,


我试图从文件中读入缓冲区。通常情况下我会做这个非常低级别的
,但是我已经得出结论我必须停止做所有事情。

所以,我想我会尝试

< fstream>。下面的代码在while()语句中导致无限循环。

调试,我看到缓冲区在第一个位置只有一个0 / b
,没有别的。


ifstream listfile;


listfile.open(" data \\modellist.txt",ios :: in);


如果(listfile.bad())返回0;


char buffer [100];


while(!listfile.getline(buffer,100).eof())

{


//用缓冲区做的东西< br $>

}


任何人都知道为什么?该文件没问题,并且在fread之类的情况下工作正常。


提前致谢!

Hello all,

I am trying to read in lines into a buffer from a file. Normally I would do
this very low-level,
but I have come to the conclusion I must stop doing everything the hard way.
So, I thought I would try
<fstream>. The code below causes an infinite loop in the while() statement.
Debugging, I see that the buffer just has a 0 in
the first position, and nothing else.

ifstream listfile;

listfile.open("data\\modellist.txt", ios::in);

if(listfile.bad()) return 0;

char buffer[100];

while(!listfile.getline(buffer, 100).eof())

{

//Do stuff with buffer

}

Anyone know why? The file is ok, and works fine with fread and the like.

Thanks in advance!

推荐答案

"约翰" <乔** @ ivide.com>在留言新闻中写道:3f0e780e @ shknews01 ...

|我试图从文件中读入缓冲区。通常我会



|这个非常低级,

|但是我得出结论我必须停止做所有的事情。

方式。

|所以,我想我会尝试

| < fstream的取代。下面的代码在while()

语句中导致无限循环。

|调试,我看到缓冲区只有一个0在
|中第一个位置,没有别的。

....

| ifstream listfile;


请注意,安全正确地执行操作的最简单方法是:

std :: string line;

while(std :: getline(listfile,line))

{...}


| char缓冲区[100];

|

| while(!listfile.getline(buffer,100).eof())

如果getline()由于eof之外的任何其他原因而失败,这将是

进入无限循环。此外,最后一个非空但有效的
行可以忽略(IIRC,eof设置如果行是

成功提取到文件末尾而不是<换行符



退出条件的更好测试是:

while(!listfile.getline(buffer) ,100).fail())

或简单(依赖于强制转换运算符):

while(listfile.getline(buffer,100))

或我个人喜欢的风格:

while(!! listfile.getline(buffer,100))


hth,

-

Ivan Vecerina,医学博士。 <> http://www.post1.com/~ivec

Brainbench MVP for C ++<> http://www.brainbench.com
"John" <jo**@ivide.com> wrote in message news:3f0e780e@shknews01...
| I am trying to read in lines into a buffer from a file. Normally I would
do
| this very low-level,
| but I have come to the conclusion I must stop doing everything the hard
way.
| So, I thought I would try
| <fstream>. The code below causes an infinite loop in the while()
statement.
| Debugging, I see that the buffer just has a 0 in
| the first position, and nothing else.
....
| ifstream listfile;

Note that the easiest way to do things safely and correctly is:
std::string line;
while( std::getline( listfile, line ) )
{ ... }

| char buffer[100];
|
| while(!listfile.getline(buffer, 100).eof())

If getline() fails for any other reason than eof, this will
get into an infinite loop. Also, the last non-empty but valid
line could be neglected (IIRC, eof is set if the line was
extracted successfully up to the end of the file instead
of a newline).

A better test for the exit condition would be:
while( !listfile.getline(buffer, 100).fail() )
or simply (relying on a cast operator):
while( listfile.getline(buffer, 100) )
or my personally preferred style:
while( !! listfile.getline(buffer, 100) )

hth,
--
Ivan Vecerina, Dr. med. <> http://www.post1.com/~ivec
Brainbench MVP for C++ <> http://www.brainbench.com

Ivan Vecerina写道:
Ivan Vecerina wrote:
或我个人喜欢的风格:
while(!! listfile.getline(buffer,100))
or my personally preferred style:
while( !! listfile.getline(buffer, 100) )



那个和


之间有什么区别(listfile.getline(缓冲区,100))


?为什么需要双倍'''''?


-

" Codito ergo sum"

Roel Schroeven



What''s the difference between that and

while (listfile.getline(buffer, 100))

? Why the need for the double ''!''?

--
"Codito ergo sum"
Roel Schroeven


John写道:
大家好,

我试图从文件中读入缓冲区。通常情况下我会做这个非常低级的,但是我得出结论我必须停止做所有事情。
所以,我想我会尝试
< ; fstream的取代。下面的代码在while()语句中导致无限循环。
调试,我看到缓冲区在第一个位置只有一个0,没有别的。

ifstream listfile;

listfile.open(" data \\modellist.txt",ios :: in);

if(listfile.bad())返回0;

char buffer [100];

while(!listfile.getline(buffer,100).eof())

{<
//用缓冲区做的东西

}


试试


std :: ifstream listfile(filename,std :: ios_base :: in);


if(stream)

{

std :: string s;

while(std :: getline(listfile,s))

{

//做东西

}

}


您是否从正确的目录中运行?

任何人都知道为什么?文件没问题,并且在fread之类的情况下工作正常。


bad()与good()不相反。

提前致谢!
Hello all,

I am trying to read in lines into a buffer from a file. Normally I would do
this very low-level,
but I have come to the conclusion I must stop doing everything the hard way.
So, I thought I would try
<fstream>. The code below causes an infinite loop in the while() statement.
Debugging, I see that the buffer just has a 0 in
the first position, and nothing else.

ifstream listfile;

listfile.open("data\\modellist.txt", ios::in);

if(listfile.bad()) return 0;

char buffer[100];

while(!listfile.getline(buffer, 100).eof())

{

//Do stuff with buffer

}
Try

std::ifstream listfile (filename, std::ios_base::in);

if (stream)
{
std::string s;
while (std::getline (listfile, s))
{
// do stuff
}
}

Are you running from the right directory?

Anyone know why? The file is ok, and works fine with fread and the like.

bad () is not the opposite of good ().

Thanks in advance!






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

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