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

Jul 25, 2013 at 11:05am
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 Jul 25, 2013 at 11:23am
Jul 25, 2013 at 11:17am
I mean when the code is executed. How to forbid replacing the file("example.txt") as I executed the code again.
Last edited on Jul 25, 2013 at 12:01pm
Jul 25, 2013 at 11:25am
Jul 25, 2013 at 11:29am
I am confused, please take me the mode flag which forbid the file replacing.
Last edited on Jul 25, 2013 at 11:31am
Jul 25, 2013 at 11:40am
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 ) ;


Jul 25, 2013 at 11:59am
Thank you.
Last edited on Jul 25, 2013 at 12:14pm
Topic archived. No new replies allowed.