c++ - 为什么我的数据流在 20 或 30 行后开始重复?

标签 c++ arrays string file-io

<分区>

Possible Duplicate:
.eof() loop not working

所以我有一个相当简单的 C++ 程序,我试图读取包含几列数据的文本文件,然后重新格式化这些行并将它们输出到不同的文本文件中。 (重新格式化基本上只涉及获取某些行,省略其他行,并在每行的末尾附加一些额外的数据)。它似乎工作得很好,但在几个不同的数据文件上,它似乎特别卡在一行上并开始无限期地输出那一行——我必须手动停止这个过程。我认为这可能与“fileSize”变量的存储方式有关(如果您查看输出,您会发现重复的 fileSize 对该行不正确,并且它与该行相对应在它开始重复之前)。除此之外,我不知道要查找或修复什么,而且我已经为这个问题苦苦挣扎了好几天。我将包括代码和运行它的输出。任何想法或建议将不胜感激。

代码:

#include <iostream>
#include <fstream>
#include <assert.h>
#include <iomanip>
#include <string>


using namespace std;

int main(int argc, char* argv[]) { 
  //cout << "argc = " << argc << endl; 
  //for(int i = 0; i < argc; i++) 
  //  cout << "argv[" << i << "] = " << argv[i] << endl; 

  ifstream inFile;
  ofstream outFile;

  int numReq, fileSize;
  string lang, dash, langBefore, langAfter;
  string beforeDash, afterDash, beforeNumReq, afterNumReq;
  string beforeFileSize, afterFileSize;

  char fileName[60];
  char outfileName[60];
  char confirm[10];
  char confirm2[10];
  int character;
  char year[5];
  char month[3]; 
  char day[3];
  char hour[3];

  for (int a = 0;a < 60; a++){
    fileName[a] = argv[1][a];
  }

  //fileName = argv[1];


  year[0] = fileName[14];
  year[1] = fileName[15];
  year[2] = fileName[16];
  year[3] = fileName[17];
  year[4] = '\0';

  month[0] = fileName[18];
  month[1] = fileName[19];
  month[2] = '\0';

  day[0] = fileName[20];
  day[1] = fileName[21];
  day[2] = '\0';

  hour[0] = fileName[23];
  hour[1] = fileName[24];
  hour[2] = '\0';

  //cout << "fileName = "<<fileName<<"?"<<endl;
  //cin >>confirm;


  //cout << "So, we have: year = "<<year<<", month = "<<month<<", day = "<<day
  //     <<", and hour = "<<hour<<"?"<<endl;

  inFile.open(fileName);
  assert (!inFile.fail());

  for (int i=0;i<30;i++){  
    outfileName[i] = fileName[i];
  }
  outfileName[29] = '_';
  outfileName[30] = 'O';
  outfileName[31] = 'U';
  outfileName[32] = 'T';
  outfileName[33] = 'P';
  outfileName[34] = 'U';
  outfileName[35] = 'T';
  outfileName[36] = 'p';
  outfileName[37] = 'r';
  outfileName[38] = '.';
  outfileName[39] = 't';
  outfileName[40] = 'x';
  outfileName[41] = 't';
  outfileName[42] = '\0';



  outFile.open(outfileName);
  //Now, add the commands to manipulate the data:

  outFile << fixed << showpoint; 
  outFile << setprecision(2);    

  cout << "Processing data from " << fileName <<" to "<<outfileName<<"."<<endl;

  inFile >> lang;

  cout << "about to begin the 'while' statement." << endl;

  while (!inFile.eof() ){
    string dot (".");
    size_t found;
    found = lang.find(dot); 


    beforeDash = dash;
    beforeNumReq = numReq;
    beforeFileSize = fileSize;

    if (found == string::npos){ //should this be != or == ?

      outFile << lang << " ";

      //*see footnote*

      inFile >> dash >> numReq >> fileSize;
      outFile << numReq << " " << fileSize << " " << year << " " << month
          << " " << day << " " << hour << endl;
      cout <<"Read: "<<lang<<" "<<dash<<" "<<numReq<<" "<<fileSize<<"."<<endl;
      afterDash = dash;
      afterNumReq = numReq;
      afterFileSize = fileSize;      
    }
    else{
      inFile >> dash >> numReq >> fileSize;

      cout <<"Read: "<<lang<<" "<<dash<<" "<<numReq<<" "<<fileSize<<"."<<endl;

      afterDash = dash;
      afterNumReq = numReq;
      afterFileSize = fileSize;

      //      if (beforeFileSize == afterFileSize){ 
      //    cout <<"Why oh why is the afterFileSize = >>" << "<<?" << endl;
      //    cin >> confirm;
      //       }  
    }


    langBefore = lang;
    inFile >> lang;
    langAfter = lang;

    //if (langBefore == langAfter)
    //  cin >>confirm;


  }

  //cout << "after the 'while' statement" << endl;

  inFile.close();
  //assert(!inFile.fail());
  outFile.close();      

  return 0;
}

