【问题标题】:reading from a textfile line by line | C++逐行读取文本文件| C++
【发布时间】:2020-09-11 00:33:04
【问题描述】:

我现在正在互联网上搜索一段时间,但找不到适合我的解决方案。或者我只是愚蠢。 我在一个 .txt 文件中有 12 行,我想逐行读取这些行,一个接一个。从 1,2...12 开始。 我的 .txt 文件如下所示:

ITEM1     (string)
ITEM2     (string)
ITEM3     (string)
ITEM4     (string)
ITEM5     (string)
ITEM6     (string)
ITEM7     (string)
ITEM8     (string)
ITEM9     (string)
ITEM10    (string)
1         (INT)
NATHAN    (string)

括号中的内容没有写入文件。它只是从程序中保存的方式。我想读取文件并将文本存储在变量中。 ITEM1-ITEM10 来自一个名为 inventory[10] 的数组。 数字“1”只是一个标记并进入变量“int gameposition”。名称“NATHAN”进入字符串“字符串字符名”。

文件路径是:

C:\apoadventure\savegame.txt

我知道这种保存游戏信息的方法不是很聪明,但我对此很陌生,我只是在尝试和学习。

【问题讨论】:

    标签: c++ file path filesystems


    【解决方案1】:

    我的 InputFile.txt

    Apple 1
    Mango 2
    Banana 3
    Grape 4
    

    我的主体

    #include <iostream>
    #include <fstream>
    #include <string>
    
    int main()
    {
        std::ifstream file;
        file.open("InputFile.txt");
        std::string str[4];
        for (int i = 0; std::getline(file, str[i]);i++) 
        {
            std::cout << str[i] << "\n";
        }
        file.close();
    }
    

    现在在上面的代码中,我使用 getline() 函数逐行输入 .txt 文件中的数据。

    这是我的输出

    Apple 1
    Mango 2
    Banana 3
    Grape 4
    

    希望这可以帮助您了解如何逐行从文件中输入数据:-)

    【讨论】:

      【解决方案2】:

      你有一个基于行的格式,所以用getline阅读它。任何不是字符串的东西都可以从刚刚读取的字符串转换(例如下面代码中的stoi

      string inventory[10], charactername, work;
      int gameposition;
      for (int i = 0; i < 10; ++i)
          getline(file, inventory[i]);
      getline(file, work);
      gameposition = stoi(work);
      getline(file, charactername);
      

      更新

      cout << "Gameposition: " + gameposition << endl;
      cout << "Your character: " + selectedChar << endl;
      

      应该是

      cout << "Gameposition: " << gameposition << endl;
      cout << "Your character: " << selectedChar << endl;
      

      【讨论】:

        【解决方案3】:

        这部分目前正在工作。

        txt 文件包含:

            ITEM1
            ITEM2
            ITEM3
            ITEM4
            ITEM5
            ITEM6
            ITEM7
            ITEM8
            ITEM9
            ITEM10
            0
            NATHAN
        
        
        string savedinv[10], charactername, work;
            int gameposition;
            ifstream file;
            file.open("C:/apoadventure/savegame.txt");
            cout << "Inventory loaded:\n";
            cout << "\n";
            for (int i = 0; i <= 9; ++i) {
                getline(file, savedinv[i]);
                cout << savedinv[i] << "\n";
            }
            cout << "\n";
            getline(file, work);
            gameposition = stoi(work);
            getline(file, charactername);
            selectedChar = charactername;
            file.close();
        
            //handling information
            //inventory + gameposition + selectedChar
            inventory[0] = savedinv[0];
            inventory[1] = savedinv[1];
            inventory[2] = savedinv[2];
            inventory[3] = savedinv[3];
            inventory[4] = savedinv[4];
            inventory[5] = savedinv[5];
            inventory[6] = savedinv[6];
            inventory[7] = savedinv[7];
            inventory[8] = savedinv[8];
            inventory[9] = savedinv[9];
        
        
        
        

        但是当我尝试调用游戏位置时,它什么也没给我。

            cout << "Gameposition: " + gameposition << endl;
            cout << "Your character: " + selectedChar << endl;
            cout << "\n";
        

        输出:

            ITEM1
            ITEM2
            ITEM3
            ITEM4
            ITEM5
            ITEM6
            ITEM7
            ITEM8
            ITEM9
            ITEM10
        
            Gameposition:
            Your character: NATHAN
        
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2012-10-11
          • 1970-01-01
          • 1970-01-01
          • 2014-06-21
          • 2012-06-13
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多