The C++ file input and output: using seekg(), tellg(), read() etc member functions C++ code sample

 

Compiler: Visual C++ Express Edition 2005

Compiled on Platform: Windows XP Pro SP2

Header file: Standard

Additional library: none/default

Additional project setting: Set project to be compiled as C++

Project -> your_project_name Properties -> Configuration Properties -> C/C++ -> Advanced -> Compiled As: Compiled as C++ Code (/TP)

Other info: none

To do: Using the seekg(), tellg(), read() etc member functions in C++ programming

To show: The C++ file input and output programming on how to use the seekg(), tellg(), read() etc member functions C++ code sample

 

// using the seekg(), tellg(), open(), read(), fail() etc

#include <iostream>

#include <fstream>

using namespace std;

 

void main(void)

{

char filename[ ] = "C:\\amad\\testfileio3.txt";

fstream inputfile, outputfile;

int length;

char* buffer;

 

// create, open and write data to file

outputfile.open(filename, ios::out);

 

// simple error handling

if(inputfile.fail())

{

cerr<<"The file "<<filename<<" could not be created/opened!\n";

exit(1);

}

// test if successful to close the file

else

cout<<"The "<<filename<<" file was opened successfully!\n";

 

// write some text

cout<<"Writing some text to the output file..."<<endl;

outputfile<<"This is just line of text as a simple file content. This is testfileio3.txt file"<<endl;

// close the output file

cout<<"Closing the output file..."<<endl;

outputfile.close();

 

// opening and reading data from file

cout<<"Opening the input file..."<<endl;

inputfile.open(filename, ios::in);

 

// simple error handling for files creating/opening for writing

if(inputfile.fail())

{

cout<<"Opening "<<filename<<" file for reading\n";

cout<<"------------------------------------------\n";

cerr<<"The file "<<filename<<" could not be created/opened!\n";

cerr<<"Possible errors:\n";

cerr<<"1. The file does not exist.\n";

cerr<<"2. The path was not found.\n";

exit(1); // just exit

// 0-normal, non zero - some errors

}

else

{

cout<<"The "<<filename<<" file was opened successfully!\n";

cout<<"\nMove the pointer to the end\n"

<<"Then back to the beginning with\n"

<<"10 offset. The pointer now at...\n"<<endl;

// flush the stream buffer explicitly

cout<<flush;

// get length of file, move the get pointer to the end of the stream

inputfile.seekg(0, ios::end);

// The tellg() member function returns the current stream position.

length = inputfile.tellg();

cout<<"string length = "<<length<<"\n";

// dynamically allocate some memory storage for type char

buffer = new char[length];

// move back the pointer to the beginning with offset of 10 (skips 10 char)

inputfile.seekg(10, ios::beg);

// read data as block from input file

inputfile.read(buffer, length);

cout<<buffer<<endl;

// free up the allocated memory storage

delete [ ] buffer;

// close the input file

inputfile.close();

}

return;

}

 

Output example:

 

The C:\amad\testfileio3.txt file was opened successfully!

Writing some text to the output file...

Closing the output file...

Opening the input file...

The C:\amad\testfileio3.txt file was opened successfully!

Move the pointer to the end

Then back to the beginning with

10 offset. The pointer now at...

string length = 82

st line of text as a simple file content. This is testfileio3.txt file

═══════════²²²²▌▌◄

Press any key to continue . . .

 

(there is some rubbish)

 

 

C and C++ Programming Resources | C & C++ Code Example Index