CodesJava

Easy learning with example program codes

CPP File & Stream


CPP standard library fstream is used to create a file, to read the data from a file and to write the data into a file in CPP programming. There are three data types defined in fstream library. These are:

fstream:

Used to create a file, to read the data from a file and to write the data into a CPP file.

ifstream:

Used to read the data from a CPP file.

ofstream:

Used to create a file and to write the data into a CPP file.

 

Example:

#include <iostream>  
#include <fstream>  
using namespace std;  
 
int main ()
{  
char arr[100];  
ofstream str;  
str.open("myfile.txt");  
cout <<"Saving your informations." << endl;  
cout << "Dress Required: ";   
cin.getline(arr, 100);  
str << arr << endl;  
cout << "Dress Color Required: ";   
cin >> arr;  
cin.ignore();  
str << arr << endl;  
str.close();  
cout << "\n";
ifstream str2;   
string line;  
str2.open("myfile.txt");   
cout << "Review the Saved informations." << endl;   
while (getline (str2,line))  
{  
cout << line << endl;  
} 	 
str.close();  
return 0;  
}

Output

Saving your informations
Dress Required: T Shirt             
Dress Color Required: White                                      
 
Review the Saved informations.          
T Shirt                
White
Posted in CPP   
Sign Up/ Sign In
Ask a Question


Industrial Training

We offers Placement Oriented Training on Java, Spring, JSF, Hibernate, PHP, AngularJS, Angular 4, PLSQL, Oracle BI Publisher etc. We also provides Online training, please mail us at hr@codesjava.com.

Development

We also provides the Development services for Website Development , Java Development, PHP Development, Android App Development etc. You can contact us on hr@codesjava.com.

Copyright © 2018 CodesJava DMCA.com Protection Status SiteMap Reference: Java Wiki