views:

95

answers:

4

I'm at the time beginning the development of a simple hex editor(that only reads at the time). I want to substitute OA for "\n", I'm trying with this code:

#include <iostream>
#include <fstream>
#include <iomanip>
using namespace std;

int main() {
   ifstream infile;
   int crtchar = (int)infile.get();
   infile.open("test.txt", ifstream::in);
   while(infile.good())
   {
      if(crtchar != 0xA)
         cout << hex << setfill('0') << setw(2) << crtchar << ":";
      else
         cout << endl;
   }
   cout << "\n=====================================\n";
   infile.close();
   return 0;
}

It compiles without errors, but when I try to execute it, I just got nothing:

C:\Documents and Settings\Nathan Campos\Desktop>hex

=====================================

C:\Documents and Settings\Nathan Campos\Desktop>

This is happening just after I've added the feature to substitute OA for \n, because before it was working very nice. What is wrong?

+7  A: 

You realize that you are only reading a character once, and before even opening the file, at that?

Konrad Rudolph
+3  A: 

Sigh. You try to read the file before you open it.

anon
A: 

Shouldn't you first open(...) your file and then try to get() from it? Also, shouldn't you do more get()'s inside your while loop

jilles de wit
A: 

Put int crtchar = (int)infile.get(); inside a while(infile.good()) and give it a try.

Dmitry