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

    Updated: Oct 26, 2024
    Views: 1.1K
    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??
    0
    Replies
Howdy guest!
Dear guest, you must be logged-in to participate on CrazyEngineers. We would love to have you as a member of our community. Consider creating an account or login.
Replies
  • Dancer_Engineer

    MemberJan 20, 2012

    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. 😀
    Are you sure? This action cannot be undone.
    Cancel
  • PraveenKumar Purushothaman

    MemberJan 22, 2012

    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;
    }
    Are you sure? This action cannot be undone.
    Cancel
Home Channels Search Login Register