The processing of C ++ file streams is actually very simple, provided that you can understand it. In essence, a file stream uses a buffer intermediate layer. A bit similar to standard output and standard input.
The Design of C ++ Io ensures Io efficiency while taking into account both encapsulation and ease of use. This article describes how to use the C ++ file stream.
Where there are errors and omissions, you are welcome to criticize and testify.
Header file to be included: <fstream>
Namespace: std
You can also try <fstream. h>
Fstream provides three classes for C ++ to perform file operations. (File Creation, read/write ).
Ifstream -- read from an existing file
Ofstream -- write content to a file
Fstream-open a file for reading and writing
Supported file types
In fact, there are two types of files: text files and binary files.
Text Files store readable characters, while binary files only store binary data. In binary mode, you can operate images and other files. In text mode, you can only read and write text files. Otherwise, an error is reported.
Example 1: Write a file
Declare An ostream variable
Call the Open Method to associate it with a file
Write files
Call the close method.
# Include <fstream. h>
Void main
{
Ofstream file;
File. Open ("file.txt ");
File <"Hello file/N" <75;
File. Close ();
}
You can try the operator <write content to a file as you try cout.
Usages:
File <"String/N ";
File. Put ('C ');
Example 2: read an object
1. Declare An ifstream variable.
2. open the file.
3. read data from a file
4. close the file.
# Include <fstream. h>
Void main
{
Ifstream file;
Char output [100];
Int X;
File. Open ("file.txt ");
File> output;
Cout <output;
File> X;
Cout <X;
File. Close ();
}
Similarly, you can use> to operate files like Cin. Or call a member function.
Usages:
File> char *;
File> char;
File. Get (char );
File. Get (char *, INT );
File. Getline (char *, int sz );
File. Getline (char *, int SZ, char EOL );
1. Similarly, you can use the constructor to open a file. You only need to use the file name as the constructor's
The first parameter is enough.
Ofstream file ("fl.txt ");
Ifstream file ("fl.txt ");
The ofstream and ifstream mentioned above can only be read or written, while fstream also provides the read/write function.
Void main ()
{
Fstream file;
File. Open ("file. Ext", ISO: In | IOs: Out)
// Do an input or output here
File. Close ();
}
The open function parameter defines the open mode of the file. There are a total of the following modes:
Attribute list
IOS: In read
IOS: Out write
IOS: The app starts to write at the end of the file.
IOS: Binary binary mode
IOS: nocreate when opening a file, if the file does not exist, no file is created.
IOS: noreplace: When opening a file, if the file does not exist, create the file
IOS: trunc open a file and clear the content
IOS: ate when opening a file, move the location to the end of the file
Notes
The default mode is text.
If the file does not exist by default, a new
Multiple modes can be mixed. | (by bit or)
The Byte index of the file starts from 0. (Just like an array)
We can also call the Read and Write Functions to read and write files.
Usage of file pointer position in C ++:
IOS: Beg File Header
IOS: End file tail
IOS: current location of cur
Example:
File. seekg (
= "Nu0"> 0, IOS: End );
Int fl_sz = file. tellg ();
File. seekg (0, IOS: Beg );
Common error determination methods:
Good () if the file is successfully opened
An error occurred while opening the file in bad ().
EOF () arrives at the end of the file
Example:
Char ch;
Ifstream file ("Kool. cpp", IOS: In | IOs: Out );
If (file. Good () cout <"the file has been opened without problems;
Else cout <"an error has happend on opening the file;
While (! File. EOF ())
{
File> CH;
Cout <ch;
}
The following are common operations for reading data files:
# Include <iostream>
# Include <fstream>
# Include <string>
Using namespace STD;
// Empty output line
Void outputanemptyline ()
{
Cout <"/N ";
}
// Read method: Read by word, separated by Spaces
// Read data from the file,
WORD
BY
WORD
// When used in this manner, we'll get space-delimited bits of text from the file
// But all of the whitespace that separated words (including newlines) was lost.
Void readdatafromfilewbw ()
{
Ifstream fin ("data.txt ");
String S;
While (Fin> S)
{
Cout <"read from file:" <S <Endl;
}
}
// Read method: Read rows by row, read rows into character arrays, and use carriage return to line feed to distinguish between rows
// If we were interested in preserving whitespace,
// We cocould read the file in
LINE-
BY-
LINE using the I/O Getline () function.
Void readdatafromfilelblintochararray ()
{
Ifstream fin ("data.txt ");
Const int line_length = 100;
Char STR [line_length];
While (Fin. Getline (STR, line_length ))
{
Cout <"read from file:" <STR <Endl;
}
}
// Read method: Read rows by line, read rows into strings, and use carriage return to line feed to distinguish between rows
// If you want to avoid reading into character arrays,
// You can use the C ++ string Getline () function to read lines into strings
Void readdatafromfilelblintostring ()
{
Ifstream fin ("data.txt ");
String S;
While (Getline (FIN, S ))
{
Cout <"read from file:" <S <Endl;
}
}
// Read method with Error Detection
// Simply evaluating an I/O object in a Boolean context will return false
// If any errors have occurred
Void readdatawitherrchecking ()
{
String filename = "datafunny.txt ";
Ifstream fin (filename. c_str ());
If (! Fin)
{
Cout <"error opening" <FILENAME <"for input" <Endl;
Exit (-1 );
}
}
Int main ()
{
Readdatafromfilewbw (); // read strings one by one
Outputanemptyline (); // empty output line
Readdatafromfilelblintochararray (); // read the character array one by one
Outputanemptyline (); // empty output line
Readdatafromfilelblintostring (); // read strings one by one
Outputanemptyline (); // empty output line
Readdatawitherrchecking (); // read with detection
Return 0;
} Original post address: http://www.cnblogs.com/kevin2010_vip/archive/2010/02/03/1662853.html