Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

C++ file input/output library –

C++ file I/O library -

In C++, is a class in the standard library for file input/output operations. It provides a convenient way to read and write files.

fstreamIsiostreampart of the library, supporting reading and writing of text and binary files.

fstreamThe class isiostreama class in the library; it inherits fromistreamandostreamclass, which means it can be used for both input and output.

Syntax

fstream The basic syntax is as follows:

#include

int

main

(

)

{

    std

::

fstream

file

;

// Create an fstream object

    file.

open

(

"filename"

, mode

)

;

// Open the file

   

// Perform file operations

    file.

close

(

)

;

// Close file

   

return

0

;

}

wherefilename is the file name,mode is the file open mode; common modes include:

Example

Writing a text file:

Example

#include

#include

int

main

(

)

{

    std

::

fstream

file

;

    file.

open

(

"example.txt"

, std

::

ios

::

out

)

;

// Open file in output mode

   

if

(

!

file

)

{

        std

::

cerr

<<

"Unable to open file!"

<<

std

::

endl

;

       

return

1

;

// File failed to open

   

}

    file

<<

"Hello, World!"

<<

std

::

endl

;

// Write text

    file.

close

(

)

;

// Close file

   

return

0

;

}

creates a file namedexample.txtin the current directory, with the content:

Hello, World!

Reading a text file

Example

#include

#include

#include

int

main

(

)

{

    std

::

fstream

file

;

    file.

open

(

"example.txt"

, std

::

ios

::

in

)

;

// Open file in input mode

   

if

(

!

file

)

{

        std

::

cerr

<<

"Unable to open file!"

<<

std

::

endl

;

       

return

1

;

// File failed to open

   

}

    std

::

string

line

;

   

while

(

getline

(

file, line

)

)

{

// Read line by line

        std

::

cout

<<

line

<<

std

::

endl

;

   

}

    file.

close

(

)

;

// Close file

   

return

0

;

}

If both the example.txt The file contains the following content:

Hello, World!

This is a test file.

then the program will output:

Hello, World!

This is a test file.

Appending to a file:

Example

#include

#include

int

main

(

)

{

    std

::

fstream

file

;

    file.

open

(

"example.txt"

, std

::

ios

::

app

)

;

// Open file in append mode

   

if

(

!

file

)

{

        std

::

cerr

<<

"Unable to open file!"

<<

std

::

endl

;

       

return

1

;

// File failed to open

   

}

    file

<<

"Appending this line to the file."

<<

std

::

endl

;

// Append text

    file.

close

(

)

;

// Close file

   

return

0

;

}

example.txt The file originally contains the following content:

Hello, World!

This is a test file.

After executing the program above, the file content will become:

Hello, World!

This is a test file.

Appending this line to the file.