Hi there,

I need to read a file with unknown number of columns and lines. Basically, I have an ID per line (ID00005) and many columns containing 2 letters. The format is:

ID00005 RR WW SS QQ TT RR UU II
ID00008 WW SS QQ YY HH JJ KK LL
ID00011 WW SS QQ YY HH JJ KK LL
...

I don't known how to read line by line, getting column by column after ID (RR WW SS QQ TT ...) and do some calculations using the column information (RR WW). The calculation is count the number of occurrence of each two letters Example line 1 (RR = 2 times)

I only know how to get ID and number of columns that I can count

The idea is read the first line, do the calculation based on the elements of columns and after that, go to the next line

Can anyone help me?

Thanks a lot!

string ID = "";

if ( myfile.is_open() )
	{
		while(myfile >> lineString)
		{
			ID = lineString;
			myfile >> lineString;
                        // ??????????????????????????
			
		}
	}
	else
		cout << "Unable to open file." << endl;

Recommended Answers

All 3 Replies

Perhaps something like this.

string line;
  while ( getline(file,line) ) {
    istringstream s(line);
    string word;
    while ( s >> word ) {
      cout << line << " ";
    }
    cout << endl;
  }

Hi Salem, thanks a lot!

I have just one doubt, how I will take just the columns AFTER id? (RR TT YY ...)

Thanks for your help

Thanks Salem! SOLVED! Cheers and have a nice weekend

string line;
  while ( getline(file,line) ) {
    istringstream s(line);
    string word;
    s >> word; // ADD
    while ( s >> word ) {
      cout << word << " ";
    }
    cout << endl;
  }
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.