Reputation: 393
I'm writing class to r/w files from OS file system or from my own archives format in my game engine. How can I make impossible to open file by std::fopen()
or std::fstream
in modes "rw"
and "r"
. I have written some code to test that on Linux. Here's it:
#include <fstream>
#include <iostream>
using namespace std;
int main()
{
fstream in("file.txt", ios::in | ios::out);
if(!in.is_open())
{
cout << "Plik nie może być otwarty w trybie rw" << endl;
return 1;
}
cout << "Plik otwarty w trybie rw" << endl;
in << ".test.";
cout << "Wpisano tekst" << endl;
while(1){}
return 0;
}
/* Drugi plik */
/* The second src code */
#include <fstream>
#include <iostream>
using namespace std;
int main()
{
fstream in("file.txt", ios::in);
if(!in.is_open())
{
cout << "Plik nie może być otwarty w trybie r" << endl;
return 1;
}
cout << "Plik otwarty w trybie r" << endl;
cout << in << endl;
return 0;
}
When I've executed the ./rw
program and some instances of ./r
, the ./rw
has gone into endless loop and the instances of ./r
have terminated with 0
code.
Sorry for my English. :)
Upvotes: 0
Views: 98
Reputation: 249153
You should "lock" the file using lockf()
: http://man7.org/linux/man-pages/man3/lockf.3.html
Upvotes: 1