C + + file stream basic usage (fstream, ifstream, Ostream)

Source: Internet
Author: User

Objective:
C + + file stream processing is actually very simple, if you can understand it. The essence of file flow is the use of a buffer middle layer. A bit like standard output and standard input.

The design of C + + IO guarantees IO efficiency while taking into account both encapsulation and ease of use. This article will describe the use of C + + file streams.

Where there are errors and omissions, you are welcome to criticize the evidence.

Header files to include: <fstream>

Namespaces: STD

You can also try <fstream.h>

FStream provides three classes for implementing C + + operations on files. (file creation, read-write).
Ifstream-read from an existing file

Ofstream-Write content to a file

FStream-Open file for read and write

Supported file types

In fact, file types can be divided into two kinds: text files and binary files.

A text file holds a readable character, while a binary file saves only binary data. Using binary mode, you can manipulate images and other files. In text mode, you can only read and write text files. Otherwise you will get an error.

Example one: writing a file

Declaring a Ofstream variable

    1. Call the Open method so that it is associated with a file
    2. Write a file
    3. Call the Close method.
    1. #include <fstream.h>
    2. void Main
    3. {
    4. Ofstream file;
    5. File. Open("file.txt");
    6. file<<"Hello file/n" <<;
    7. File. Close();
    8. }

You can try the operator << write content to the file like you tried cout.
Usages:

    1. file<<"string/n";
    2. File. put(' C ');

Example two: reading a file

1. Declare a ifstream variable.

2. Open the file.

3. Read data from a file

4. Close the file.

  1. #include <fstream.h>
  2. void Main
  3. {
  4. Ifstream file;
  5. Char output[+];
  6. int x;
  7. File. Open("file.txt");
  8. file>>output;
  9. cout<<output;
  10. file>>x;
  11. cout<<x;
  12. File. Close();
  13. Similarly, you can use >> to manipulate files like CIN. or call the member function usages:
    1. File>>char *;
    2. file>>char;
    3. File. Get(char);
    4. File. Get(char *,int);
    5. File. getline(char *,int sz);
    6. File. getline(char *,int sz,char eol);

1. Similarly, you can also use the constructor to open a file, you just take the file name as the constructor

The first parameter is available.

    1. Ofstream file("Fl.txt");
    2. Ifstream file("Fl.txt");

The Ofstream and Ifstream mentioned above can only be read or written, while FStream provides both read and write functions.
void main()

    1. {
    2. FStream file;
    3. File. Open("File.ext", ISO::in|ios::out)
    4. Do a input or output here
    5. File. Close();
    6. }

The arguments to the open function define the opening mode of the file. A total of the following patterns

    1. Property List
    2. iOS:: Inread
    3. iOS:: OutWrite
    4. iOS::app starts writing from the end of the file
    5. iOS::binary binary mode
    6. iOS::nocreate When you open a file, the file is not created if the file does not exist.
    7. iOS::noreplace When you open a file, if the file does not exist, create the file
    8. iOS::trunc Open a file and then empty the content
    9. iOS::ate when you open a file, move the location to the end of the file


Notes

    • The default mode is text
    • By default, if the file does not exist, create a new
    • Multiple modes can be mixed, with | (Bitwise OR)
    • The byte index of the file starts at 0. (just like an array)

We can also call the Read function and the Write function to read and write the file.

The use of the file pointer position in C + +:

    1. iOS::Beg file Header
    2. iOS:: EndFile End
    3. iOS::cur Current Location example:
    1. File. seekg(0,ios::end);
    2. int FL_SZ = file. Tellg();
    3. File. seekg(0,ios::beg);

Common methods of error determination:

    1. Good() If the file opens successfully
    2. Bad () An error occurred while opening the file
    3. EOF() arrives at the end of the file example:
  1. Char ch;
  2. Ifstream file("Kool.cpp", iOS::in|ios::out);
  3. If(file. Good()) cout<<"The file has been opened without problems;
  4. Else cout<< "An Error had happend on opening the file;
  5. While(! File. EOF())
  6. {
  7. file>>ch;
  8. cout<<ch;
  9. }

C + + file stream basic usage (fstream, ifstream, Ostream)

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.