开发者

Substituting 0A For \n

开发者 https://www.devze.com 2022-12-15 10:00 出处:网络
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:

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 w开发者_StackOverflowas working very nice. What is wrong?


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


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


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


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

0

精彩评论

暂无评论...
验证码 换一张
取 消