Write string to a file : Text File « File « C++






Write string to a file

 

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

int main()
{
  ofstream out("test", ios::out | ios::binary);

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

  double num = 100.45;
  char str[] = "www.java2s.com";

  out.write((char *) &num, sizeof(double));
  out.write(str, strlen(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 strings to disk
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