File Input Output C++

File Input Output C++

In  C/C++ programming language, Input/output library refers to a family of class templates and supporting functions in the C++ Standard Library that implement stream-based input/output capabilities. Some Useful Functions that might come very handy in I/O.

I/O libraries

ifstream: Stream class to read.
ofstream: Stream class to write.
fstream: Stream class to read and write.

ifstream OpenFile(char *filename, int mode);

  • You should know the the file name which is stored in the filename var and the mode determine how to open the file.
  • ios::in Open file to read ios::
  • out Open file to write
  • ios::app All the date you write, is put at the end of the file. It calls ios::
  • out ios::ate All the date you write, is put at the end of the file. It does not call ios::out
  • ios::trunc Deletes all previous content in the file. (empties the file)
  • ios::nocreate If the file does not exists, opening it with the open() function gets impossible.
  • ios::noreplace If the file exists, trying to open it with the open() function, returns an error.
  • ios::binary Opens the file in binary mode.

Create file and input some string C++

#include <fstream>
using namespace std;

int main()
 ofstream myfile;"mycodinglab.txt");
 myfile << "writing this to mycodinglab.txt \n";
 return 0;

Check is a file is open while reading I/O C++

ofstream myfile("mycodinglab.txt");
if (myfile_open()) or if (!myfile.eof())

state flags
bad() Returns true if a reading or writing fails.
fail() Returns true if a format error occure.
eof() Returns true if a file reading is at the end.
good() Returns true if there are no errors.

Reading File Input/Output C++ source code

#include <fstream.h>
void main()
 ifstream myfile;"mycodinglab.txt");

char out;
 cout << out;
 return 0;

//Closing a file

For Example

#include <fstream.h>
void main()
 ofstream SaveFile("mycodinglab.txt", ios::ate);
 SaveFile << "That's new!\n";

Some more Input/Output C++ source code

Example 1: The most usual way

fstream File(“cpp-home.txt”);
if (!File)
cout << “Error opening the file! Aborting…\n”;

Example 2: If the file is created, return an error

ofstream File("unexisting.txt", ios::nocreate);

cout << “Error opening the file! Aborting…\n”;

Example 3: Using the fail() function

ofstream File("filer.txt", ios::nocreate);

cout << “Error opening the file! Aborting…\n”;

 Functions that might come very handy in Input/Output C++

  1. tellg() – Retunrs an int type, that shows the current position of the inside-pointer. This one works only when you read a file.
  2. tellp() – The same as tellg() but used when we write in a file. To summarize: when we read a file, and we want to get the current position of the inside-pointer, we should use tellg(). When we write in a file, and we want to get the current position of the inside-pointer, we should use tellp().
  3. seekp() – go to specified position.
  4. ignore() – Used when reading a file. If you want to ignore certain amount of characters, just use this function.

Some more complicated example showing how to copy files in Linux environment using C++ libraries

<pre>#include <stdlib.h>
#include <string>
#include <iostream>
#include <fstream>

 using namespace std;

 int main() {

 //declare global variables
 ifstream inFile;
 ofstream outFile;
 char inputFilename[90];
 char outputFilename[90];
 char flag; string p;

 //user input
 cout << endl;
 cout<< "Input path to sourse and destination files: "<< endl;
 cin >> inputFilename >> outputFilename;, ios::in);

 //check the file
 if (!inFile) {
 cerr << "Can't open input file " << inputFilename << endl;
 //open the file, ios::in);

 //check if the output(second) file exist and if not make one
 if (!outFile) {
 cerr << "Can't open output file: "<< outputFilename << endl;
 cout << "Perhaps the file doesn't exist, Would you like to create one Y/N: ";
 cin >> flag;

 //check the user input
 if(flag == 'y'), ios::out); else exit(0);
 cout << "start copying information from "
 << inputFilename
 << " to "
 << outputFilename<< endl;

 //copy files
 while (!inFile.eof()) {

 cout << "copy finished " << endl;
 cout << "closing files.... " << endl;

 //close both files

 return 0;
 }// end of main

You must be logged in to post a comment.

Proudly powered by WordPress   Premium Style Theme by
%d bloggers like this: