11/29/2010 S 2907 1
11/29/2010 S 9673 4
11/30/2010 A 4321 30
11/31/2010 S 9673 12
12/01/2010 N 5789 wind_chimes 13.54 17.83
12/02/2010 S 5140 5


I have string date, char type and int id, processNumber.
myfile>>date>>type>>id>>processNumber; does not work..what should i do?

This is a double post. You just posted the same question about an hour ago.

You have identified that you have 2 different possibilities for file input:

1) Date, char, 4 digits, int.

2) Date, char, 4 digits, string, double, double.

I would recommend reading in your file line at a time. This will allow you to parse and test the line for it's contents and identify which of the 2 formats you have and respond accordingly.

Probably the easiest cheat method to identify one format over the other, is just to count the number of white spaces; the first format will have 3 white spaces, the other will have 5:

#include<sstream>
#include<string>
#include<vector>


vector<string> tokens;
stringstream line;
string word;

while(getline(infile, line))
{
     while(word << line)
     {
          tokens.push_back(word);
     }

     if(tokens.size() == 4)
     {
          //Handle a line containing 4 pieces of data
     }
     else
     {
          //Handle a line containing 6 pieces of data
     }   

     tokens.clear();  

}

Now you have the ability to be the coolest kid in your computer science class and possibly pick up some hot nerd chicks because you can handle varying formats of data from your file.

Edited 6 Years Ago by Clinton Portis: n/a

This article has been dead for over six months. Start a new discussion instead.