Results 1 to 20 of 34

Thread: Restrict user to open the same file

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Sep 2006
    Posts
    339
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    15
    Thanked 21 Times in 16 Posts

    Default Re: Restrict user to open the same 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*......

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,373
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Thanks
    3
    Thanked 5,019 Times in 4,795 Posts
    Wiki edits
    10

    Default Re: Restrict user to open the same file

    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.

  3. #3
    Join Date
    Sep 2006
    Posts
    339
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    15
    Thanked 21 Times in 16 Posts

    Default Re: Restrict user to open the same file

    Quote Originally Posted by wysota View Post
    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:
    1. int main ( )
    2. {
    3. FILE *pFile = NULL;
    4. char *fileName = "Logfile.log";
    5.  
    6. // Open the file in write mode
    7. #ifdef win32
    8. fopen_s(&pFile, fileName ,"r");
    9. #else
    10. pFile = fopen( fileName ,"r");
    11.  
    12. if (!pFile)
    13. {
    14. printf("Error opening file %s!\n", fileName);
    15. exit(1);
    16. }
    17.  
    18. // Lock the file.
    19. #ifdef win32
    20. _lock_file(pFile);
    21. #else
    22. //lock file on linux
    23.  
    24. printf("Locking the file %s.\n", fileName);
    25.  
    26. ifstream ifs( pFile ); //This doesnt work on linux why???
    27. char line[255];
    28. ifs.seekg( 0 );
    29. while( !ifs.eof() )
    30. {
    31. ifs.getline( line, sizeof( line ) );
    32. cout<<line;
    33. }
    34. return 0;
    35. }
    To copy to clipboard, switch view to plain text mode 

    Thanks

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,373
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Thanks
    3
    Thanked 5,019 Times in 4,795 Posts
    Wiki edits
    10

    Default Re: Restrict user to open the same file

    I don't see any "#endif" lines...

  5. #5
    Join Date
    Sep 2006
    Posts
    339
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    15
    Thanked 21 Times in 16 Posts

    Default Re: Restrict user to open the same file

    Quote Originally Posted by wysota View Post
    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...

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,373
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Thanks
    3
    Thanked 5,019 Times in 4,795 Posts
    Wiki edits
    10

    Default Re: Restrict user to open the same file

    What errors do you get?

  7. #7
    Join Date
    Apr 2007
    Location
    Bangalore,India
    Posts
    25
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    2

    Default Re: Restrict user to open the same file

    Where is the error?????
    Veda

  8. #8
    Join Date
    Sep 2006
    Posts
    339
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    15
    Thanked 21 Times in 16 Posts

    Default Re: Restrict user to open the same file

    Quote Originally Posted by wysota View Post
    What errors do you get?
    Ok, First the sample
    Qt Code:
    1. int main (){
    2. FILE *pFile = NULL;
    3. char *fileName = "Logfile.log";
    4.  
    5. // Open the file in read mode
    6. #ifdef win32
    7. fopen_s(&pFile, fileName ,"r");
    8. #else
    9. pFile = fopen( fileName ,"r");
    10. #endif
    11.  
    12. if (!pFile){
    13. printf("Error opening file %s!\n", fileName);
    14. exit(1);
    15. }
    16.  
    17. // Lock the file.
    18. #ifdef win32
    19. _lock_file(pFile);
    20. #else
    21. flockfile(pFile);
    22. #endif
    23.  
    24. printf("Locking the file %s.\n", fileName);
    25. ifstream ifs( pFile ); //This doesnt work on linux why???
    26. char line[255];
    27. ifs.seekg( 0 );
    28. while( !ifs.eof() ){
    29. ifs.getline( line, sizeof( line ) );
    30. cout<<line;
    31. }
    32.  
    33. //unlock the file after the instance is closed
    34. return 0;
    35. }
    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

  9. #9
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,373
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Thanks
    3
    Thanked 5,019 Times in 4,795 Posts
    Wiki edits
    10

    Default Re: Restrict user to open the same file

    Could we see the exact message? It probably suggest other constructors.

  10. #10
    Join Date
    Sep 2006
    Posts
    339
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    15
    Thanked 21 Times in 16 Posts

    Default Re: Restrict user to open the same file

    Quote Originally Posted by wysota View Post
    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.

  11. #11
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,373
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Thanks
    3
    Thanked 5,019 Times in 4,795 Posts
    Wiki edits
    10

    Default 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.

  12. #12
    Join Date
    Sep 2006
    Posts
    339
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    15
    Thanked 21 Times in 16 Posts

    Default Re: Restrict user to open the same file

    Quote Originally Posted by wysota View Post
    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.

    Qt Code:
    1. 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 
    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???

  13. #13
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,373
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Thanks
    3
    Thanked 5,019 Times in 4,795 Posts
    Wiki edits
    10

    Default 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.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.