Write strings to disk : Text File « File « C++






Write strings to disk

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

int main(int argc, char *argv[])
{
  if(argc!=2) {
    cout << "Usage: WRITE <filename>\n";
    return 1;
  }

  ofstream out(argv[1]); // output file

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

  char str[80];
  cout << "Write strings to disk, '$' to stop\n";

  do {
    cout << ": ";
    cin >> str;
    out << str << endl;
  } while (*str != '$');

  out.close();
  return 0;
}


           
         
  








Related examples in the same category

1.Use the getline function with the C++ string classUse the getline function with the C++ string class
2.Write string to a file
3.Write to file: ofstream
4.Read from file.Read from file.
5.Reading from a File
6.Writing to a File
7.reading a text file
8.writing on a text file
9.Sequential Files