C++ C++;:ifstream::getline问题

C++ C++;:ifstream::getline问题,c++,file-io,input,ifstream,istream,C++,File Io,Input,Ifstream,Istream,我正在读这样一个文件: char string[256]; std::ifstream file( "file.txt" ); // open the level file. if ( ! file ) // check if the file loaded fine. { // error } while ( file.getline( string, 256, ' ' ) ) { // handle input } 出于测试目的,我的文件只有一行,末尾有一个空格:

我正在读这样一个文件:

char string[256];

std::ifstream file( "file.txt" ); // open the level file.

if ( ! file ) // check if the file loaded fine.
{
    // error
}

while ( file.getline( string, 256, ' ' )  )
{
    // handle input
}
出于测试目的,我的文件只有一行,末尾有一个空格:

12345 
我的代码首先成功读取12345。但是,它读取的不是循环结束,而是另一个字符串,似乎是返回/换行符

我已将文件保存在
gedit
nano
中。我还用Linux
cat
命令输出了它,最后没有返回。所以这个文件应该没问题

为什么我的代码读取返回/换行符

谢谢。

它正在工作

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

using namespace std;

ifstream file("file.txt");

int main()
{
   string tmp="",st="";

   while (!file.eof())
    {
      file>>tmp;  
      if (tmp != "") st+=tmp;
      tmp="";  
    }
   cout<<st<<endl; 

   return 0;
}
#包括
#包括
#包括
使用名称空间std;
ifstream文件(“file.txt”);
int main()
{
字符串tmp=“”,st=“”;
而(!file.eof())
{
文件>>tmp;
如果(tmp!=“”)st+=tmp;
tmp=“”;
}
请尝试以下方法:

while ( !file.eof()  )
{
    file.getline( string, 256, ' ' );
        // handle input
}

首先,确保您的输入文件良好:

运行以下命令并让我们知道输出:

#include <iostream>
#include <sstream>
#include <string>
#include <iterator>
#include <fstream>>
#include <iomanip>
#include <algorithm>

int main()
{
    std::ifstream file("file.txt");
    std::cout << std::hex;

    std::copy(std::istreambuf_iterator<char>(file),
              std::istreambuf_iterator<char>(),

              std::ostream_iterator<int>(std::cout, " ")); 
}
因此,该文件包含以
0x0A

使用此程序:

#include <iostream>
#include <sstream>
#include <string>
#include <iterator>
#include <fstream>>
#include <iomanip>
#include <algorithm>

int main()
{
    std::ifstream   file("file.txt");

    std::string line;
    while(std::getline(file,line))
    {
        std::cout << "Line(" << line << ")\n";
    }
}

这是旧的,但似乎没有适当的解决办法

我很惊讶,没有人注意到他使用的是空格分隔符。因为整行不会被读取,而只能读取到第一个空格。因此getline在遇到EOF之前仍有更多的数据要读取

因此,下一个getline将读取newline并返回与分隔符相同的结果。如果getline调用如下所示:

char string[256];

std::ifstream file( "file.txt" ); // open the level file.

if ( ! file ) // check if the file loaded fine.
{
    // error
}

while ( file.getline( string, 256, ' ' )  )
{
    // handle input
}
file.getline(字符串,256)


它不会返回换行符,只需一步即可完成。

不会;解决问题。但请使用
getline(std::istream&,std::string&)
在中找到它需要一个流和std::string。你确定该文件没有空行吗。请使用十六进制编辑器将其转储出去。@Martin关于你的第一条评论,我为什么要这样做?至于你的第二条评论,我将尝试检查。但是我用Linux命令
cat
输出了该文件,并且没有任何错误wline。它在字符串后具体读取哪些字符?@tiagovtr当我输出字符串时,它只会导致返回/换行对不起,它仍然是不相关的。OP在这里没有变化。只是它更笨重。我想说这是读取文件的标准反模式。我不确定标准是否指定了内容当getline()失败时的tmp(我可能是错的)。@tiagovtr是的,这是我最初所做的。它不起作用。而且OP中的代码更好:不。eof()在您尝试读取它之前是不正确的。因此,您可能已经读取了整个文件,并且仍然进入循环。在这一点上,您执行getline()操作它失败了。当字符串不正确时,你正在处理输入。@Martin你的评论是针对@tiagovtr的答案的,对吗?@Jay:是的。这个评论是针对这个答案的。@Jay:或者运行
hextump
或者
od
或者
xxd
,如果你的系统上有任何工具,那么就使用它们。这是jus我没有提供,因为我假设您在Windows上无法访问这些工具。我在Linux上…
hextump
:“0000000 3231 3433 2035 000a 0000007”;
od
:“0000000 031061 032063 020065 0000120000007”;
xxd
:“0000000:3132 3334 3520 0a 12345”我对OP的评论是GUI程序给我的。@martin我转储了由新代码生成的文件的输出,结果是一样的:
313233333520a
。看来我的文件不好,对吗?@Jay:如果文件的输出相同,那么输入就好。这个问题的第一条评论建议同样的解决方案也许这个答案可以为新读者提供建设性的表述,这样他们就可以轻松地应用这个解决方案。这个问题有一个公认的答案,所以如果得不到投票,这将不是人们会读到的第一个答案。
#include <iostream>
#include <sstream>
#include <string>
#include <iterator>
#include <fstream>>
#include <iomanip>
#include <algorithm>

int main()
{
    std::ifstream   file("file.txt");

    std::string line;
    while(std::getline(file,line))
    {
        std::cout << "Line(" << line << ")\n";
    }
}
> g++ t.cpp
> ./a.out
Line(12345 )