【问题标题】:Store string into a variable in next line of a file将字符串存储到文件下一行的变量中
【发布时间】:2020-01-17 19:24:44
【问题描述】:

我刚开始在 C++ 中的 <fstream> 库上刷新自己,我正在尝试将我的文本文件的第一行存储到 3 个整数变量中,所有变量都用空格分隔。文本文件的第二行有一个字符串,我试图让我的字符串变量来存储它。但是,我不确定如何转到文件的下一行。该文件如下所示:

5 10 15
My name is Luke

我知道使用 getline 获取整行然后转到下一行,但我没有将第一行存储到一个变量中,而是 3 所以我不能使用 getline()那个。这是我的代码。

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

int main(int argc, char **argv)
{
    ifstream inFile;
    ofstream outFile;

    string name;
    int x,y,z;

    outFile.open("C:\\Users\\luked\\Desktop\\Test.txt");
    outFile << 5 << " " << 10 << " " << 15 << endl << "My name is Luke";
    outFile.close();

    inFile.open("C:\\Users\\luked\\Desktop\\Test.txt");
    inFile >> x >> y >> z;
    getline(inFile, name);
    cout << x  << " " << y << " " << z << " " << endl << name;

    return 0;
}

【问题讨论】:

    标签: c++ fstream


    【解决方案1】:

    替换

    inFile >> x >> y >> z;
    getline(inFile, name);
    

    由

    inFile >> x >> y >> z;
    inFile.ignore();
    getline(inFile, name);
    

    因为

    Why is cin.ignore() necessary when using "getline" after "cin", but is not required when using "cin" multiple times?

    还有深度解释

    Why does std::getline() skip input after a formatted extraction?

    【讨论】:

      【解决方案2】:

      std::getline 读取直到遇到分隔符或文件结尾,这里是紧接在

      之后的换行符
      5 10 15\n
              ^
      // you need to ignore this
      

      你可以忽略它

      inFile.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
      getline(inFile, name);
      

      【讨论】:

        【解决方案3】:

        你有几个选择:

        您可以使用operator&gt;&gt; 读取这3 个整数,然后使用ignore() 和ifstream 直到跳过新行,然后您可以使用std::getline() 读取第二行:

        #include <iostream>
        #include <string>
        #include <fstream>
        #include <limits>
        using namespace std;
        
        int main(int argc, char **argv)
        {
            ifstream inFile;
            ofstream outFile;
        
            string name;
            int x,y,z;
        
            outFile.open("C:\\Users\\luked\\Desktop\\Test.txt");
            outFile << 5 << " " << 10 << " " << 15 << "\n" << "My name is Luke";
            outFile.close();
        
            inFile.open("C:\\Users\\luked\\Desktop\\Test.txt");
            inFile >> x >> y >> z;
            infile.ignore(numeric_limits<streamsize>::max(), '\n');
            getline(inFile, name);
            cout << x  << " " << y << " " << z << " " << endl << name;
        
            return 0;
        }
        

        否则,尽管您声称“我不能将 getline() 用于 [第一行]”,但您实际上可以使用 std::getline() 来读取第一行。之后只需使用std::istringstream 从该行读取整数。然后你可以使用std::getline()阅读第二行:

        #include <iostream>
        #include <string>
        #include <fstream>
        #include <sstream>
        using namespace std;
        
        int main(int argc, char **argv)
        {
            ifstream inFile;
            ofstream outFile;
        
            string line, name;
            int x,y,z;
        
            outFile.open("C:\\Users\\luked\\Desktop\\Test.txt");
            outFile << 5 << " " << 10 << " " << 15 << "\n" << "My name is Luke";
            outFile.close();
        
            inFile.open("C:\\Users\\luked\\Desktop\\Test.txt");
            getline(inFile, line);
            istringstream(line) >> x >> y >> z;
            getline(inFile, name);
            cout << x  << " " << y << " " << z << " " << endl << name;
        
            return 0;
        }
        

        【讨论】:

        • 啊,好吧,我明白你在第一行使用 getline 的意思。我只是不确定如何将它分别读入每个 var。
        猜你喜欢
        • 1970-01-01
        • 2011-09-07
        • 1970-01-01
        • 2018-07-20
        • 2020-09-20
        • 1970-01-01
        • 1970-01-01
        • 2014-07-27
        • 2015-07-20
        相关资源
        最近更新 更多