C++ Part 23

Using Getline

Reading From a Text File Using ifstream:

To read from a text file we need to include the fstream library and create an ifstream object which is placed in the std namespace.


#include <fstream>
using namespace std;  


Example


//NavidPlus.blogspot.com
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main( )
{
   string Name, Website;
   string description;
   fstream inputStream;
   inputStream.open("Website");

   inputStream >> Name >> Website;
   inputStream >> description;
   cout << "Name: " << Name << "      "  << "Website: " << Website << endl;
   cout << "description: " << description << endl;
   inputStream.close();
   return 0;
}

The input file is Website and must be in the same folder


NavidPlus https://NavidPlus.blogspot.com
NavidPlus@Blogger


Post a Comment

Popular Posts