...以及运行“./fileIO ./fileIO projectcounts-20091201-100000”的输出:

./fileIO projectcounts-20091201-100000
Processing data from projectcounts-20091201-100000 to projectcounts-20091201-100000_OUTPUTpr.txt.
about to begin the 'while' statement.
Read: aa - 21 138053.
Read: aa.b - 19 250491.
Read: aa.d - 1 4440.
Read: ab - 56 1324271.
Read: ab.d - 2 21830.
Read: ace - 158 2166792.
Read: af - 5505 55172658.
Read: af.b - 34 528378.
Read: af.d - 429 3378595.
Read: af.n - 1 7290.
Read: af.q - 62 570762.
Read: af.s - 2 14480.
Read: af.v - 2 14340.
Read: ak - 206 3819300.
Read: ak.b - 5 41948.
Read: ak.d - 2 13046.
Read: als - 1294 21339647.
Read: als.b - 23 262665.
Read: als.d - 26 161574.
Read: als.n - 1 7170.
Read: als.q - 14 72963.
Read: am - 431 7073857.
Read: am.d - 19 201112.
Read: am.q - 8 41454.
Read: an - 3084 21488152.
Read: an.d - 127 888272.
Read: ang - 466 7408138.
Read: ang.b - 41 282335.
Read: ang.d - 104 742003.
Read: ang.q - 14 91779.
Read: ang.s - 3 13748.
Read: ar - 62379 1279758510.
Read: ar.b - 384 8037892.
Read: ar.d - 824 8155089.
Read: ar.n - 265 3874251.
Read: ar.q - 194 1812685.
Read: ar.s - 1262 29202150.
Read: ar.v - 1 7170.
Read: arc - 169 2917934.
Read: arc.d - 2 14340.
Read: arz - 1059 18451346.
Read: arz.d - 1 7170.
Read: as - 94 1878525.
Read: as.b - 21 345858.
Read: as.n - 1 7170.
Read: ast - 932 13396685.
Read: ast.b - 21 118692.
Read: ast.d - 131 1049840.
Read: ast.n - 1 7170.
Read: ast.q - 16 95484.
Read: ast.s - 1 7170.
Read: av - 72 1164965.
Read: ay - 151 2382396.
Read: ay.b - 20 252200.
Read: ay.d - 11 76045.
Read: ay.q - 1 7180.
Read: az - 4060 56547487.
Read: az.b - 55 760781.
Read: az.d - 90 777134.
Read: az.q - 40 258399.
Read: az.s - 49 407367.
Read: az.v - 2 14350.
Read: ba - 190 4256164.
Read: ba.b - 5 26948.
Read: ba.d - 2 6709.
Read: bar - 728 13597602.
Read: bar.d - 1 7290.
Read: bat-smg - 967 9405911.
Read: bat-smg.d - 2 14576.
Read: bcl - 262 4076171.
Read: be - 4162 54603077.
Read: be-x-old - 3322 60275040.
Read: be-x-old.d - 2 14350.
Read: be.b - 35 636805.
Read: be.d - 36 317993.
Read: be.n - 2 14462.
Read: be.q - 19 149051.
Read: beta.v - 600 4360526.
Read: bg - 34459 574849813.
Read: bg.b - 197 3226803.
Read: bg.d - 5570 60366778.
Read: bg.n - 40 413897.
Read: bg.q - 560 4307046.
Read: bg.s - 64 863653.
Read: bh - 111 2191650.
Read: bh.d - 3 18855.
Read: bi - 100 2133903.
Read: bi.b - 4 34733.
Read: bi.d - 1 4575.
Read: bm - 90 1441605.
Read: bm.b - 9 25871.
Read: bm.d - 6 43046.
Read: bm.q - 5 26033.
Read: bn - 5509 77859066.
Read: bn.b - 10 150663.
Read: bn.d - 21 216131.
Read: bn.s - 113 1110252.
Read: bo - 226 4894572.
Read: bo.b - 14 222210.
Read: bo.d - 5 33491.
Read: bo.q - 1 7170.
Read: bpy - 2434 42007534.
Read: bpy.b - 1 7180.
Read: br - 4559 33243202.
Read: br.b - 1 7170.
Read: br.d - 297 2117972.
Read: br.q - 13 90504.
Read: bs - 10316 106662123.
Read: bs.b - 48 752610.
Read: bs.d - 136 1136090.
Read: bs.n - 121 987922.
Read: bs.q - 696 5332854.
Read: bs.s - 63 781599.
Read: bug - 84 1047713.
Read: bxr - 37 557388.
Read: ca - 28059 363534831.
Read: ca.b - 332 5455385.
Read: ca.d - 938 7722199.
Read: ca.n - 226 2408335.
Read: ca.q - 272 1806660.
Read: ca.s - 289 2114361.
Read: ca.v - 3 21510.
Read: cbk-zam - 307 7825246.
Read: cdo - 97 1299726.
Read: ce - 253 8919027.
Read: ceb - 2186 21116711.
Read: ceb.d - 3 21750.
Read: ch - 215 2847349.
Read: ch.b - 6 26667.
Read: ch.d - 3 23261.
Read: cho - 15 76754.
Read: cho.d - 1 1972.
Read: chr - 86 1428755.
Read: chr.d - 14 103309.
Read: chy - 19 183064.
Read: chy.d - 1 7170.
Read: ckb - 202 3931545.
Read: closed-zh-tw - 2 14301.
Read: co - 528 7662925.
Read: co.b - 38 2934634.
Read: co.d - 59 498919.
Read: co.q - 11 58046.
Read: commons - 354 270316.
Read: commons.m - 304277 270316.
Read: commons.m - 304277 270316.
Read: commons.m - 304277 270316.
Read: commons.m - 304277 270316.
Read: common^C

