2

i'm reading file which has some number of columns each line has different number of columns and they are numerical values of different length, and i have fix number of rows (20) how to put each column in to array?

suppose i have data file like following (there is tab between each column)

20   30      10
22   10       9       3     40 
60    4
30    200   90
33    320    1        22    4

how to put these columns into different array,, that column 1 goes to one arrary and column 2 goes to another. Only column 2 has more than 2 digit values and the rest of columns has 1 or two digit values, and some columns are null too except 1, 2, and 3

int main()
{     
    ifstream infile;
    infile.open("Ewa.dat");

    int c1[20], c2[20], ... c6[20];

    while(!infile.eof()) { 
        //what will be the code here?
        infile >>lines;
        for(int i = 1; i<=lines; i++) {
            infile >> c1[i];     
            infile >> c2[i];
             .
             .
            infile >> c6 [20]; 
        }
    }
}
4
  • @skaffman You probably meant to tag C++ instead of C ? Commented May 9, 2012 at 18:12
  • i wanted to tag C++ and i did it.. why? Commented May 9, 2012 at 18:24
  • No, you didn't tag it at all. Someone else edited and added a C tag and then another person corrected the C tag to C++. Commented May 9, 2012 at 18:28
  • okay i'm sorry, i forgot to mention, i accept my mistake Commented May 9, 2012 at 18:32

2 Answers 2

1

Here's the main idea:

Use a 2D array instead of many 1D arrays.
Read each line using std::string.
Then use: istringstream is(str); which will help parse the string and put into the arrays like this:

while(...) {
    ....
    getline(infile, str);
    istringstream is(str);
    j=0;
    while(is)
        {
            is >> c[i][j];
        }
    i++;
    ....
    }

I'll leave the rest to you.

Sign up to request clarification or add additional context in comments.

1 Comment

thanks very much KingsIndian for your time and help, it was really helpful
1

It might be easier to take advantage of some of some C++ library classes, like std::vector, std::istringstream, and std::string:

#include <vector>
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>

int main() {
  std::vector<std::vector<int> > allData;

  std::ifstream fin("data.dat");
  std::string line;
  while (std::getline(fin, line)) {      // for each line
    std::vector<int> lineData;           // create a new row
    int val;
    std::istringstream lineStream(line); 
    while (lineStream >> val) {          // for each value in line
      lineData.push_back(val);           // add to the current row
    }
    allData.push_back(lineData);         // add row to allData
  }

  std::cout << "row 0 contains " << allData[0].size() << " columns\n";
  std::cout << "row 0, column 1 is " << allData[0][1] << '\n';
}

1 Comment

can you fix the problem of garbage values, it is also filling the array for which i don't have any value in the data file?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.