Thread: strange file opening problem

  1. #1
    Registered User
    Join Date
    May 2006
    Posts
    89

    strange file opening problem

    I am trying to use a file stream to count some data. The odd thing is the second time I use the ifstream, the file opens but it doesnt seem to read any data at all. Here is the code from the working portion:

    Code:
    ifstream infile;
    ofstream outfile;
    infile.open("08_28_06.txt");
    
    while(getline(infile, line)){
             string cat = line.substr(48, 3);
            if(cat == "HHD" || cat == "TLT")
               hhd++;
            else if(cat == "BAK")
                 bak++;
            else if(cat == "BEV")
                 bev++;
            else if(cat == "BXB")
                 bxb++;
            else if(cat == "CND")
                 cnd++;
            else if(cat == "CNJ")
                 cnj++;
            else if(cat == "CON")
                 con++;
            else if(cat == "HAB" || cat == "MED")
                 haba++;
            else if(cat == "RFR")
                 rfr++;
            else if(cat == "SNK")
                 snk++;
            else if(cat == "STA")
                 sta++;
            else if(cat == "SNA")
                 sna++;
            else
                misc++;
        }
        infile.close();
    And here is the non-working part:
    Code:
    line = "";
        infile.open("NEW_BAK.csv");
        bak = 0;
        while(getline(infile, line)){
            if(isdigit(line[0]))
             bak++;
        }
        cout << "BAK: " << bak << endl;
        infile.close();
    I tried putting the statement: if(infile.is_open()) cout << "open"; before the loop and it works, so the file is opened... but it never enters the loop and there IS stuff in the file.

    thanks

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    Closing a stream does not reset the error flags. In this case, eofbit remains set despite closing the file and opening another with the same stream object. Try this:
    Code:
    infile.clear();
    infile.open("NEW_BAK.csv");
    My best code is written with the delete key.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    infile.clear();
    IIRC, the end of file doesn't go away when you just close the file and reopen it.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User
    Join Date
    May 2006
    Posts
    89
    thanks! I didn't know that and was completely stumped.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. problem in opening a txt file
    By arian in forum C++ Programming
    Replies: 3
    Last Post: 06-07-2009, 01:17 PM
  2. File Writing Problem
    By polskash in forum C Programming
    Replies: 3
    Last Post: 02-13-2009, 10:47 AM
  3. gcc link external library
    By spank in forum C Programming
    Replies: 6
    Last Post: 08-08-2007, 03:44 PM
  4. Simple File encryption
    By caroundw5h in forum C Programming
    Replies: 2
    Last Post: 10-13-2004, 10:51 PM
  5. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 04:10 PM