File in C++ - Writing text to a file in C++

By: Babu  

This C++ program shows you how to open a file and write some text into that file and save it.

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[] = "This is my text. I want to store this text in that file.";

  out.write((char *) &num, sizeof(double));
  out.write(str, strlen(str));


  return 0;

Archived Comments

1. Well first-oof, I don't see the obvious app.
View Tutorial          By: Marvin Ambrose at 2010-10-07 01:30:30

2. errr! how about reopening the file, and transfer the content of it into the program? plz....
View Tutorial          By: szel at 2010-10-05 20:01:04

Most Viewed Articles (in C++ )

Latest Articles (in C++)

Comment on this tutorial