The C++ standard library
is a header file in the C++ standard library that contains the C++ wrapper of the C standard I/O library, mainly used for file input and output operations.
The library defines a set of functions for performing input and output operations; these functions can be used to read and write files and the console.
Syntax
When using functions in the library, you need to include this header at the top of your C++ program:
#include
Common functions
The library contains many functions for file I/O; here are some commonly used ones:
fopen: Opens a file.fclose: Closes a file.fread: Reads data from a file.fwrite: Writes data to a file.fprintf: Writes formatted output to a file.fscanf: Reads formatted input from a file.fgetc: Reads a character from a file.fputc: Writes a character to a file.fgets: Reads a line from a file.fputs: Writes a line to a file.
Example
- Opening and closing files:
Example
#include
int
main
(
)
{
FILE
file
=
fopen
(
"example.txt"
,
"w"
)
;
// Open file for writing
if
(
file
==
NULL
)
{
perror
(
"Error opening file"
)
;
return
1
;
}
fclose
(
file
)
;
// Close file
return
0
;
}
- Writing data to a file:
Example
#include
int
main
(
)
{
FILE
file
=
fopen
(
"example.txt"
,
"w"
)
;
if
(
file
==
NULL
)
{
perror
(
"Error opening file"
)
;
return
1
;
}
fprintf
(
file,
"Hello, World!n"
)
;
fclose
(
file
)
;
return
0
;
}
wrote "Hello, World!" to the file "example.txt".
- Reading data from a file:
Example
#include
#include
int
main
(
)
{
FILE
file
=
fopen
(
"example.txt"
,
"r"
)
;
if
(
file
==
NULL
)
{
perror
(
"Error opening file"
)
;
return
1
;
}
char
buffer
[
100
]
;
while
(
fgets
(
buffer,
100
, file
)
!
=
NULL
)
{
std
::
cout
<<
buffer
;
}
fclose
(
file
)
;
return
0
;
}
read and output "Hello, World!" from the file "example.txt".
- Using fscanf and fprintf for formatted input/output:
Example
#include
int
main
(
)
{
FILE
file
=
fopen
(
"data.txt"
,
"w"
)
;
if
(
file
==
NULL
)
{
perror
(
"Error opening file"
)
;
return
1
;
}
fprintf
(
file,
"%d %fn"
,
42
,
3.14159
)
;
fclose
(
file
)
;
file
=
fopen
(
"data.txt"
,
"r"
)
;
if
(
file
==
NULL
)
{
perror
(
"Error opening file"
)
;
return
1
;
}
int
number
;
float
pi
;
fscanf
(
file,
"%d %f"
,
&
number,
&
pi
)
;
fclose
(
file
)
;
std
::
printf
(
"Number: %d, Pi: %fn"
, number, pi
)
;
return
0
;
}
Output result:Number: 42, Pi: 3.141590
fopen and fclose
are used to open and close files.
Example
#include
int
main
(
)
{
FILE
file
=
fopen
(
"example.txt"
,
"r"
)
;
if
(
file
)
{
// File operations
fclose
(
file
)
;
}
else
{
// Handle errors
}
return
0
;
}
fread and fwrite
are used to read and write data from and to files.
Example
#include
int
main
(
)
{
FILE
file
=
fopen
(
"example.bin"
,
"wb"
)
;
if
(
file
)
{
int
data
=
12345
;
fwrite
(
&
data,
sizeof
(
data
)
,
1
, file
)
;
fclose
(
file
)
;
}
return
0
;
}
fseek and ftell
are used to move the file pointer within a file and get its position.
Example
#include
int
main
(
)
{
FILE
file
=
fopen
(
"example.txt"
,
"r"
)
;
if
(
file
)
{
fseek
(
file,
0
,
SEEK_END
)
;
// Move to end of file
long
size
=
ftell
(
file
)
;
// Get file size
fclose
(
file
)
;
}
return
0
;
}
fflush
flushes the file stream, writing the buffered data to the file.
Example
#include
int
main
(
)
{
FILE
file
=
fopen
(
"example.txt"
,
"w"
)
;
if
(
file
)
{
fputs
(
"Hello, World!"
, file
)
;
fflush
(
file
)
;
// Ensure data is written to the file immediately
fclose
(
file
)
;
}
return
0
;
}
printf and fprintf
are used for formatted output to standard output or a file.
Example
#include
int
main
(
)
{
int
value
=
42
;
printf
(
"Value: %d\n"
, value
)
;
// Output to standard output
FILE
file
=
fopen
(
"output.txt"
,
"w"
)
;
if
(
file
)
{
fprintf
(
file,
"Value: %d\n"
, value
)
;
// Output to a file
fclose
(
file
)
;
}
return
0
;
}
scanf and fscanf
are used for formatted input from standard input or a file.
Example
#include
int
main
(
)
{
int
value
;
scanf
(
"%d"
,
&
value
)
;
// Read from standard input
FILE
file
=
fopen
(
"input.txt"
,
"r"
)
;
if
(
file
)
{
fscanf
(
file,
"%d"
,
&
value
)
;
// Read from a file
fclose
(
file
)
;
}
return
0
;
}
sprintf and sscanf
are used for formatted output to a string and reading from a string.
Example
#include
int
main
(
)
{
char
buffer
[
50
]
;
int
value
=
42
;
sprintf
(
buffer,
"Value: %d"
, value
)
;
// Output to a string
int
readValue
;
sscanf
(
buffer,
"Value: %d"
,
&
readValue
)
;
// Read from a string
return
0
;
}
fgets and fputs
are used to read strings from a file and write strings to a file.
Example
#include
int
main
(
)
{
FILE
file
=
fopen
(
"example.txt"
,
"r"
)
;
if
(
file
)
{
char
buffer
[
100
]
;
if
(
fgets
(
buffer,
sizeof
(
buffer
)
, file
)
)
{
// Read succeeded
}
fclose
(
file
)
;
}
file
=
fopen
(
"example.txt"
,
"w"
)
;
if
(
file
)
{
fputs
(
"Hello, World!\n"
, file
)
;
// Write string to file
fclose
(
file
)
;
}
return
0
;
}
getc and putc
are used to read characters from a file and write characters to a file.
Example
#include
int
main
(
)
{
FILE
file
=
fopen
(
"example.txt"
,
"r"
)
;
if
(
file
)
{
int
c
=
getc
(
file
)
;
// Read a character
fclose
(
file
)
;
}
file
=
fopen
(
"example.txt"
,
"w"
)
;
if
(
file
)
{
putc
(
'A'
, file
)
;
// Write a character
fclose
(
file
)
;
}
return
0
;
}
feof and ferror
are used to detect end-of-file and file errors.
Example
#include
int
main
(
)
{
FILE
file
=
fopen
(
"example.txt"
,
"r"
)
;
if
(
file
)
{
while
(
!
feof
(
file
)
)
{
int
c
=
getc
(
file
)
;
if
(
ferror
(
file
)
)
{
// Handle errors
break
;
}
// Process characters
}
fclose
(
file
)
;
}
return
0
;
}
EOF
represents the end-of-file indicator.
Example
#include
int
main
(
)
{
FILE
file
=
fopen
(
"example.txt"
,
"r"
)
;
if
(
file
)
{
int
c
;
while
(
(
c
=
getc
(
file
)
)
!
=
EOF
)
{
// Process characters
}
fclose
(
file
)
;
}
return
0
;
}
NULL
represents a null pointer.
Example
#include
int
main
(
)
{
FILE
file
=
fopen
(
"example.txt"
,
"r"
)
;
if
(
file
==
NULL
)
{
// Handle file open error
}
else
{
fclose
(
file
)
;
}
return
0
;
}
Notes
- Use
library, you need to ensure files are opened and closed properly to avoid resource leaks. - When using
fopen, you need to provide the correct file path and mode. - Use
fprintfandfscanf, the format string should match the variable types.