开发者

read data from text file

开发者 https://www.devze.com 2023-02-11 01:00 出处:网络
2136090 295008 3100606 3110408 217008 350405 41310007 257020 386007 5140980 262007 3100505 6122080 I have many text files. The format is like the above one. I hope to store each column data to diffe
  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, e.g., col01[5] ={2,3,4,5,6}(corresponding to the 1st column). How can I do this? col02[15] ={1,2,3......}(corresponding to the 2nd column data).

The number in the first column is not fixed and the position is also random. For example, the numbers in the first column are random开发者_高级运维ly 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. Thanks


The simpler and more efficient way would be to scan the file character by character, i.e increment "i" aand compare for each value. if(i==" ") // if the character is " " SPACE then do nothing /\/\ if(i==10) // if the character is ascii(10) i.e ENTER then switch to col01 /\/\ else go on storing the DIGITS in col01, then col02 on and on till 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.


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. You'll need to do it manually, you can't use 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);


For this specific question.

Declare 7 columns of 13 space.

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.


  1. Convert the text to a 2- D array (you can use this for splitting by spaces)
  2. Transpose the array ( like this )
  3. Read each row of the array.


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

精彩评论

暂无评论...
验证码 换一张
取 消