Getline issue with input of open file stream

早过忘川 提交于 2019-12-11 08:28:01

问题


I'm trying to figure out why this is broke now, because I had it working and I'm not sure what is wrong. I'm trying a simple getline from a file that's been opened, however, the compiler keeps giving me errors. I've tried finding someone else with these issues, but I haven't been able to find anyone else with this. Any advice?

void Foo::bar(ifstream &inputFile)
{
// Read in the data, parse it out, and 
// call loadQueue
string input;
do {    
    getline(inputFile, input);
    loadQueue(input);
}while (!(inputFile.eof()));

}

Here is what I get in return:

g++    -c -o Airworthy.o Airworthy.cpp
Foo.cpp: In member function ‘void Airworthy::readData(std::ifstream&)’:
Foo.cpp:25:27: error: no matching function for call to ‘getline(std::ifstream&, std::string&)’
Foo.cpp:25:27: note: candidates are:
In file included from /usr/lib/gcc/x86_64-unknown-linux-gnu/4.7.2/../../../../include/c++/4.7.2/string:55:0,
             from /usr/lib/gcc/x86_64-unknown-linux-gnu/4.7.2/../../../../include/c++/4.7.2/bits/locale_classes.h:42,
             from /usr/lib/gcc/x86_64-unknown-linux-gnu/4.7.2/../../../../include/c++/4.7.2/bits/ios_base.h:43,
             from /usr/lib/gcc/x86_64-unknown-linux-gnu/4.7.2/../../../../include/c++/4.7.2/ios:43,
             from /usr/lib/gcc/x86_64-unknown-linux-gnu/4.7.2/../../../../include/c++/4.7.2/ostream:40,
             from /usr/lib/gcc/x86_64-unknown-linux-gnu/4.7.2/../../../../include/c++/4.7.2/iostream:40,

Any ideas on what the issue is?


回答1:


Most likely you forgot to #include all the necessary standard headers. One possibility is:

#include <fstream>

Or perhaps you forgot to:

#include <string> 

You always have to #include all pertinent standard headers explicitly, without relying on indirect inclusion through some other headers.




回答2:


As Andy says, you need the appropriate includes. There are, however, at least two other major problems with your code (one of which will affect which includes you need):

  • You should never (or almost never) pass ifstream as an argument to a function. Unless the function is going to do an open or a close, you should pass it std::istream&, so that it can be called with any istream, and not just ifstream.

    Once you've changed this, you need to include <istream>, and not <fstream>. (<fstream> includes <istream>. And a lot more which you don't need.)

  • You should never loop on ! inputFile.eof(). It doesn't work. In your case, the loop should be

    while ( std::getline( inputFile, input ) ) {4
        //  ...
    }
    

    It works, and almost nothing else does.

    In general, a do...while loop is almost always wrong when doing input; it results in your processing the input even when it fails (which you do—any use of input after the getline but before testing whether the getline has succeeded is an error). And the results of inputFile.eof() are not really well defined until input has failed. Using istream::eof() to control a loop is almost always an error.



来源:https://stackoverflow.com/questions/16264205/getline-issue-with-input-of-open-file-stream

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!