getline c ++需要帮助

时间:2014-07-09 20:43:04

标签: c++ input inputstream iostream

我正在尝试将各行读取到不同的变量,而我在阅读第一行之后的任何内容时遇到了麻烦

ifstream inputFileStream;
inputFileStream.open( fileName );
if (inStream.good() && inStream.open())
    {
        string empNum;
        string name;
        string streetAddress;

        getline(inStream, empNum);
        getline(inStream, name);
        getline(inStream, streetAddress);

cout <<empNum << endl;
cout <<name << endl;
cout <<streetAddress << endl;


}

此外,txt文件如下所示:

12  
Bob  
123 Main  
555-555  

1 个答案:

答案 0 :(得分:0)

此代码适用于我。副本&amp;粘贴&amp;跑(没有改变......)

#include <fstream> 
#include <iostream> 
using namespace std;

int main(int argc, char *argv[])
{
    ifstream inputFileStream("text.txt");

    if (inputFileStream.good() )
    {
        string empNum;
        string name;
        string streetAddress;

        getline(inputFileStream, empNum);
        getline(inputFileStream, name);
        getline(inputFileStream, streetAddress);

        cout <<empNum << endl;
        cout <<name << endl;
        cout <<streetAddress << endl;
    }
    return 0;
}