【问题标题】:Odd output when passing file stream by reference通过引用传递文件流时的奇怪输出
【发布时间】:2015-05-01 22:42:08
【问题描述】:

在下面的代码中,我需要从数据文件中读取以下内容:

Dean DeFino     88  98  99
Sally Johnson   78  89  82
Bill Benny      75  79  81
Thomas Billows  78  84  89 

但是,我得到了这个输出:

Dean DeFino           88        98        99
hnson   78  89        82-1850860984     32742
        0         0        28
         0-413051128        48
-1608528832     32767-415228472
0    -49089-1024065536   6406657
-512190696        48     18048
     32767         0         0
0-1850766544     32742-520043854
0        48-517861416        48
-1608527569     32767-520043904
0     32767-520041858        48
-1564475392    -49089         0
        48-1850766544     32742
-514484708        48     26100
         0-1608527632     32767
`-1608528304     32767-310120049
0        48         0         0
-1608527592     32767-1608527584
        48-520056170        48
`-1850862392     32742-1608528432
     32742 479434334         0
   7491161         0        30
a         0-1608528048     32767
H<@0-1024065536   6406657-1398731262
f 673193995 5371330921477443664
DB(         0         0-1850860160
        48-515832000        48
0      2027         1-520053849
0     32742-1608527968     32767
-1850860160     32742 479434334
         0-1850860160     32742
         6         0-1850860928
        48         6         0
[-310357908        48         0
        48-520056170        48
-310356040        48-1608527984
     32742 725871085         0
  11341735         0        45
0         0-1608527600     32767
H<@0-310281904        48-1850766920
     32767-1850767776     32742
         0         0-1850860808
         0-515832000        48
@@           786         1         0
        48-1608527520     32767
-1850860808     32742 725871085
         0-1850860808     32742
          1     32767-517860984
0         1-310305400        48

似乎只有第一行半被正确读取。鉴于下面的代码,我看不出问题出在哪里。有什么想法吗?

#include <fstream>
#include <iostream>
#include <iomanip>
using namespace std;

const int NAMESIZE = 15;
const int MAXRECORDS = 50;
struct Grades                           
{
    char name[NAMESIZE + 1];
    int test1;
    int test2;
    int final;

};

typedef Grades gradeType[MAXRECORDS];

void readIt(ifstream&, gradeType, const int);

int main()

{
    ifstream indata;
    indata.open("graderoll.dat");               
    gradeType studentRecord;

    if (!indata)
    {
        cout << "Error opening file. \n";
        cout << "It may not exist where indicated" << endl;
        return 1;
    }

    readIt(indata, studentRecord, MAXRECORDS);


    for (int count = 0; count < MAXRECORDS; count++)
    {
        cout << studentRecord[count].name << setw(10)
             << studentRecord[count].test1
             << setw(10) << studentRecord[count].test2;
        cout << setw(10) << studentRecord[count].final << endl;
    }

    indata.close();

    return 0;
}

void readIt(ifstream& inData, gradeType gradeRec, const int max)

{
    int total = 0;

    inData.get(gradeRec[total].name, NAMESIZE);
    while (inData)
    {
        inData >> gradeRec[total].test1;
        inData >> gradeRec[total].test2;
        inData >> gradeRec[total].final;

        total++;     // add one to total

        if (!inData.eof())
            inData.get(gradeRec[total].name, NAMESIZE);
    }

}

谢谢!

【问题讨论】:

  • 您可能需要在最后一行 &gt;&gt; 之后跳过行尾字符。您可以尝试在total++; 之前添加inData &gt;&gt; std::ws; 以跳过它。
  • @Galik 那没用:/
  • 你在使用调试器时,读入第二个名字后的name变量的内容是什么?

标签: c++ file pass-by-reference fileinputstream


【解决方案1】:

出现问题是因为while 循环正在寻找整数,并且遇到了第二个名称,它不是整数。

更好的方法是将整行读取为字符串,然后使用std::istringstream 从字符串中获取名称和测试值。

【讨论】:

    【解决方案2】:

    在读取while 循环时,没有代码消耗行尾,因此程序遇到行尾,它会跳过文件的其余部分并终止循环。屏幕上奇怪的输出是输出 49 个垃圾值的空数组的结果(自 MAXRECORDS = 50 起)。创建一个名为numRead 或类似名称的变量,并将其设置为等于您要读取的行数。把它作为结束条件放在for 循环中。然后在while 循环中,将 inData.ignore(NUMSIZE, '\n'); 放在读取名称的位置之前。所以:

    while (inData)
    {
        inData >> gradeRec[total].test1;
        inData >> gradeRec[total].test2;
        inData >> gradeRec[total].final;
    
        total++;     // add one to total
    
        inData.ignore(NUMSIZE, '\n');
        inData.get(gradeRec[total].name, NAMESIZE);
    }
    

    inData.ignore(NUMSIZE, '\n'); 将有效地告诉程序忽略接下来的 15 个字符或直到遇到换行符转义序列。这将允许正确读取文件。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-05-09
      • 1970-01-01
      • 2015-05-02
      • 2023-03-14
      • 1970-01-01
      • 1970-01-01
      • 2015-12-30
      相关资源
      最近更新 更多