最佳答案

问题是:

  1. 您正在使用 inFile.eof(),这是错误的条件! eof() 的主要用途是,如果由于到达 EOF 而导致文件读取失败,它不会报告错误。
  2. 您不检查after 读取文件是否成功读取,似乎在某些时候失败了。当从流中读取失败时,std::ios_base::failbit 设置并阻止任何进一步的读取成功,直到该位被清除。

从文件中读取的正确方法是这样的:

while (inFile >> v0 >> v1 >> v2) {
    process(v0, v1, v2);
}

或者,如果您不能在一个语句中完成所有输入,您可以将它们拆分并在处理读取值之前检查循环条件中文件的状态:

while (inFile) {
    inFile >> v0 >> v1 >> v2;
    if (inFile) {
        process(v0, v1, v2);
    }
}

显然,您可以使用其他输入函数,例如,std::getline(),但您仍然需要检查after读取是否成功。

关于c++ - 为什么我的数据流在 20 或 30 行后开始重复?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12948777/

相关文章:

c - 在 C 中存储来自字符串的两个整数

SQL内插字符串

c++ - 在 windows 中运行 linux c++ 代码

c++ - 硬币找零 DP 算法打印所有组合

c++ - 在 OpenGL C++ 中绘制一个球和两个圆锥体

c++ - C++ string erase return *this 是什么意思?

c++ - 为什么我在 C++ 中用 ^ 计算指数时得不到正确的结果?

python - 索引和切片结构化 ndarrays

javascript - 数组包括

java - 在Spring Boot java项目中如何将不同数据类型的属性从mongo DB映射到java类?