1
  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 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. Thanks

Sean
  • 4,267
  • 10
  • 36
  • 50
  • Can you elaborate a little more? I'm still not quite sure how you want to store your data. So each line has 18 numbers? – user607455 Feb 15 '11 at 03:12
  • Which means each lines has 7 numbers. And the first column has 2 missing numbers every 3 lines. And you want to get this into 7 columns? – user607455 Feb 15 '11 at 03:21
  • I can see visually what you want. But how to do it programatically will depend on the format of the file. What is the definition of the spacing beteen cells, what is the defintion of a NULL cell. Would not col1 be {2, NULL, NULL, 3, NULL, NULL, 4, NULL, NULL, 5, NULL, NULL, 6}; – Martin York Feb 15 '11 at 03:24

5 Answers5

2

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.

Kartikya
  • 455
  • 2
  • 9
1
  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.
Community
  • 1
  • 1
Reno
  • 33,594
  • 11
  • 89
  • 102
  • How can I convert to a 2-D array? using fin.get(ch)? Thanks. – Sean Feb 15 '11 at 03:30
  • @Sean you can use [this](http://www.cplusplus.com/reference/iostream/istream/getline/) to read a line. Its not as complicated as you think :) – Reno Feb 15 '11 at 03:33
1

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);

Benjamin Lindley
  • 101,917
  • 9
  • 204
  • 274
1

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.

user607455
  • 479
  • 5
  • 18
0
# 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;
}
paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953
  • you can mark your code in the editor and press CTRL-K to indent it (must start with 4 or more spaces and be preceded by blank line. Fixed it for you, cheers. – paxdiablo Feb 15 '11 at 03:15
  • I vote up for you but your code seems not working for my case :) – Sean Feb 15 '11 at 03:33