473,230 Members | 1,458 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,230 software developers and data experts.

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 1906
"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Dario de Judicibus | last post by:
I wish to implement a simple C++ filter that reads from a file byte by byte and writes to an output files two bytes for each byte read according to some hardcoded table. I am looking for a very...
5
by: Rob Somers | last post by:
Hey all I am writing a program to keep track of expenses and so on - it is not a school project, I am learning C as a hobby - At any rate, I am new to structs and reading and writing to files,...
9
by: Arnold | last post by:
I need to read a binary file and store it into a buffer in memory (system has large amount of RAM, 2GB+) then pass it to a function. The function accepts input as 32 bit unsigned longs (DWORD). I...
6
by: KevinD | last post by:
assumption: I am new to C and old to COBOL I have been reading a lot (self teaching) but something is not sinking in with respect to reading a simple file - one record at a time. Using C, I am...
0
by: Daniel Sélen Secches | last post by:
I found a good class to do a simple FTP. Very good.... I'm posting it with the message, i hope it helps someone ============================================================== Imports...
8
by: Mark | last post by:
Hello. I am attempting to write binary data from a file to an OLE Object field, and then write the file back out from the database. I am reading and writing the files in binary mode, and using...
7
by: elliotng.ee | last post by:
I have a text file that contains a header 32-bit binary. For example, the text file could be: %%This is the input text %%test.txt Date: Tue Dec 26 14:03:35 2006...
1
by: yohan610 | last post by:
i have to read the binary data of a file, then encrypt them according to a supplied algorithm...and then the obtained output has to be written to an output file...everything works ok, and there are...
1
by: jerry | last post by:
i have written a simple phonebook program,i'll show you some of the codes,the program's head file is member.h . i suppose the head file works well.so i don't post it. here's the clips of main...
0
by: VivesProcSPL | last post by:
Obviously, one of the original purposes of SQL is to make data query processing easy. The language uses many English-like terms and syntax in an effort to make it easy to learn, particularly for...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.