简体   繁体   English

从文本文件读取数据

[英]read data from text file

  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

I have many text files. 我有很多文本文件。 The format is like the above one. 格式类似于上面的格式。 I hope to store each column data to different array, eg, col01[5] ={2,3,4,5,6} (corresponding to the 1st column). 我希望将每个列的数据存储到不同的数组,例如col01[5] ={2,3,4,5,6} (对应于第一列)。 How can I do this? 我怎样才能做到这一点? col02[15] ={1,2,3......} (corresponding to the 2nd column data). col02[15] ={1,2,3......} (对应于第二列数据)。

The number in the first column is not fixed and the position is also random. 第一栏中的数字不是固定的,位置也是随机的。 For example, the numbers in the first column are randomly located in some lines. 例如,第一列中的数字随机位于某些行中。 The column number is fixed . 列号是固定的 It may be in the following format: 它可能采用以下格式:

  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

I tried to use istringstream and getline but it is too complicated. 我尝试使用istringstreamgetline但是它太复杂了。 Thanks 谢谢

The simpler and more efficient way would be to scan the file character by character, ie increment "i" aand compare for each value. 更简单,更有效的方法是逐字符扫描文件,即递增“ i”并比较每个值。 if(i==" ") // if the character is " " SPACE then do nothing /\\/\\ if(i==10) // if the character is ascii(10) ie ENTER then switch to col01 /\\/\\ else go on storing the DIGITS in col01, then col02 on and on till col07. if(i ==“”)//如果字符是“”空格,则不执行/// \\ if(i == 10)//如果字符是ascii(10),即ENTER,然后切换到col01 / \\ / \\否则继续将数字存储在col01中,然后将col02连续存储到col07中。

This is the abstract of your problem's solution. 这是您的问题解决方案的摘要。 Hope it helps. 希望能帮助到你。 If it doesn't let me now, I'll be glad to help again. 如果现在不让我,我将很高兴再次提供帮助。

  1. Convert the text to a 2- D array ( you can use this for splitting by spaces ) 将文本转换为2-D数组( 您可以将其用于按空格分割
  2. Transpose the array ( like this ) Transpose置数组( 像这样
  3. Read each row of the array. 读取数组的每一行。

Keep a std::map<int,std::vector<int>> , pairing integers with the column they are in. Read through each line until you find a number. 保持std::map<int,std::vector<int>> ,将整数与它们所在的列配对。通读每一行,直到找到一个数字。 You'll need to do it manually, you can't use operator>> . 您需要手动进行操作,不能使用operator>> You'll need to read to the end of the number to determine which column it's in, then: the_map[the_column].push_back(the_number); 您需要读取数字的末尾以确定它在哪一列,然后: the_map[the_column].push_back(the_number);

For this specific question. 对于这个具体问题。

Declare 7 columns of 13 space. 声明7列的13个空格。

Read a line. 阅读一行。 First number goes to first col if first char is not a space. 如果第一个字符不是空格,则第一个数字进入第一个列。 Reads until next number. 读取直到下一个数字。 Goes to 2nd col. 转到第二列。 Repeat. 重复。

# 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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM