【问题标题】:Sorting out int variables from a .txt file (fstream, c++)从 .txt 文件中整理出 int 变量(fstream、c++)
【发布时间】:2021-02-26 10:23:02
【问题描述】:

我有一个 .txt 文件,这些在顶部的这一行 '12 4 25 257' 每个数字由 ' ' 隔开,该行以 '\n' 结尾

我有这些变量和一个函数getFirstLine

int A;
int B;
int C;
int D;
ifstream File("a.txt");
void getFirstLine(int &A, int &B, int &C, int &D)
{
 int list[] = {A, B, C, D};
    string myText;
    getline(File, myText);
    int size = myText.size();
    cout << size;
    for (int i = 0; i < size; i++)
    {
        if (myText[i] != ' ')
        {
            list[i] = (int)myText[i] - 48;
            cout << list[i] << endl;
        }
    }
}

我想基本上将第一个数字保存在 A 上,第二个保存在 B 上,第三个 C 等等...

可悲的是,我似乎无法正常工作:( 有人可以帮我解决这个问题吗?

输出应如下所示:

A = 12,
B = 4,
C = 25,
D = 257,

【问题讨论】:

  • 您大致走上了一条不错的道路,但您似乎已经发现,它非常笨拙。考虑使用标准化的解析函数 (std::stoi),或者直接使用提取运算符来提取数字。
  • 读一行,你已经这样做了。然后将 line 放入 std::istringstream 中,然后使用它来解析数字。你现在所做的并没有做任何你想做的事情,没有任何意义,并且会导致未定义的行为。

标签: c++ file c++11 fstream ifstream


【解决方案1】:

也许是这样的?

std::string line;
std::getline(file, line);
line = line.substr(1, line.length() - 2);
std::istringstream iss(line);
iss >> A >> B >> C >> D;

【讨论】:

    【解决方案2】:
    #include<iostream> 
    #include<vector>
    #include<ifstream>
    
    using namespace std; 
    
        struct pairs
        {
            char ch; 
            int value;
        };
        int main()
        {   
            vector<pairs> store;
            ifstream is("koord.txt"); 
            pairs temp;
            char ch = 'A';
            while (is)
            {
                temp.ch = ch; 
                is >> temp.value; 
                store.push_back(temp);
            }
            for (int i = 0; i < store.size(); ++i)
            {
                cout << store[i].ch << " " << store[i].value << endl;
            }
            return 0;
        }
    

    你可以使用它。但是,如果您阅读更多条目,char 将会溢出。您可以使用字符串而不是字符。

    【讨论】:

    • 对了,我忘记增加char了。如果你添加 ++ch; push_back 函数后字母会增加。
    • 谢谢 - 这很有用!无论如何,使用结构实际上对我需要做的事情很有意义:)
    【解决方案3】:

    这就是我最终要做的,可能不是最有效的方法,但它似乎有效。但我会调查std::istringstream。这似乎是个不错的方法。

    void getFirstLine(int &A, int &B, int &C, int &D)
    {
        string myText;
        string temp;
        vector<int> save;
        int size = myText.size();
        getline(File, myText); //get the first line of text not including \n
        for (int i = 0, j = 0, size = myText.length(); i < size; i++)
        {
            temp.push_back(myText[i]); //adds the myTest[i] to the end of temp
            if (myText[i] == ' ')
            {
                save.push_back(stoi(temp)); //stoi(temp) to an int --> push_back appened to save at the end
                j++;
                temp.clear();
            }
            if (i == (size - 1)) //because the end of the line doesnt have a space
            {
                save.push_back(stoi(temp));
                j++;
                temp.clear();
            }
        }
         for (int i = 0; i < 5; i++){
             cout << "list "<< i << ": "<< save[i] << endl;
         }
         //retruning the values
         A = save[0];
         B = save[1];
         C = save[2];
         D = save[3];
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-12-12
      • 2012-07-02
      • 1970-01-01
      相关资源
      最近更新 更多