How to forbid file replacing in fstream library for C++

How to forbid file replacing in fstream library for C++?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream>
#include <fstream>

using namespace std;

int main()
{
  ofstream myfile;
  myfile.open ("example.txt");
  myfile << "Writing this to a file.\n";
  myfile.close();
  cin.get;
  return 0;
};
Last edited on
I mean when the code is executed. How to forbid replacing the file("example.txt") as I executed the code again.
Last edited on
I am confused, please take me the mode flag which forbid the file replacing.
Last edited on
1
2
// open the file for reading and writing, fail if file does not exist
std::fstream one( "example.txt", std::ios::in | std::ios::out ) ;


1
2
3
4
// open an existing file for reading and writing, 
// or create a new file if it does not exist, 
// write always appends to the file
std::fstream one( "example.txt", std::ios::in | std::ios::out | std::ios::app ) ;


Thank you.
Last edited on
Topic archived. No new replies allowed.