【问题标题】:read data from text file从文本文件中读取数据
【发布时间】:2011-02-15 03:07:52
【问题描述】:
  2      1     3       6    0    9    0
         2     9       5    0    0    8
         3    10       0    6    0    6
  3      1     1       0    4    0    8
         2     1       7    0    0    8
         3     5       0    4    0    5
  4      1     3      10    0    0    7
         2     5       7    0    2    0
         3     8       6    0    0    7
  5      1     4       0    9    8    0
         2     6       2    0    0    7
         3    10       0    5    0    5
  6      1     2       2    0    8    0

我有很多文本文件。格式和上面的一样。我希望将每列数据存储到不同的数组中,例如col01[5] ={2,3,4,5,6}(对应第一列)。我怎样才能做到这一点? col02[15] ={1,2,3......}(对应第2列数据)。

第一列的数字不是固定的,位置也是随机的。例如,第一列中的数字随机位于某些行中。列号固定。它可能是以下格式:

  2      1     3       6    0    9    0
  2      2     9       5    0    0    8
         3    10       0    6    0    6
  3      1     1       0    4    0    8
         2     1       7    0    0    8
  5      3     5       0    4    0    5
  4      1     3      10    0    0    7
         2     5       7    0    2    0
         3     8       6    0    0    7
  5      1     4       0    9    8    0
         2     6       2    0    0    7
         3    10       0    5    0    5
  6      1     2       2    0    8    0

我尝试使用istringstreamgetline,但它太复杂了。谢谢

【问题讨论】:

  • 你能详细说明一下吗?我仍然不太确定您要如何存储数据。所以每行有18个数字?
  • 这意味着每行有 7 个数字。第一列每 3 行有 2 个缺失数字。你想把它分成 7 列吗?
  • 我可以直观地看到你想要什么。但是如何以编程方式进行操作将取决于文件的格式。单元格间距的定义是什么,NULL单元格的定义是什么。 col1 不会是 {2, NULL, NULL, 3, NULL, NULL, 4, NULL, NULL, 5, NULL, NULL, 6};

标签: c++ text


【解决方案1】:

更简单、更有效的方法是逐个字符扫描文件,即递增“i”并比较每个值。 if(i==" ") // 如果字符是 " " SPACE 则什么也不做 /\/\ if(i==10) // 如果字符是 ascii(10) 即 ENTER 然后切换到 col01 /\/\ 否则继续在 col01 中存储 DIGITS,然后在 col02 中继续存储直到 col07。

这是您的问题解决方案的摘要。 希望能帮助到你。 如果现在不让我,我很乐意再次提供帮助。

【讨论】:

  • 这不起作用,因为第 1 列中缺少一些数字。
【解决方案2】:

保留std::map<int,std::vector<int>>,将整数与它们所在的列配对。通读每一行,直到找到一个数字。您需要手动完成,不能使用operator>>。您需要阅读到数字的末尾以确定它在哪一列,然后:the_map[the_column].push_back(the_number);

【讨论】:

    【解决方案3】:

    针对这个具体问题。

    声明 7 列,每列 13 个空格。

    读一行。 如果第一个字符不是空格,则第一个数字转到第一个 col。 读到下一个数字。转到第 2 栏。 重复。

    【讨论】:

      【解决方案4】:
      1. 将文本转换为二维数组 (you can use this for splitting by spaces)
      2. Transpose 数组(like this
      3. 读取数组的每一行。

      【讨论】:

      • 如何转换为二维数组?使用 fin.get(ch)?谢谢。
      • @Sean 您可以使用this 读取一行。它没有你想象的那么复杂:)
      【解决方案5】:
      # include < iostream>
      # include < fstream>
      using namespace std;
      int main()
      {
        char ch;
        char str[256];
        ofstream fout("test.dat");
        if(!fout) {
          cout << "Cannot open file for output.\n";
          return 1;
        }
        fout << "This is a line of text.\n";
        fout << "This is another line of text.\n";
        fout << "This is the last line of text.\n";
        fout.close();
        if(!fout.good()) {
          cout << "An error occurred when writing to the file.\n";
          return 1;
        }
        ifstream fin("test.dat", ios::in);
        if(!fin) {
          cout << "Cannot open file for input.\n";
          return 1;
        }
        cout << "Use get():\n";
        cout << "Here are the first three characters: ";
        for(int i=0; i < 3; ++i) {
          fin.get(ch);
          cout << ch;
        }
        cout << endl;
        fin.get(str, 255);
        cout << "Here is the rest of the first line: ";
        cout << str << endl;
        fin.get(ch);
        cout << "\nNow use getline():\n";
        fin.getline(str, 255);
        cout << str << endl;
        fin.getline(str, 255);
        cout << str;
        fin.close();
        if(!fin.good()) {
          cout << "Error occurred while reading or closing the file.\n";
          return 1;
        }
        return 0;
      }
      

      【讨论】:

      • 您可以在编辑器中标记您的代码,然后按 CTRL-K 进行缩进(必须以 4 个或更多空格开头,并且前面有空行。为您修复它,干杯。
      • 我投票支持你,但你的代码似乎不适用于我的情况 :)
      猜你喜欢
      • 2021-06-12
      • 1970-01-01
      • 2011-02-08
      • 2012-06-10
      • 2020-05-26
      • 2023-03-06
      • 2015-08-11
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多