Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8703035
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T02:47:25+00:00 2026-06-13T02:47:25+00:00

Possible Duplicate: .eof() loop not working So I have a fairly simple C++ program

  • 0

Possible Duplicate:
.eof() loop not working

So I have a fairly simple C++ program that I’m trying to get to read text files with a few columns of data, and reformat those lines and output them into a different text file. (The reformatting basically just involves taking certain lines, leaving out others, and appending a bit of extra data to the end of each line). It seems to work perfectly well, but on several different data files it seems to get hung up on one line in particular and start outputting that one line indefinitely- I have to manually stop the process. I think it may have to do with the way that the ‘fileSize’ variable is stored, (if you look at the output, you can see that the fileSize that is repeated is not correct for that line, and that it corresponds with the line before it started repeating). Other than that I have no idea what to look for or fix, and I’ve been wrestling with this issue for several days. I’ll include the code and the output of running it. Any ideas or suggestions would be very much appreciated.

Code:

#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;
}

… And the output from running “./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 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T02:47:26+00:00Added an answer on June 13, 2026 at 2:47 am

    The problems are:

    1. You are using inFile.eof() which is the wrong condition! The main use of eof() it not report an error if the failure to read from a file was due to reaching EOF.
    2. You don’t check after reading if reading the file was successful and it seems it failed at some point. When reading from a stream fails, std::ios_base::failbit gets sets and prevents any further read from succeeding until this bit is cleared.

    The correct way to read from a file looks something like this:

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

    Alternatively, if you can’t do all the inputs in one statement, you can split them and just check the status of the file in the loop’s condition and before processing the read values:

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

    Obviously, you may use other input functions, e.g., std::getline() but you still need to check after reading that reading was successful.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: fgetc does not identify EOF fgetc, checking EOF I have created a
Possible Duplicate: Trying to get tables next to each other horizontal I have two
Possible Duplicate: PHP EOF shows only one result from loop Hello Seems like I
Possible Duplicate: regex for URL including query string I have a text or message.
Possible Duplicate: Why is iostream::eof inside a loop condition considered wrong? So I've been
Possible Duplicate: EndOfFile in C - EOF I was trying examples from K&R. I
Possible Duplicate: Testing stream.good() or !stream.eof() reads last line twice I'm working on an
Possible Duplicate: Why is iostream::eof inside a loop condition considered wrong? im having a
Possible Duplicate: Reading from text file until EOF repeats last line c++ EOF running
Possible Duplicate: Realloc is not resizing array of pointers Can anyone tell me where

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.