File input / output in C (5)
Original: Ilia Yordanov, Loobian@cpp-home.com
Treatment of binary files
Although there is a format (formatted) text (all files I discussed so far) is very useful, but sometimes you need to use unformatted files - binaries. They look like your executables, and files created with the << and >> operator are greatly different. GET () function and put () function gives you the ability to read / write a rule of format file: To read a byte, you can use the get () function; use the PUT () function to write a byte. . You should recall GET () - I used it. You may puzzle why we use it when we use it, the file content that outputs to the screen looks a text format? Well, I guess this because I used << and >> operators before.
Translation: The author's so-called "formatted text" is the non-text of our usual text format, and relative "unformatted files" to store all kinds of data or executable code. Format file. Typically, the latter needs to read in memory, parsing in the binary level, and the former can be read / written directly from a predetermined << and >> operator (of course, the latter can also be properly reloaded <
Get () functions and each with a parameter: a CHAR type variable (the translation: refers to the get () function) or a character (the translation: refers to the PUT () function, of course this character can also be provided in a CHAR type variable).
If you want to read / write a whole piece of data, you can use the read () and write () functions. The prototypes are as follows:
iStream & Read (Char * BUF, Streamsize Num);
Ostream & Write (Const Char * BUF, Streamsize Num);
For the read () function, the BUF should be an array of characters, and the data read by the file will be saved here. For Write () functions, BUF is a character array that is used to store data you want to write to files. For these two functions, NUM is a number, which specifies the number of bytes you want to read / written from the file.
If you read the data, you have reached the end of the file before you read "NUM" bytes, then you can learn about the number of bytes actually read by calling the gcount () function. This function returns the number of bytes actually read on the read operation of the last time the last time.
Before giveing the sample code, I have to add that if you want to read / write the file in a binary method, you should add ios :: binary as the open mode to the file open parameter table.
Let me showcase sample code now, you will see how it works.
Example 1: Using GET () and PUT ()
#include
void main ()
{
FSTREAM File ("Test_File.txt", ios :: Out | iOS :: in | ios :: binary;
CHAR CH;
CH = 'o';
File.put (ch); // write the contents of the CH into the file
File.seekg (iOS :: beg); // Position to the head of the file
File.get (ch); // read a character cout << ch << Endl; // display it on the screen
File.Close ();
}
Example 2: Using Read () and Write ()
#include
#include
void main ()
{
FSTREAM File ("Test_File.txt", ios :: Out | iOS :: in | ios :: binary;
Char arr [13];
STRCPY (Arr, "Hello World!"); // Put Hello World!
File.write (Arr, 5); // Write the top 5 characters - "Hello"
File.seekg (iOS :: beg); // Position to the head of the file
Static Char Read_Array [10]; // I will intend to read some data here.
File.read (Read_Array, 3); // Read the top three characters - "Hel"
COUT << Read_Array << Endl; // outputs them
File.Close ();
}