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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2706076/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 00:25:20  来源:igfitidea点击:

new >> how would i read a file that has 3 columns and each column contains 100 numbers into an array?

c++filearrays

提问by user320950

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(); 
} 

采纳答案by IVlad

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 forfrom 0 to the number of lines.

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

回答by James McNellis

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

关于从文件读取数据的规则#1:不要相信文件的内容。在您阅读文件之前,您永远无法绝对确定地知道文件中的内容

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

这就是说,为了读取一个正确的方式线从一个文件,其中每一行是由多个空格分隔的字段的数据的将是使用的组合getlinestringstream

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.

在英语中,我们使用 获取文件流中的每一行getline,然后使用将行解析为三个整数stringstream。这使我们能够确定每一行的格式正确。

We check to ensure the extraction of the integers succeeded beforewe 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.
  • 在示例中,如果从行中提取整数失败,我们就忽略该行;添加逻辑以中止进程或报告错误可能是个好主意。
  • 得到三个整数后,我们忽略该行的其余部分;根据文件格式需要的严格程度,添加检查以确保在所需整数之后的行上没有更多数据可能是一个好主意。
  • 在我们读完文件后,我们应该测试以确保eof()是 set 而不是fail()or bad();如果设置了这两个标志之一,则读取文件时会发生一些错误。