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.
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;
}
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
Bookmarks