Skip to content

File Operations

Concept of Files

A file is a collection of data gathered for a specific purpose. C/C++ treats each file as an ordered byte stream. Each file ends with an End-of-File (EOF) marker. To operate on a file, a program must first open it. Whenever a file is opened (remember to close it afterward), it is associated with a stream, which is essentially a sequence of bytes.

C/C++ classifies files into text files and binary files. Text files are simple text files (the main focus), while binary files include special format files or executable code files, etc.

Steps for File Operations

  1. Open the file, point the file pointer to the file, and decide the type of file to open.
  2. Perform read or write operations on the file (the main operations used in competitions; other operations are omitted here).
  3. Close the file after use.

freopen Function

Function Overview

This function redirects a specified input/output stream to a file in a specified mode. It is included in the header file stdio.h (cstdio). It can change the input/output environment without altering the original code, but ensure the stream is reliable when using it.

The function mainly supports three modes: read, write, and append.

Syntax

1
FILE* freopen(const char* filename, const char* mode, FILE* stream);

Parameter Description

  • filename: The name of the file to open.
  • mode: The mode for opening the file, indicating file access permissions.
  • stream: The file pointer, typically using standard file streams (stdin/stdout) or the standard error output stream (stderr).
  • Return value: A file pointer pointing to the opened file.

File Opening Modes (Optional Reading)

  • r: Open file for reading only. The file must exist, and only reading data is allowed. (Commonly used)
  • r+: Open file for reading and writing. The file must exist, and both reading and writing data are allowed.
  • rb: Open binary file for reading only. The file must exist, and only reading data is allowed.
  • rb+: Open binary file for reading and writing. The file must exist, and both reading and writing data are allowed.
  • rt+: Open text file for reading and writing. Both reading and writing data are allowed.
  • w: Open file for writing only. If the file does not exist, it will be created; otherwise, its contents will be cleared. Only writing data is allowed. (Commonly used)
  • w+: Open file for reading and writing. If the file does not exist, it will be created; otherwise, its contents will be cleared. Both reading and writing data are allowed.
  • wb: Open binary file for writing only. If the file does not exist, it will be created; otherwise, its contents will be cleared. Only writing data is allowed.
  • wb+: Open binary file for reading and writing. If the file does not exist, it will be created; otherwise, its contents will be cleared. Both reading and writing data are allowed.
  • a: Open file for writing only. If the file does not exist, it will be created. Written data will be appended to the end of the file (EOF marker is preserved).
  • a+: Open file for reading and writing. If the file does not exist, it will be created. Written data will be appended to the end of the file (EOF marker is not preserved).
  • at+: Open text file for reading and writing. Written data will be appended to the end of the file.
  • ab+: Open binary file for reading and writing. Written data will be appended to the end of the file.

Usage

Read file content:

1
2
freopen("data.in", "r", stdin);
// data.in is the filename to read from, placed in the same directory as the executable.

Output to a file:

1
2
freopen("data.out", "w", stdout);
// data.out is the output filename, placed in the same directory as the executable.

Close standard input/output streams:

1
2
fclose(stdin);
fclose(stdout);

Note

Functions like printf/scanf/cin/cout default to using stdin/stdout. After redirecting stdin/stdout, these functions will input/output to the redirected file.

Template

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
#include <cstdio>
#include <iostream>

int main(void) {
  freopen("data.in", "r", stdin);
  freopen("data.out", "w", stdout);
  /*
  The code in between does not need to change; simply use cin and cout as usual.
  */
  fclose(stdin);
  fclose(stdout);
  return 0;
}

fopen Function (Optional Reading)

This function is largely similar to freopen. It opens the specified file and returns a pointer to the opened file.

Function Prototype

1
FILE* fopen(const char* path, const char* mode)

The parameters have the same meanings as in freopen.

Available Read/Write Functions (Basic)

  • fread/fwrite
  • fgetc/fputc
  • fscanf/fprintf
  • fgets/fputs

Usage

1
2
3
4
5
6
7
8
FILE *in, *out;  // Define file pointers
in = fopen("data.in", "r");
out = fopen("data.out", "w");
/*
Do what you want to do
*/
fclose(in);
fclose(out);

C++ ifstream/ofstream File Input/Output Streams

Usage

Read file content:

1
2
ifstream fin("data.in");
// data.in is the relative or absolute path of the file to read.

Output to a file:

1
2
ofstream fout("data.out");
// data.out is the relative or absolute path of the output file.

Close standard input/output streams:

1
2
fin.close();
fout.close();

Template

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
#include <fstream>
using namespace std;  // Both types are in the std namespace

ifstream fin("data.in");
ofstream fout("data.out");

int main(void) {
  /*
  In the code between, replace cin with fin and cout with fout.
  */
  fin.close();
  fout.close();
  return 0;
}

References

  1. Informatics Olympiad One-stop Guide