C++ Standard Library

Input/Output with files

ofstream: Stream class to write on files

ifstream: Stream class to read from files

fstream: Stream class to both read and write from/to files

file.cpp

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
// basic file operations
#include <iostream>
#include <fstream>
using namespace std;

int main(){

  ofstream myfile;
  
  myfile.open("example.txt");
  myfile << "Writing this to file." << endl;
  myfile.close();

  return 0;
}

file.cpp の実行結果は:

[cactus:~/code_c++/cpp_tuts]% ./file
[cactus:~/code_c++/cpp_tuts]% cat example.txt
Writing this to file.

example.txt

Writing this to file.

Text files

textfile.cpp

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
// writing on a text file
#include <iostream>
#include <fstream>
using namespace std;

int main(){

  ofstream myfile("example1.txt");

  if( myfile.is_open() ){
    myfile << "This is the first line" << endl;
    myfile << "This is the second line" << endl;
    myfile.close();
  }else{
    cout << "Uable to open this file!!" << endl;
  }

  return 0;
}

textfile.cpp の実行結果は:

[cactus:~/code_c++/cpp_tuts]% ./textfile
[cactus:~/code_c++/cpp_tuts]% cat example1.txt
This is the first line
This is the second line

example1.txt

This is the first line
This is the second line

r_file.txt

The first line
第二行目
第三行目

r_file.cpp

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
// reading a text file
#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int main(){
  string line;
  ifstream myfile("r_file.txt");

  if( myfile.is_open() ){
    while( ! myfile.eof() ){
      getline(myfile, line);
      cout << line << endl;
    }
    myfile.close();
  }else{
    cout << "Uable to open this file!!" << endl;
  }

  return 0;
}

r_file.cpp の実行結果は:

[cactus:~/code_c++/cpp_tuts]% ./r_file
The first line
第二行目
第三行目

filesize

filesize.cpp

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
// obtaining file size
#include <iostream>
#include <fstream>

using namespace std;

int main(){
  long begin, end;
  
  ifstream myfile("r_file.txt");
  
  begin = myfile.tellg();
  myfile.seekg(0, ios::end);
  end = myfile.tellg();

  myfile.close();

  cout << "r_file.txt size is: " << (end - begin) << " bytes." << endl;
    
  return 0;
}

filesize.cpp の実行結果は:

[cactus:~/code_c++/cpp_tuts]% ./filesize
r_file.txt size is: 41 bytes.

binaryfile.cpp

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
// reading a complete binary file
#include <iostream>
#include <fstream>

using namespace std;

ifstream::pos_type size;
char *memblock;

int main(){
  
  ifstream file("bf.bin", ios::in | ios::binary | ios::ate);
  
  if( file.is_open() ){
    size = file.tellg();
    memblock = new char [size];
    file.seekg(0, ios::beg);
    file.read(memblock, size);
    file.close();
    cout << "the complete file content is in memory" << endl;
    delete[] memblock;
  }else{
    cout << "Unable to open file" << endl;
  }
    
  return 0;
}

binaryfile.cpp の実行結果は:

[cactus:~/code_c++/cpp_tuts]% ./binaryfile
the complete file content is in memory

Table Of Contents

Previous topic

Advanced Concepts

Next topic

C++ by Dissection (Ira Pohl)