Pages

Sunday, October 15, 2017

Read one word from a file.



Following C++ program will read a word from data.txt file stored in the same location where source program is stored.

#include <fstream>
#include <iostream>
using namespace std;

int main () {
   
   char word[20];

   ifstream fin;    //open file to read.
   fin.open("data.txt");
 
   cout << "Reading a word from a file...\n";
   
   fin >> word;
   cout << word;    //write data to screen.
 
   fin.close();     //close the opened file.
   return 0;
}

Output of program

Reading a word from a file...
hello

No comments:

Post a Comment