Page 2 of 2 FirstFirst 12
Results 21 to 34 of 34

Thread: Restrict user to open the same file

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

    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

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

    Default Re: Restrict user to open the same file

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

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

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

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

    Default Re: Restrict user to open the same file

    What errors do you get?

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

    Default Re: Restrict user to open the same file

    Where is the error?????
    Veda

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

    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

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

    Default Re: Restrict user to open the same file

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

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

    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 14:17.

  9. #29
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,363
    Thanks
    3
    Thanked 5,012 Times in 4,791 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    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.

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

    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???

  11. #31
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,363
    Thanks
    3
    Thanked 5,012 Times in 4,791 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    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.

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

    Default Re: Restrict user to open the same file

    Quote Originally Posted by wysota View Post
    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
    Qt Code:
    1. void _lock_file(
    2. FILE* file
    3. );
    To copy to clipboard, switch view to plain text mode 
    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

  13. #33
    Join Date
    Jan 2006
    Location
    Kerala
    Posts
    371
    Thanks
    76
    Thanked 37 Times in 32 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Restrict user to open the same file

    If you are using the Qt 4 Commercial edition. I think you are entitled to use the QtFileLock in the Qt Solutoins

    Just have a look at these
    http://trolltech.com/products/qt/add...ions/catalog/4
    http://trolltech.com/products/qt/add.../qtlockedfile/
    http://doc.trolltech.com/solutions/4...ockedfile.html
    Last edited by sunil.thaha; 25th May 2007 at 07:26. Reason: updated contents
    We can't solve problems by using the same kind of thinking we used when we created them

  14. The following user says thank you to sunil.thaha for this useful post:

    vermarajeev (25th May 2007)

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

    Default Re: Restrict user to open the same file

    Quote Originally Posted by sunil.thaha View Post
    If you are using the Qt 4 Commercial edition. I think you are entitled to use the QtFileLock in the Qt Solutoins

    Just have a look at these
    http://trolltech.com/products/qt/add...ions/catalog/4
    http://trolltech.com/products/qt/add.../qtlockedfile/
    http://doc.trolltech.com/solutions/4...ockedfile.html
    Hi sunil,
    Thanks for the links. I'll try downloading and see how it works...

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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.