TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

C++ File and Stream

C++ File and Stream tutorial for beginners and professionals with examples on constructor, if-else, switch, break, continue, comments, arrays, object and class, exception, static, structs, inheritance, aggregation etc.

<< Back to CPP

C++ Files and Streams

In C++ programming we are using the iostream standard library, it provides cin and cout methods for reading from input and writing to output respectively.

To read and write from a file we are using the standard C++ library called fstream. Let us see the data types define in fstream library is:

Data Type Description
fstream It is used to create files, write information to files, and read information from files.
ifstream It is used to read information from files.
ofstream It is used to create files and write information to the files.

C++ FileStream example: writing to a file

Let's see the simple example of writing to a text file testout.txt using C++ FileStream programming.

#include <iostream>
#include <fstream>
using namespace std;
int main () {
  ofstream filestream("testout.txt");
  if (filestream.is_open())
  {
    filestream << "Welcome to javaTpoint.\n";
    filestream << "C++ Tutorial.\n";
    filestream.close();
  }
  else cout <<"File opening is fail.";
  return 0;
}

Output:

The content of a text file testout.txt is set with the data:
Welcome to javaTpoint.
C++ Tutorial.

C++ FileStream example: reading from a file

Let's see the simple example of reading from a text file testout.txt using C++ FileStream programming.

#include <iostream>
#include <fstream>
using namespace std;
int main () {
  string srg;
  ifstream filestream("testout.txt");
  if (filestream.is_open())
  {
    while ( getline (filestream,srg) )
    {
      cout << srg <<endl;
    }
    filestream.close();
  }
  else {
      cout << "File opening is fail."<<endl; 
    }
  return 0;
}

Note: Before running the code a text file named as "testout.txt" is need to be created and the content of a text file is given below:
Welcome to javaTpoint.
C++ Tutorial.

Output:

Welcome to javaTpoint.
C++ Tutorial.

C++ Read and Write Example

Let's see the simple example of writing the data to a text file testout.txt and then reading the data from the file using C++ FileStream programming.

#include <fstream>
#include <iostream>
using namespace std;
int main () {
   char input[75];
   ofstream os;
   os.open("testout.txt");
   cout <<"Writing to a text file:" << endl;
   cout << "Please Enter your name: "; 
   cin.getline(input, 100);
   os << input << endl;
   cout << "Please Enter your age: "; 
   cin >> input;
   cin.ignore();
   os << input << endl;
   os.close();
   ifstream is; 
   string line;
   is.open("testout.txt"); 
   cout << "Reading from a text file:" << endl; 
   while (getline (is,line))
   {
   cout << line << endl;
   }	
   is.close();
   return 0;
}

Output:

Writing to a text file:  
 Please Enter your name: Nakul Jain    
Please Enter your age: 22  
 Reading from a text file:   Nakul Jain  
 22

Next TopicC++ getline()




Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf