views:

85

answers:

2
int exam1[100];// array that can hold 100 numbers for 1st column 
int exam2[100];// array that can hold 100 numbers for 2nd column 
int exam3[100];// array that can hold 100 numbers for 3rd column  

int main() 
{ 
  ifstream infile;   

  int num; 
  infile.open("example.txt");// file containing numbers in 3 columns 
     if(infile.fail()) // checks to see if file opended 
    { 
      cout << "error" << endl; 
    } 
       while(!infile.eof()) // reads file to end of line 
      { 
         for(i=0;i<100;i++); // array numbers less than 100 
           { 
        while(infile >> [exam]); // while reading get 1st array or element 
            ???// how will i go read the next number 
            infile >> num; 
          } 
      } 
  infile.close(); 
} 
A: 
int exam1[100];// array that can hold 100 numbers for 1st column 
int exam2[100];// array that can hold 100 numbers for 2nd column 
int exam3[100];// array that can hold 100 numbers for 3rd column  

int main() // int main NOT void main
{ 
  ifstream infile;   

  int num = 0; // num must start at 0
  infile.open("example.txt");// file containing numbers in 3 columns 
     if(infile.fail()) // checks to see if file opended 
    { 
      cout << "error" << endl; 
      return 1; // no point continuing if the file didn't open...
    } 
       while(!infile.eof()) // reads file to end of *file*, not line
      { 
         infile >> exam1[num]; // read first column number
         infile >> exam2[num]; // read second column number
         infile >> exam3[num]; // read third column number

         ++num; // go to the next number

         // you can also do it on the same line like this:
         // infile >> exam1[num] >> exam2[num] >> exam3[num]; ++num;
      } 
  infile.close(); 

  return 0; // everything went right.
} 

I assume you always have 3 numbers per line. If you know the exact number of lines, replace the while with a for from 0 to the number of lines.

IVlad
Note the `homework` tag - it's probably better to just provide helpful hints etc for homework problems rather than complete solutions, otherwise the student won't learn anything.
Paul R
Well, true, but he did try and the problem is pretty small. He probably just didn't know that you can string multiple read operations that way.
IVlad
is this the right syntex for reading a file. i am new and i think i get the idea but im not 100 percent sure u know?
Yes, this is the right syntax.
IVlad
Don't do this: while(!infile.eof()). The basic anti-pattern for reading a file. num will now be 1 to large.
Martin York
+2  A: 

Rule # 1 about reading data from a file: don't trust the contents of the file. You never know with absolute certainty what is in the file until you've read it

That said, one correct way to read lines of data from a file, where each line is composed of multiple whitespace-delimited fields would be to use a combination of getline and stringstream:

std::string line;
while (std::getline(infile, line))
{
    std::stringstream ss(line);
    int a, b, c;
    if (ss >> a >> b >> c)
    {
        // Add a, b, and c to their respective arrays
    }
}

In English, we get each line from the file stream using getline, then parse the line into three integers using a stringstream. This allows us to be certain that each line is formatted correctly.

We check to ensure the extraction of the integers succeeded before we add them to the arrays to ensure that the arrays always have only valid data.

There is other error handling that might be desirable:

  • In the example, if extraction of the integers from the line fails, we just ignore that line; it could be a good idea to add logic to abort the process or report an error.
  • After we get three integers, we ignore the rest of the line; it might be a good idea to add checks to ensure that there is no more data on the line after the required integers, depending on how strict the file's formatting needs to be.
  • After we finish reading the file, we should test to be sure eof() is set and not fail() or bad(); if one of those two flags is set, some error occurred when reading the file.
James McNellis
whoa! i dont even know about stringstream is i am a beginner. is there away achieving this another way? so are you saying std::string line will read lines and not whitespaces? and then the numbers i read will save as a string in the variable line? what is the if statement i dont really understand that one and maybe the other one too
@user: The `while` loop reads each line of the file as a string into `line`. Then the `if` statement using the `stringstream` splits the line and parses it as three `int` variables. You can then copy the `int` variables into the arrays. There are many ways to solve this problem in C++; I think this is by far the simplest method to do it with correct error handling.
James McNellis
ok does it also read the numbers if they are less than 3 maybe just one number?
@user: No, because that wasn't what you specified. It can be easily modified to handle that case, however, with a little research on streams and how extraction works.
James McNellis