jigneshdgreat
jigneshdgreat
Branch Unspecified
22 Jan 2012

How to give input to a c++ program from an input file ?

I know how to take input in c++ using standard console functions like cout and cin . But how can I use an external input file to give input to it??
Dancer_Engineer

Dancer_Engineer

Branch Unspecified
7 years ago
jigneshdgreat
I know how to take input in c++ using standard console functions like cout and cin . But how can I use an external input file to give input to it??
To get input from a file, you will have to use ifstream to read data from files as input streams.
To get output to a file, you will have to use ofstream to write data to files as output streams.

Include this library: #include <fstream>

I hope that helps. 😀
7 years ago
You can try using file handlers... Eg.:
#include <stdio.h>
int main ()
{
  FILE * pFile;
  pFile = fopen ("myfile.txt","w");
  if (pFile!=NULL)
  {
    fputs ("fopen example",pFile);
    fclose (pFile);
  }
  return 0;
}

Share this content on your social channels -

Only logged in users can reply.