New >> how would I read a file that has 3 columns and each column contains 100 numbers in an array?

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(); } 
+4
source share
2 answers
 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 that you always have 3 numbers in a row. If you know the exact number of lines, replace this time with for from 0 to the number of lines.

+3
source

Rule number 1 on reading data from a file: do not trust the contents of the file. You never know with absolute certainty what is in a file until you read it.

However, one correct way to read data lines from a file, where each line consists of several fields with space separators, 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 a file stream using getline , then parse the string into three integers using stringstream . This allows us to be sure that each line is formatted correctly.

We test to ensure that integers are successfully extracted before we add them to arrays to ensure that arrays always only have valid data.

There is another error handling:

  • In the example, if fetching integers from a string fails, we simply ignore that string; It might be a good idea to add logic to interrupt 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 in the line after the required integers, depending on how strict the file formatting should be.
  • After we finish reading the file, we need to check that eof() installed, and not fail() or bad() ; if one of these two flags is set, an error occurred while reading the file.
+3
source

Source: https://habr.com/ru/post/1307892/


All Articles