File Operations

The Concept of Files

A file is a collection of data gathered for a specific purpose. C/C++ regards every file as an ordered stream of bytes, and every file ends with an end-of-file indicator (EOF). To operate on a file, a program should first open it. Whenever a file is opened (remember to close files that you open), the file is associated with a stream; here, a stream is actually a sequence of bytes.

C/C++ divides files into text files and binary files. Text files are simply text files (the important case); binary files are files in special formats, executable code files, and so on.

File Operations

  1. Open the file, point the file pointer to it, and decide the file-opening mode;
  2. Read from and write to the file (the operations mainly used in contests; some other operations are omitted for now);
  3. Close the file after using it.

The freopen Function

Introduction

This function redirects a specified input or output stream to a file in a specified mode. It is declared in the stdio.h (cstdio) header. It can change the input and output environment without changing the original code, but you should ensure that the stream is valid when using it.

There are three main modes: read, write, and append.

Syntax

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

Parameters

  • filename: the name of the file to open
  • mode: the file-opening mode, indicating the file-access permissions
  • stream: the file pointer, usually a standard file stream (stdin/stdout) or the standard error stream (stderr)
  • Return value: a file pointer pointing to the opened file

File Opening Modes (Optional Reading)

  • r: open the file for read-only access; the file must exist and data can only be read (common)
  • r+: open the file for read/write access; the file must exist and data can be read and written
  • rb: open a binary file for read-only access; the file must exist and data can only be read
  • rb+: open a binary file for read/write access; the file must exist and data can be read and written
  • rt+: open a text file for read/write access; data can be read and written
  • w: open the file for write-only access; create it if it does not exist, otherwise clear its contents; data can only be written (common)
  • w+: open the file for read/write access; create it if it does not exist, otherwise clear its contents; data can be read and written
  • wb: open a binary file for write-only access; create it if it does not exist, otherwise clear its contents; data can only be written
  • wb+: open a binary file for read/write access; create it if it does not exist, otherwise clear its contents; data can be read and written
  • a: open the file for write-only access; create it if it does not exist, and append written data to the end (preserving the EOF character)
  • a+: open the file for read/write access; create it if it does not exist, and append written data to the end (without preserving the EOF character)
  • at+: open a text file for read/write access, appending written data to the end
  • ab+: open a binary file for read/write access, appending written data to the end

Usage

Read file contents:

freopen("data.in", "r", stdin);
// data.in is the name of the file to read, and must be in the same directory as the executable

Output to a file:

freopen("data.out", "w", stdout);
// data.out is the name of the output file, and must be in the same directory as the executable

Close standard input/output streams:

fclose(stdin);
fclose(stdout);
Note

Functions such as printf/scanf/cin/cout use stdin/stdout by default. After redirecting stdin/stdout, these functions read from or write to the redirected file.

Template

#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
  */
  fclose(stdin);
  fclose(stdout);
  return 0;
}

The fopen Function (Optional Reading)

The function is broadly the same as freopen: it opens the specified file and returns a pointer to the opened file.

Function Prototype

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

The meanings of the parameters are the same as for freopen.

Available Read/Write Functions (Basic)

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

Usage

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 and Output Streams

Usage

Read file contents:

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

Output to a file:

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

Close standard input/output streams:

fin.close();
fout.close();

Template

#include <fstream>
using namespace std;  // Both types are in the std namespace

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

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

References

  1. One Book Through the Olympiad in Informatics

This content comes from OI Wiki: File Operations. See the original for authors and revision history. AIOJ has adapted the formatting, in-site links, and resource paths.

Content is shared under CC BY-SA 4.0 and SATA license; additional terms are governed by the original notice. Thanks to the OI Wiki authors and contributors; per the SATA terms, please visit the OI Wiki repository and star it to show your support.