468,760 Members | 1,899 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,760 developers. It's quick & easy.

a simple C++ question (about binary file input)

i have a binary file "myfile.bin", its output of "od -t x1 myfile.bin" is:

0000000 7f 45 4c 46 01 02 01

and longer. i want the output to screen be:

7f 45 4c 46 01 02 01

i try to write the program like that:

char ch2;
ifstream ifs ("myfile.bin", ios:binary|ios::in);
while (ifs.get(ch2)) { *****
cout.put(ch2); *****
}
ifs.close(); return 0;

i think the two lines with ***** are problematic, could anyone gives me a
helping hand? thanks for reading this post.
Jul 22 '05 #1
2 1793
"news.hku.hk" <bi******@hkusua.hku.hk> wrote...
i have a binary file "myfile.bin", its output of "od -t x1 myfile.bin" is:

0000000 7f 45 4c 46 01 02 01

and longer. i want the output to screen be:

7f 45 4c 46 01 02 01

i try to write the program like that:

char ch2;
ifstream ifs ("myfile.bin", ios:binary|ios::in);
while (ifs.get(ch2)) { *****
cout.put(ch2); *****
You probably want to do

cout << int(ch2) << ' ';
}
ifs.close(); return 0;

i think the two lines with ***** are problematic, could anyone gives me a
helping hand? thanks for reading this post.


Victor
Jul 22 '05 #2

"news.hku.hk" <bi******@hkusua.hku.hk> wrote in message
news:3f********@newsgate.hku.hk...
i have a binary file "myfile.bin", its output of "od -t x1 myfile.bin" is:

0000000 7f 45 4c 46 01 02 01

and longer. i want the output to screen be:

7f 45 4c 46 01 02 01
These are hex integers not chars.
cat shows chars
od shows the hexadecimal integer values of those chars (using 2 chars per
char :-)

i try to write the program like that:
#include <iomanip> // for hex setfill and setw
char ch2;
ifstream ifs ("myfile.bin", ios:binary|ios::in);
cout << std::hex << std::setfill('0'); // use hex for
ints and padd with leading 0
while (ifs.get(ch2)) { *****
// each char is 2 hex digits and width isn't sticky so it
must be done for each
cout << ' ' << std::setw(2) << int(ch2);
}
cout << dec << setfill(' '); // set back to default - this isn't
ideal but proper saving and restoring of state is messy
ifs.close(); return 0;

i think the two lines with ***** are problematic, could anyone gives me a
helping hand? thanks for reading this post.

Jul 22 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

6 posts views Thread by Dario de Judicibus | last post: by
5 posts views Thread by Rob Somers | last post: by
6 posts views Thread by KevinD | last post: by
reply views Thread by Daniel Sélen Secches | last post: by
7 posts views Thread by elliotng.ee | last post: by
1 post views Thread by jerry | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by Marin | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.