Basic usage of c ++ file stream fstream and ifstream, fstreamifstream

Source: Internet
Author: User

Basic usage of c ++ file stream fstream and ifstream, fstreamifstream

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 files to be included:

Namespace: std

You can also try it out.

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

  1. Call the open Method to associate it with a file
  2. Write files
  3. Call the close method.
    #include
       
        #include
        
         using namespace std;int main(){ofstream file;file.open("file.txt");file<<"Hello file/n"<<75;file<<"string/n";file.put('c');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 File 1. Declare An ifstream variable. 2. Open the file. 3. read data from the file. 4. close the file.
    # Include
       
        
    # Include
        
         
    Using namespace std; int main () {ifstream file; char output [100]; char x; file. open ("file.txt", ios: in | ios: out); // file> output; // cout <
         
          
    > X; cout <
          
           
    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",ios::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: app write from the end of the file ios: binary mode ios: nocreate when opening a file, if the file does not exist, do not create files. Ios: noreplace: When opening a file, if the file does not exist, create the file ios: trunc to open a file, and then 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: cur current location example:
      1. File. seekg (0, ios: end );
      2.  
      3. Int fl_sz = file. tellg ();
      4.  
      5. File. seekg (0, ios: beg );
        # Include
                    
                     
        # Include
                     
                      
        Using namespace std; int main () {char ch; ifstream file ("kool. cpp ", ios: in | ios: out); if (file. good () {cout <"The file has been opened without problem";} else cout <"An error has happened on opening the file"; while (! File. eof () // output {file> ch; cout <
                      
                       

        Common error determination methods:

        
                       
                
        1. Good () if the file is successfully opened

      6. An error occurred while opening the file in bad ().
      7. Example of eof () arriving at the end of a file:

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.