How to convert C++ stream to FILE*......You can get a FILE* from a C++ stream, you know... And then both flock and _lock_file will work.![]()
How to convert C++ stream to FILE*......You can get a FILE* from a C++ stream, you know... And then both flock and _lock_file will work.![]()
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.
Qt 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; }To copy to clipboard, switch view to plain text mode
Thanks
I don't see any "#endif" lines...
What errors do you get?
Where is the error?????
Veda
Ok, First the sample
Qt 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; }To copy to clipboard, switch view to plain text mode
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
Could we see the exact message? It probably suggest other constructors.
Here it goes
/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 ***
Last edited by vermarajeev; 22nd May 2007 at 13:17.
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.
But the same constructor works fine on windows. According to me since it is a C++ stream, it should work.
But then I need to change a lot of code then and you know it is really overhead.Qt 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.To copy to clipboard, switch view to plain text mode
There has to be some way...Isnt it???
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.
Bookmarks