Wednesday 24 August 2016

Write a C++ program that creates an output file, writes information to it, closes the file and open it again as an input file

Write a C++ program that creates an output file, writes information to it, closes the file and open it again as an input file and read the information from the file.

#include <fstream>
#include <iostream>
using namespace std;
int main()
{
  char str[10];
  //Creates an instance of ofstream, and opens example.txt
  ofstream a_file("c:\\one.txt");
  // Outputs to example.txt through a_file
  a_file<<"This text will now be inside of example.txt";
  // Close the file stream explicitly
  a_file.close();
  //Opens for reading the file
  ifstream b_file("c:\\one.txt");
   //Reads one string from the file
  b_file>>str;
  //Should output 'this'
  cout<<str<<"\n";
  cin.get();    // wait for a keypress
  // b_file is closed implicitly here
}

1 comment:

  1. Its showing and error it shows only this as output.

    ReplyDelete