Write char to a file : File Read « File « C++






Write char to a file

Write char to a file
 

#include <iostream>
#include <fstream>
using namespace std;

int main(int argc, char *argv[])
{
  char ch;

  if(argc!=2) {
    cout << "Usage: WRITE <filename>\n";
    return 1;
  }

  ofstream out(argv[1], ios::out | ios::binary);

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

  cout << "Enter a $ to stop\n";
  do {
    cout << ": ";
    cin.get(ch);
    out.put(ch);
  } while (ch!='$');

  out.close();

  return 0;
}

           
         
  








Related examples in the same category

1.Read file contentRead file content
2.Demonstrate gcount().Demonstrate gcount().
3.Use getline() to read a string that contains spaces.Use getline() to read a string that contains spaces.
4.Demonstrate peek() in ifstreamDemonstrate peek() in ifstream
5.Demonstrate seekg().
6.Read formatted data from a file.
7.Write unsigned char to a file and read it back