file open cpp 1

file open cpp

#include 
#include 
using namespace std;

int main () {
  ofstream myfile;
  myfile.open ("file.txt");
  myfile << "Writing to a file.\n";
  myfile.close();
  return 0;
}

Here is what the above code is Doing:
1. We create an ofstream object called myfile.
2. We open the file "file.txt" for writing.
3. We write the text "Writing to a file.\n" to the file.
4. We close the file.

If the file "file.txt" does not exist, it will be created.

If the file "file.txt" already exists, its contents will be overwritten.

If you want to append to an existing file, instead of overwriting it, you can open the file in append mode.

Similar Posts