0

I am working on an assignment "Text file compression" using Bitwise operators. I dunno why it isn't reading the whole file(compressed text file). Whereas simple text file is read as fine why is that?

int main()
{
    ifstream inFile;
    inFile.open("Compressed.txt");
    int counter = 0;
    inFile.seekg (0, inFile.end);
    int length = inFile.tellg();
    inFile.seekg (0, inFile.beg);
    cout << "The real file size:  " << length << "\n\n\n";

    while(!inFile.eof())
    {
        char ch;
        inFile.get(ch);
        counter ++;
    }
    inFile.close();
    cout << endl <<"Number of characters read before eof:  "<< counter;

    counter = 0;
    inFile.open("Compressed.txt", ios_base::binary);
    while(!inFile.eof())
    {
        char ch;
        inFile.get(ch);
        counter ++;
    }
    inFile.close();
    cout << endl <<"Number of characters read before eof as a binary stream:  "<< counter << endl << endl;
}
François Andrieux
  • 24,129
  • 6
  • 46
  • 72
  • 1
    You have to add more information. What have you tried, what behaviour do you expect? – OutOfBound Oct 18 '17 at 18:05
  • https://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-wrong – melpomene Oct 18 '17 at 18:07
  • after compression when I read that file for decompressing couple of characters get missed –  Oct 18 '17 at 18:11
  • As noted by melpomene, asking ``eof`` doesn't do the expected behavior. I would use ``peek`` to make sure the ``eof`` flag is valid. ``while((inFile.peek(),!inFile.eof()))`` (The comma operator allows us to peak in the condition without using the returned value.) – unDeadHerbs Oct 18 '17 at 18:15

0 Answers0