new >> 我如何将一个包含 3 列且每列包含 100 个数字的文件读取到一个数组中?

use*_*950 4 c++ arrays file

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(); 
} 
Run Code Online (Sandbox Code Playgroud)

IVl*_*lad 5

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.
} 
Run Code Online (Sandbox Code Playgroud)

我假设你每行总是有 3 个数字。如果您知道确切的行数,请将 while 替换为for0 到行数。