Re: Restrict user to open the same file
Quote:
Originally Posted by
wysota
You'll have to search through available documentation of ostream probably. There should be a way to access the buffer which you can then associate with a file descriptor or file handler or whatever.
So far so good.
I have written a sample to lock a file on windows. On linux there are some problems. Please help me to make the below code work on linux too.
Code:
int main ( )
{
FILE *pFile = NULL;
char *fileName = "Logfile.log";
// Open the file in write mode
#ifdef win32
fopen_s(&pFile, fileName ,"r");
#else
pFile = fopen( fileName ,"r");
if (!pFile)
{
printf("Error opening file %s!\n", fileName);
exit(1);
}
// Lock the file.
#ifdef win32
_lock_file(pFile);
#else
//lock file on linux
printf("Locking the file %s.\n", fileName);
ifstream ifs( pFile ); //This doesnt work on linux why???
char line[255];
ifs.seekg( 0 );
while( !ifs.eof() )
{
ifs.getline( line, sizeof( line ) );
cout<<line;
}
return 0;
}
Thanks
Re: Restrict user to open the same file
I don't see any "#endif" lines...
Re: Restrict user to open the same file
Quote:
Originally Posted by
wysota
I don't see any "#endif" lines...
Ok, I think I copied the wrong program....
There has to be #endif but still it doesnt work...
Re: Restrict user to open the same file
Re: Restrict user to open the same file
Re: Restrict user to open the same file
Quote:
Originally Posted by
wysota
What errors do you get?
Ok, First the sample
Code:
int main (){
FILE *pFile = NULL;
char *fileName = "Logfile.log";
// Open the file in read mode
#ifdef win32
fopen_s(&pFile, fileName ,"r");
#else
pFile = fopen( fileName ,"r");
#endif
if (!pFile){
printf("Error opening file %s!\n", fileName);
exit(1);
}
// Lock the file.
#ifdef win32
_lock_file(pFile);
#else
flockfile(pFile);
#endif
printf("Locking the file %s.\n", fileName);
ifstream ifs( pFile ); //This doesnt work on linux why???
char line[255];
ifs.seekg( 0 );
while( !ifs.eof() ){
ifs.getline( line, sizeof( line ) );
cout<<line;
}
//unlock the file after the instance is closed
return 0;
}
I get error on line 25 on linux machine. The compiler says there is no constructor defined.
There is no problem on windows.
I tried using both gcc and g++ compilers
Thanks
Re: Restrict user to open the same file
Could we see the exact message? It probably suggest other constructors.
Re: Restrict user to open the same file
Quote:
Originally Posted by
wysota
Could we see the exact message? It probably suggest other constructors.
Here it goes
Quote:
/Desktop/fileLock/lock/src/lock.cpp:77: error: no matching function
for call to ‘std::basic_ifstream<char, std::char_traits<char>
>::basic_ifstream(FILE*&)’
/usr/lib/gcc/i386-redhat-linux/4.0.0/../../../../include/c++/4.0.0/fstream:442:
note: candidates are: std::basic_ifstream<_CharT,
_Traits>::basic_ifstream(const char*, std::_Ios_Openmode) [with _CharT =
char, _Traits = std::char_traits<char>]
/usr/lib/gcc/i386-redhat-linux/4.0.0/../../../../include/c++/4.0.0/fstream:428:
note: std::basic_ifstream<_CharT, _Traits>::basic_ifstream() [with
_CharT = char, _Traits = std::char_traits<char>]
/usr/lib/gcc/i386-redhat-linux/4.0.0/../../../../include/c++/4.0.0/iosfwd:89:
note: std::basic_ifstream<char, std::char_traits<char>
>::basic_ifstream(const std::basic_ifstream<char,
std::char_traits<char> >&)
*** Exited with status: 2 ***
Re: Restrict user to open the same file
Seems it won't be that easy. You'd probably have to dig into the base class and find a way to initialise the object properly. It'd be much faster if you got rid of C++ streams and used stdio or if you used Qt streaming classes and files.
Re: Restrict user to open the same file
Quote:
Originally Posted by
wysota
Seems it won't be that easy. You'd probably have to dig into the base class and find a way to initialise the object properly.
But the same constructor works fine on windows. According to me since it is a C++ stream, it should work.
Code:
It'd be much faster if you got rid of C++ streams and used stdio or if you used Qt streaming classes and files.
But then I need to change a lot of code then and you know it is really overhead.
There has to be some way...Isnt it???
Re: Restrict user to open the same file
Sure there is a way. Dig into ios::base and find a way to use your FILE* as the appropriate buffer for the stream. That's quite time consuming, so that's why I suggested to find an alternative. Substituting using Qt streams should be quite quick using search&replace tools.
Re: Restrict user to open the same file
Quote:
Originally Posted by
wysota
Sure there is a way. Dig into ios::base and find a way to use your FILE* as the appropriate buffer for the stream. That's quite time consuming, so that's why I suggested to find an alternative. Substituting using Qt streams should be quite quick using search&replace tools.
OH!!! File locking doesnt work on windows. I'm surprised..Might be i'm doing somethig wrong..
Firstly
Code:
void _lock_file(
FILE* file
);
doesnt work on Microsoft Visual Studio .NET 2003. It works on only Microsoft Visual Studio 2005/.NET Framework 2.0.
I use Microsoft Visual Studio .NET 2003. Can people there tell me how to lock a file on windows (Microsoft Visual Studio .NET 2003 ) such that, it should be shared locking not exclusive. i.e the file should be locked to prevent other processes to read the same file.
Thanks.
Marcel, looking for some other links...
Thanks
Re: Restrict user to open the same file
Re: Restrict user to open the same file
Quote:
Originally Posted by
sunil.thaha
Hi sunil,
Thanks for the links. I'll try downloading and see how it works...