parsing fie, bonus char

Discussion in 'OT Technology' started by HY, Dec 1, 2008.

  1. HY

    HY New Member

    Joined:
    Dec 10, 2006
    Messages:
    57
    Likes Received:
    0
    im trying to read in a file to do some encryption on and i am getting some extra character at the end
    this is the original file
    0000000: 6162 6162 6162 6162 0a6a 666b 646a 666b abababab.jfkdjfk
    0000010: 640a
    and this is the output i am getting
    0000000: 6162 6162 6162 6162 0a6a 666b 646a 666b abababab.jfkdjfk
    0000010: 640a ff

    #include <iostream>
    #include <fstream>

    using namespace std;

    int main()
    {

    char ch = ' ';
    ifstream fin;
    fin.open("test1.txt");
    while (!fin.eof())
    {
    ch=fin.get();
    cout<< ch;
    }
    fin.close();
    return 0;
    }
     
  2. 5Gen_Prelude

    5Gen_Prelude There might not be an "I" in the word "Team", but

    Joined:
    Mar 14, 2000
    Messages:
    14,519
    Likes Received:
    1
    Location:
    Vancouver, BC, CANADA
    I could be wrong, but I think the eof flag only gets set once you position it past the end of file. So it's reading 0a which is at the end of the file, but not setting the flag because you haven't asked for something past the end of the file yet.
     
  3. HY

    HY New Member

    Joined:
    Dec 10, 2006
    Messages:
    57
    Likes Received:
    0
    well can you think of any way of not processing that without specifically stripping it? instead of eof() i tried good() too. iirc this is the way
     
  4. skinjob

    skinjob Active Member

    Joined:
    Jan 6, 2001
    Messages:
    2,337
    Likes Received:
    0
    Location:
    Aztlán
    Instead of checking just for eof, check whether the stream is still good.

    while(fin.good()) ...
     
  5. EkriirkE

    EkriirkE Zika Xenu OT Supporter

    Joined:
    Jan 11, 2004
    Messages:
    14,799
    Likes Received:
    0
    Location:
    Dublin & San Francisco, CA
    int main()
    {

    char ch = ' ';
    ifstream fin;
    fin.open("test1.txt");
    ch=fin.get();
    while (!fin.eof())
    {
    cout<< ch;
    ch=fin.get();
    }
    fin.close();
    return 0;
    }
     

Share This Page