Important alert: (current site time 7/15/2013 6:26:31 PM EDT)
 

winzip icon

C++ Binary File I/O Example

Email
Submitted on: 4/8/2003 6:12:43 PM
By: *LuckY* 
Level: Beginner
User Rating: By 15 Users
Compatibility: C++ (general), Microsoft Visual C++, Borland C++, UNIX C++
Views: 72351
(About the author)
 
     Most people are familiar with reading/writing text to/from a disk file. This is a quick little example for beginners detailing how to go about doing the same with binary data. This will allow you to make better use of files. You will be able to save/load structures in files and other non-human-readable data. It illustrates use of the ios_base::binary flags and the fstream functions read() and write() (as opposed to operator>>() and operator<<()). It's a short little program, but very thoroughly commented. Hope this helps somebody as much as my "C++ File IO Examples" submission did!
 
winzip iconDownload code

Note: Due to the size or complexity of this submission, the author has submitted it as a .zip file to shorten your download time. Afterdownloading it, you will need a program like Winzip to decompress it.Virus note:All files are scanned once-a-day by Planet Source Code for viruses, but new viruses come out every day, so no prevention program can catch 100% of them. For your own safety, please:
  1. Re-scan downloaded files using your personal virus checker before using it.
  2. NEVER, EVER run compiled files (.exe's, .ocx's, .dll's etc.)--only run source code.

If you don't have a virus scanner, you can get one at many places on the net including:McAfee.com

 
Terms of Agreement:   
By using this code, you agree to the following terms...   
  1. You may use this code in your own programs (and may compile it into a program and distribute it in compiled format for languages that allow it) freely and with no charge.
  2. You MAY NOT redistribute this code (for example to a web site) without written permission from the original author. Failure to do so is a violation of copyright laws.   
  3. You may link to this code from another website, but ONLY if it is not wrapped in a frame. 
  4. You will abide by any additional copyright restrictions which the author may have placed in the code or code's description.


Other 23 submission(s) by this author

 


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this code (in the Beginner category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments
4/9/2003 4:32:17 AM

nice code,but i need to know how to read binary data from a file and then write the same data into another.with using the get and put method etc... So that i can basicly open a file,read the binary data,close the file.Open another file,write the binary data from file one into it and then close it.
if you happen to know how to do this or have an idea => barcaboy_66@hotmail.com.

in the end: nice code indeed!
(If this comment was disrespectful, please report it.)

 
4/9/2003 9:56:45 AM*LuckY*

Now are you saying that you _must_ use get() and put() instead of read() and write()? I'll tell you both ways; they are pretty straight forward.
You would just use an ifstream object and specify the source file, then use an ofstream object specifying the destination filename. How you read/write the data is up to you. Using read() and write() you can specify the number of bytes to process. Using get() and put() you process one byte at a time. Example (assuming you know the size you're dealing with):
for (int i = 0; i < THE_SIZE; ++i) {
fin.get(ch);// read a byte
fout.put(ch);// write a byte
}

if you wanted to copy the entire contents, you could use "while (fin.good()) {" instead of the for() statement.
(If this comment was disrespectful, please report it.)

 
4/9/2003 11:48:34 AM

how can u find the size,because the file would be user-defined so i'll need to find the size and then do the code

how can i do this?

thx anyway for the comment
(If this comment was disrespectful, please report it.)

 
4/9/2003 11:56:30 AM*LuckY*

Quoting my previous comment:
"if you wanted to copy the
entire contents, you could use "while
(fin.good()) {" instead of the for()
statement."

You don't need to know the size to copy the entire file. Just keep going "while (fin.good())"

In the event that you do want to know the size of the file for any reason, you can fin.seekg(0, ios_base::end) to move to the end of the file then fin.tellg() to retrieve the number of bytes from start to end.

Side note: in the loop, after fin.get(ch) you might want to check if fin.good() before doing the fout.put(ch). Try
if (fin.get(ch).good())
fout.put(ch);
(If this comment was disrespectful, please report it.)

 
6/14/2003 2:44:33 PMEric Malamisura

Instead of specifying multiple namespaces why dont you just use using namespace std; I dont understand why I see developer after developer specify direct namespaces...
(If this comment was disrespectful, please report it.)

 
6/15/2003 4:28:58 PM*LuckY*

Well Eric, it may be as simple as that it is each developer's preference. In my particular case I tend to make it a habit not to bring any namespace into scope in its entirety. This is because it brings a lot of function and variable names into global scope and that may result in name clashes. Unless you plan on using everything in the namespace there's no reason for such overkill (except laziness). In this particular program there are only 4 elements in namespace std utilized. If you see developer after developer doing something, it should give you some clue that there may be a reason for it. Now do you understand?
(If this comment was disrespectful, please report it.)

 
6/15/2003 5:08:07 PMEric Malamisura

Yeah I got it lucky. I guess I am just lazy :P
(If this comment was disrespectful, please report it.)

 
8/8/2003 2:08:35 AM

I check the file I/O for my homework since its the only one i struggle with out of 10 homeworks. Its what i need to work on is struct with bool function but one problem i try to use the cout and cin inside the bool function since my teacher doesnt want any cout or cin inside the main function. i keep work on it iwth bool and struct.. its keep fell apart. i dont know what wrong with it. when it finally can running. its bark the dos window and disappear... any idea ?
(If this comment was disrespectful, please report it.)

 
10/24/2003 6:27:57 PM

I am looking for file I/O search in VC ++ looking for the sorce code
(If this comment was disrespectful, please report it.)

 
9/19/2004 11:40:35 PM

hi, the code is very nicely done ^^

I wonder if there's a way to make it search for a line then print it on the screen?

like if a user input a number for the salvary, is there a way for a user to search and make the salvary input on the screen?
(If this comment was disrespectful, please report it.)

 
11/22/2006 12:07:13 PMkronso.23

Awesome code, 5 globes from me

to Eric: also, using an entire namespace creates a lot of overhead, and longer compile times =/
(If this comment was disrespectful, please report it.)

 
6/25/2008 4:51:08 PMMark

Excellent work indeed!! Thanks for sharing your knowledge. Cheers! :)
(If this comment was disrespectful, please report it.)

 
1/16/2010 8:20:07 PMlaxmi nair

hiii,i was looking for a visual c++ code to read a given p01 file and write it to another file,can u plllzz help me
(If this comment was disrespectful, please report it.)

 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular code, please click here instead.)
 

To post feedback, first please login.