Reading and writing text files in Open C++

From Nokia Developer Wiki
Jump to: navigation, search
Article Metadata
Tested with
Devices(s): Nokia 6220 Classic
Platform(s): S60 3rd Edition, FP2
Keywords: fstream, ifstream, ofstream, ifstream::get(), ifstream::getline(), ofstream::put(), fstream::seekg(), fstream::is_open()
Created: aknyman (24 Sep 2008)
Last edited: lpvalente (26 Oct 2012)



This code snippet shows how to use Open C++ file I/O classes for text file read and write operations. In this case, a text file is considered to be a file that stores words and sentences in readable plain text. The stream class fstream can be used for both read and write file I/O operations. Ifstream is only capable of file read operations and ofstream is only capable of file write operations. A header file <fstream> must be included in the application before these classes can be used. Reading and writing text files is possible with the extraction (<<) and the insertion (>>) operators and methods like put(), get(), and getline().

Note: In order to use this code, you need to install the Open C/C++ plug-in.

This snippet can be self-signed.

MMP file

The following libraries are required:

LIBRARY libstdcpp.lib
LIBRARY libc.lib
LIBRARY euser.lib

Source file

#include <fstream>  //fstream, ifstream, ofstream
#include <string> //string, getline()
#include <iostream> //cout
using namespace std;
void simple_write_example()
ofstream file;
char line [] = "Text To File";
string str = "Hello";
//default open mode is a text mode,
//a new file is created if a file does not exist
//if a path is not given the applications's \private folder is used
file.open("simple.txt"); //ios::out | ios::trunc
file << line << endl;
file.put('A') ;
file << 1;
file << str;
//close the file
void simple_read_example()
ifstream file;
char line [20];
char chr = ' ';
int num = 0;
string str = "";
file.open("simple.txt"); //ios::in
//getline has an optional third argument (character)
//that will end getline's input, the default value is '\n'
file.getline(line, 20);
file.get(chr); // or file >> chr;
file >> num;
file >> str;
cout << "line:"<< line << endl;
cout << "chr:" << chr << endl;
cout << "num:" << num << endl;
cout << "str:"<< str << endl;
void simple_read_and_write_example()
fstream file;
string line;
//fstream provides attributes which define
//how a file should be opened:
//app = 0x01 - append to the end of a file
//ate = 0x02 - place the file marker at the end of the file
//binary = 0x04 - open as a binary file
//in = 0x08 - open file for reading
//out = 0x10 - open file for writing
//trunc = 0x20 - truncate an existing file and overwrite (default)
file.open("simple.txt", ios::in | ios::out);
//write to text file
file << "This is a line 1" << endl;
file << "This is a line 2" << endl;
//fstream works with read and write file pointers,
//by moving these pointers it is possible to access any part of
//the file at random - seekg() moves the read pointer and
//seekp() moves the write pointer
//seek attributes:
//ios::beg - beginning of the file
//ios::end - end of the file
//ios::cur - current location of the file
//read text from file
if (file.is_open())
while (!file.eof())
getline(file, line);
cout << line << endl;
int main()
//using class ofstream
//using class ifstream
//using class fstream
return 0;


The Open C++ I/O classes fstream, ifstream, and ofstream are used to read and write text from/to the created file simple.txt. Content of the file is displayed as standard output.

See also

This page was last modified on 26 October 2012, at 20:05.
477 page views in the last 30 days.