Hello.
I'm trying to read an input file to build a list formed by structs.
The input file has this format:

1 2,15 3,0
2 1,15 3,8
...
the first number of each line will be in what I call NOMBRE, and for the rest of the numbers in the same line, one will be an ADJ and next one will be a PERTE, then again next number ADJ, next PERTE.
Example:
NOMBRE:1 ADJ:2 PERTE:15 ADJ:3 PERTE:0
NOMBRE:2 ADJ:1 PERTE:15 ADJ:3 PERTE:8

However I cann't figure how to stop the loop for the ADJs and PERTEs in order to get the next line work properly, begining with a NOMBRE.
What I get is::

NOMBRE:1 ADJ:2 PERTE:15 ADJ:3 PERTE:0
ADJ:2 PERTE:1 ADJ:15 PERTE:3 ADJ:8

my code :

while(!Lecture.eof())                     
        {
            Lecture>>PtrSommet->nombre;
            cout<<"NOMBRE" <<PtrSommet->nombre;
            do
            {
                type_adj *PtrAdj;
                PtrAdj=new type_adj();
                Lecture>>PtrAdj->nombre;
                cout<<" ADJ" <<PtrAdj->nombre;
                Lecture.ignore(1)>>PtrAdj->perte;
                cout<<" PERTE " <<PtrAdj->perte <<".";
            }while ([B]?????[/B]);
        }

what do I need to put in the do while to finish the line? something like do while (line ends).
BTW. I know that the pointers generation is wrong and the thing about avoiding eof, this code is just for testing purposes.
Thanks.

Consider the strtok function...You can do a getline to get a line from the file, then using strtok you can break up the input, using the spaces and commas as delimeters...

Are all your lines formatted exactly like you specified, with 5 numbers?

If so, Just duplicate the two reads instead of using the DO loop. Or if you need to use a loop, make it a FOR loop and just loop 2 times.

Also, see this about your use of .eof() (it's identical to C's feof() .)

Thanks ft3ssgeek and Waltp.

Does strtok works with int values? In the sites I have visited there isn't information about it.

The number of items in every line will be the same in the same file, but in a different input file, this number will change.

The number of items in every line will be the same in the same file, but in a different input file, this number will change.

Then base the reading of the file on reading the newline (\n). When you read the pair of numbers, read one more character and test what it's value is.

a. read each line in the file into a std::string
b. parse the line that was read using a std::istringstream

enum { MAX_ITEMS_PER_LINE = 25 };
  ifstream file("file_name - perhaps picked up from argv[1]") ;
  string line ;
  while( getline( file, line ) )
  {
    istringstream stm(line) ;
    int NOMBRE, ADJ[MAX_ITEMS_PER_LINE] = {0}, 
                           PERTE[MAX_ITEMS_PER_LINE] = {0} ; 
    int count = 0 ;
    stm >> NOMBRE ;
    while( stm >> ADJ[count] )
    {
      stm.ignore( line.size(), ',' ) ; // eat the comma
      stm >> PERTE[ count++ ] ;
    }
    // do whatever with the info - count == number of items read
  }

enum { MAX_ITEMS_PER_LINE = 25 }; I don't understand why this is an enum. Please explain why it's not a const or something similar.

i tend to use an un-named enum for integral constants because
a. they are as effective
b. they can appear in places where const variables can not.

struct time_of_day
{
   enum { HOURS_PER_DAY = 24, /* ... */ };
   // ...
};

in a compliant compiler, this is possible

struct time_of_day
{
   static const int HOURS_PER_DAY = 24 ;
   // ...
};

in the header.
and in the implementation file,

const int time_of_day::HOURS_PER_DAY ; // do not initialize here too!

i am too lazy to do this much work; the anonymous enum is simpler.

Thanks for your reponses WaltP and vijayan121. I will try this code and return here to comment it.

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