Turn your Mobile Apps into m-commerce apps – Learn More!

You're Browsing As A Guest! Register Now...
Become a C++ Expert!

Join 416,733 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 2,924 people online right now.Registration is fast and FREE... Join Now!



reading output in hex format Rate Topic: -----

#1 tootypegs  Icon User is offline

  • D.I.C Head
  • PipPip

Reputation: 1
  • View blog
  • Posts: 198
  • Joined: 09-October 07


Dream Kudos: 0

Share |

reading output in hex format

Posted 11 November 2007 - 04:30 AM

hi, my scenario is as follows:

I have two files, input and target. I would like to read the input file and output its data in hexidecimal format into the target file. I have the following code but it just reads the file exactly as it is from one to the other, not creating any conversions?...could anyone point me in the right direction


using namespace std;
   char ch;

   ifstream source_file("c:\\bollock\\mine.txt", ios::in |ios::out|ios::binary);

   ofstream target_file("c:\\bollock\\target.txt");

   while ((ch = source_file.get()) !=EOF)
   {
   cout << hex << source_file << endl;
   target_file.put(ch);
	}
   source_file.close();
   target_file.close();



Was This Post Helpful? 0
  • +
  • -


#2 jjhaag  Icon User is offline

  • me editor am smartastic
  • Icon

Reputation: 17
  • View blog
  • Posts: 1,789
  • Joined: 18-September 07


Dream Kudos: 775

Expert In: C,C++

Re: reading output in hex format

Posted 11 November 2007 - 12:23 PM

You shouldn't be outputting the source_file variable with the cout statement; that should probably be the ch variable that you output in that step.

As for the output begin identical to the input, that would be expected from this - you're just making a character by character copy, with no format conversion. You read in a character, and output the hex equivalent to the screen, but then you just output the same character with no format conversion.

Since you're trying to output a formatted character to a file stream, you can use the << operator again, rather than puts(). And this allows you to use the hex format specifier much as you would with the cout output stream object. The pertinent line of the code in your loop would be:
target_file << hex << int(ch) << " ";


Play around with it, see how it goes, and let us know.

Also, you should probably include the full source code rather than just a snippet from the middle of your code - many problems we see here are a result of the #include directives, global variables, namespaces, etc., and those are tough to pick up on when you don't have the full code available.

Hope that helps,

-jjh
Was This Post Helpful? 0
  • +
  • -

#3 realNoName  Icon User is offline

  • D.I.C Regular
  • PipPipPip

Reputation: 7
  • View blog
  • Posts: 343
  • Joined: 04-December 06


Dream Kudos: 0

Re: reading output in hex format

Posted 11 November 2007 - 12:37 PM

Just adding to what jjhaag said... i would move the target_file << hex to out side of the loop just because you dont need to keep setting the hex flag once its set

while ((ch = source_file.get()) !=EOF)
{
	target_file << hex << int(ch) << " ";
}

to
target_file << hex;
while ((ch = source_file.get()) !=EOF)
{
	target_file << int(ch) << " ";
}



as just a little side note if you want to show that 0x that is put with hex numbers you can add showbase
target_file << hex << showbase;

This post has been edited by realNoName: 11 November 2007 - 12:38 PM

Was This Post Helpful? 0
  • +
  • -

#4 tootypegs  Icon User is offline

  • D.I.C Head
  • PipPip

Reputation: 1
  • View blog
  • Posts: 198
  • Joined: 09-October 07


Dream Kudos: 0

Re: reading output in hex format

Posted 17 November 2007 - 05:12 AM

Sorry for the late reply, just wanted to drop in and say THANKS!!

...your adaptations to my code have worked a treat! Very much appreciated the quick and knowledgeable replies!

Cheers :^: :^: :^: :D
Was This Post Helpful? 0
  • +
  • -



Fast Reply

  

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users