Hello,
iam having trouble with reading a text file that contains , [ ]
eg. 3715,4[9]
4356,3[7,3]

char c;
     int events[12][12];//[row][col]
  ifstream infile;
  infile.open("test.txt");
     while (!infile.eof()) 
     {
        for(int j=0;j<12;j++) 
        {
                for(int k=0; k<12;k++) 
                {
                        if(c!=']') 
                        {
                                   infile >> events[j][k];
                                   infile.get(c);
                         }
                         else events[j][k] = -1;
               }
        }
        
        infile.get(c);

     } //end while
     infile.close();

for some reason it doesn't jump out of the k loop. j always stays 0

Thanks.

Hello,
iam having trouble with reading a text file that contains , [ ]
eg. 3715,4[9]
4356,3[7,3]

char c;
     int events[12][12];//[row][col]
  ifstream infile;
  infile.open("test.txt");
     while (!infile.eof()) 
     {
        for(int j=0;j<12;j++) 
        {
                for(int k=0; k<12;k++) 
                {
                        if(c!=']') 
                        {
                                   infile >> events[j][k];
                                   infile.get(c);
                         }
                         else events[j][k] = -1;
               }
        }
        
        infile.get(c);

     } //end while
     infile.close();

for some reason it doesn't jump out of the k loop. j always stays 0

Thanks.

It worked find in Visual C++ 6, but I know that Vis C doesn't behave correctly with variables declared inside if statements.

Try moving the declaration of j and k outside the loops...this may fix your problem, no guarantees.

char c;
  int events[12][12];//[row][col]
  int j, k;

  ifstream infile;
  infile.open("test.txt");

  while (!infile.eof()) 
  {
    for(j=0;j<12;j++) 
    {
      for(k=0; k<12;k++) 
      {
        if(c!=']') 
        {
          infile >> events[j][k];
          infile.get(c);
        }
        else events[j][k] = -1;
      }
    }
        
    infile.get(c);

  } //end while
  infile.close();

Hello,
iam having trouble with reading a text file that contains , [ ]
eg. 3715,4[9]
4356,3[7,3]

char c;
     int events[12][12];//[row][col]
  ifstream infile;
  infile.open("test.txt");
     while (!infile.eof()) 
     {
        for(int j=0;j<12;j++) 
        {
                for(int k=0; k<12;k++) 
                {
                        if(c!=']') 
                        {
                                   infile >> events[j][k];
                                   infile.get(c);
                         }
                         else events[j][k] = -1;
               }
        }
        
        infile.get(c);

     } //end while
     infile.close();

for some reason it doesn't jump out of the k loop. j always stays 0

Thanks.

For one thing, c is never initialized to anything so the first time you test it for ']' it contains garbage.

Next, you use while (!infile.eof()) which is also a bad thing, as explained here ( feof() is the same as .eof() )

Outside of that, I'm not sure what's going on. underjack may be right.

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