【问题标题】:new >> how would i read a file that has 3 columns and each column contains 100 numbers into an array?新 >> 我如何将一个包含 3 列且每列包含 100 个数字的文件读入一个数组?
【发布时间】:2011-02-11 23:03:15
【问题描述】:
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(); 
} 

【问题讨论】:

  • 请注意 void main 不是合法的 C++ - 它必须是 int main

标签: c++ file arrays


【解决方案1】:
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.
} 

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

【讨论】:

  • 注意homework标签——最好只为家庭作业问题提供有用的提示等,而不是完整的解决方案,否则学生将学不到任何东西。
  • 嗯,没错,但他确实尝试过,问题很小。他可能只是不知道你可以这样串多个读操作。
  • 这是读取文件的正确语法吗?我是新手,我想我明白了,但我不是 100% 确定你知道吗?
  • 不要这样做:while(!infile.eof())。读取文件的基本反模式。 num 现在将从 1 变大。
【解决方案2】:

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

也就是说,从文件中读取 数据的一种正确方法是使用 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
    }
}

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

我们检查以确保整数提取成功我们将它们添加到数组以确保数组始终只有有效数据。

还有其他可能需要的错误处理:

  • 在示例中,如果从行中提取整数失败,我们将忽略该行;添加逻辑以中止流程或报告错误可能是个好主意。
  • 得到三个整数后,我们忽略该行的其余部分;添加检查以确保在所需整数之后的行上没有更多数据可能是个好主意,具体取决于文件格式需要的严格程度。
  • 读完文件后,我们应该测试以确保设置了eof()而不是fail()bad();如果设置了这两个标志之一,则在读取文件时发生了一些错误。

【讨论】:

  • 哇!我什至不知道 stringstream 我是初学者。是否有另一种方式实现这一目标?那么您是说 std::string 行将读取行而不是空格吗?然后我读取的数字将保存为变量行中的字符串? if 语句是什么,我真的不明白那个,也许另一个也是
  • @user:while 循环将文件的每一行作为字符串读入line。然后使用stringstreamif 语句拆分该行并将其解析为三个int 变量。然后,您可以将 int 变量复制到数组中。在 C++ 中有很多方法可以解决这个问题;我认为这是迄今为止最简单的正确错误处理方法。
  • 好的,如果数字小于 3 或者只有一个数字,它还会读取数字吗?
  • @user:不,因为那不是您指定的。但是,只需对流和提取的工作原理进行一些研究,就可以轻松对其进行修改以处理这种情况。
  • 不要忘记为 std::stringstream ss(line) 添加 '#include ';
猜你喜欢
  • 2021-03-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-05-24
  • 2021-12-31
  • 1970-01-01
  • 2021-11-08
  • 2023-04-07
相关资源
最近更新 更多