Using the C++ file input and output class with open(), get(), close() etc methods for opening, reading from and writing to a file

 

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 C++ file input and output class with open(), get(), close() etc methods for opening, reading from and writing to a file

To show: How to use the C++ file input and output class with open(), get(), close() for opening, reading from and writing to a file

 

Create a text file at the C drive, name it sampleread.txt, paste the following text and save it.

 

This is sampleread.txt file content. This file will be opened for reading

then its content will be written to another file and standard output

i.e screen/console...after you have executed this C++ program,

without error....this text should be output on your screen as well as

written to the samplewrite.txt file. Don't forget to check the

content of the samplewrite.txt.

 

// reading from available file content then writing the content to another file. Firstly, create file for reading (can include path),

// let says "C:\sampleread.txt", at root on C drive. Type some text as shown then executes this program.

#include <iostream>

#include <fstream>

using namespace std;

 

// function definition, to open file for reading

void openinfile(ifstream &infile)

{

char filename[100];

 

// enter the file name that you have created (can include a full path if any). From the comment above

// you have to enter "C:\sampleread.txt" without the double quotes.

cout<<"Enter the file name: ";

// read and store the file name

cin>>filename;

infile.open(filename);

}

 

void main(void)

{

// declare the input file stream

ifstream inputfile;

// declare the output file stream

ofstream outputfile;

char chs;

 

// function call for opening file for reading

openinfile(inputfile);

// create, if not exist and open it for writing

outputfile.open("C:\\samplewrite.txt");

// test until the end of file

while (!inputfile.eof())

{

// read character until the end of file

inputfile.get(chs);

if (!inputfile.eof())

{

// output character by character (byte) on screen, standard output

cout<<chs;

// write to output file, samplewrite.txt

outputfile<<chs;

}

}

cout<<"\nReading and writing file is completed!"<<endl;

// close the input file stream

inputfile.close();

// close the output file stream

outputfile.close();

}

 

Output example:

 

Enter the file name: C:\sampleread.txt

This is sampleread.txt file content. This file will be opened for reading

then its content will be written to another file and standard output

i.e screen/console...after you have executed this C++ program,

without error....this text should be output on your screen as well as

written to the samplewrite.txt file. Don't forget to check the

content of the samplewrite.txt.

Reading and writing file is completed!

Press any key to continue . . .

 

 

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