Create output file : File Write « File « C++






Create output file

Create output file
#include <iostream>
#include <fstream>
using namespace std;

int main()
{
  ofstream fout("test");  

  if(!fout) {
    cout << "Cannot open output file.\n";
    return 1;
  }

  fout << "Hello!\n";
  fout << 100 << ' ' << hex << 100 << endl;

  fout.close();

  ifstream fin("test"); // open input file

  if(!fin) {
    cout << "Cannot open input file.\n";
    return 1;
  }

  char str[80];
  int i;

  fin >> str >> i;
  cout << str << ' ' << i << endl;

  fin.close();

  return 0;
}


           
       








Related examples in the same category

1.Write string to a file
2.Change file contentChange file content