Results 1 to 14 of 14

Thread: How to open a file in Read Write mode

  1. #1
    Join Date
    Jan 2007
    Posts
    326
    Thanks
    42
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    MacOS X

    Default How to open a file in Read Write mode

    Hi all

    Working on Qt4.2 on my Intel MAc

    I am trying to open a file in read write mode using

    QIODevice::ReadWrite
    But it returns -1 value.

    I also tried using
    O_WRONLY
    this

    then also its not working

    What can I do Pls help.

    Thanx
    Always Believe in Urself
    Merry

  2. #2
    Join Date
    Jan 2006
    Location
    Bremen, Germany
    Posts
    554
    Thanked 86 Times in 81 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to open a file in Read Write mode

    Did you check if the file really exists/ you have to proper rights to open it in write mode? Don't think so...

  3. #3
    Join Date
    Jan 2007
    Posts
    326
    Thanks
    42
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    MacOS X

    Default Re: How to open a file in Read Write mode

    Thanx 4 the reply

    Might be But How to Check that Is I am having proper rights to open a file in Write mode.

    and how can I get the permission.
    Always Believe in Urself
    Merry

  4. #4
    Join Date
    Jan 2007
    Posts
    326
    Thanks
    42
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    MacOS X

    Default Re: How to open a file in Read Write mode

    pls tell me how to get permissions for opening a file in read/write mode.

    regards
    merry
    Always Believe in Urself
    Merry

  5. #5
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    258
    Thanks
    22
    Thanked 19 Times in 16 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: How to open a file in Read Write mode

    Getting the permissions lies outside of Qt's responsibilities. Please check the documentation for QFileInfo:
    "bool QFileInfo::isWritable () const" and friends may be able to help you.
    Getting file permissions must be carried out by your operating system. On Linux you would do something like:
    chmod a+w filexy to give everyone on your system writepermission for filexy. Of course you need to have the priviliges to do that (either you own the file or you are superuser). Every system has its own way to do that. Check your systems documentation for that.

  6. #6
    Join Date
    Jan 2007
    Posts
    326
    Thanks
    42
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    MacOS X

    Default Re: How to open a file in Read Write mode

    Hi

    If i login as a " root " then also i dont have permissions to open a file in read/write mode.

    regards

    merry
    Always Believe in Urself
    Merry

  7. #7
    Join Date
    Jan 2006
    Posts
    14
    Thanked 2 Times in 2 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to open a file in Read Write mode

    Try to use this method after opening the file:

    Qt Code:
    1. FileError QFile::error () const
    To copy to clipboard, switch view to plain text mode 

    From the docs:
    Returns the file error status.
    The I/O device status returns an error code. For example, if open() returns false, or a read/write operation returns -1, this function can be called to find out the reason why the operation failed.
    This should help you understand why the open fails.

  8. #8
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    258
    Thanks
    22
    Thanked 19 Times in 16 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: How to open a file in Read Write mode

    Quote Originally Posted by merry View Post
    Hi
    If i login as a " root " then also i dont have permissions to open a file in read/write mode.
    merry
    Well, can you tell us what permissions the file has? you may be root and still not have the proper permissions. On linux you can find out the premissions by using a command like "ls -l". There must be equivalent commands on MacOSX. Your user must have both read and write permissions. Also check if you can access the directory containing the file itself. Try a simple appilcation to see wheter you can open and close simple files. You should always try to create a minimal compilable version of the code to experiment so you can spot the bugs. I wrote you a simple test app that lets you open files and has a log teling you what went wrong if it fails to do so.

    Qt Code:
    1. #include <QApplication>
    2. #include <QtGui>
    3.  
    4. class MainWindow : public QMainWindow
    5. {
    6. Q_OBJECT
    7. public:
    8. MainWindow(QWidget* parent=0) : QMainWindow(parent),
    9. msgCount(0)
    10. {
    11. resize(800, 600);
    12. m_editor = new QTextEdit;
    13. m_log = new QTextBrowser;
    14. m_log->setStyleSheet("background-color:rgb(20,40,60); color:white;");
    15.  
    16. QSplitter* splitter = new QSplitter;
    17. splitter->setOrientation(Qt::Vertical);
    18. splitter->addWidget(m_editor);
    19. splitter->addWidget(m_log);
    20. QList<int> sizes;
    21. sizes << 500 << 100;
    22. splitter->setSizes(sizes);
    23. setCentralWidget(splitter);
    24.  
    25. setupMenus();
    26.  
    27. }
    28. private:
    29. QTextEdit* m_editor;
    30. QTextBrowser* m_log;
    31. int msgCount;
    32. void setupMenus()
    33. {
    34. QMenu* fileMenu = menuBar()->addMenu(tr("&File"));
    35. fileMenu->addAction(tr("Open file"), this, SLOT(openFile()));
    36. fileMenu->addAction(tr("Exit"), this, SLOT(close()));
    37. }
    38.  
    39. public slots:
    40. void openFile()
    41. {
    42. m_log->append(QString::number(++msgCount) + ". choose file");
    43. QString filename = QFileDialog::getOpenFileName(this, tr("choose file"), QDir::homePath());
    44.  
    45. if (filename.isEmpty()) {
    46. m_log->append(QString::number(++msgCount) + ". no file selected");
    47. return;
    48. }
    49.  
    50. m_log->append(QString::number(++msgCount) + ". filename:" + filename);
    51. QFile f(filename);
    52. if (!f.open(QIODevice::Text | QIODevice::ReadWrite)) {
    53. m_log->append(QString::number(++msgCount) + ". error opening file:");
    54. m_log->append( "* " + f.errorString());
    55. return;
    56. }
    57.  
    58. m_editor->setText(f.readAll());
    59. f.close();
    60. }
    61. };
    62.  
    63. #include "main.moc"
    64. int main(int argc, char* argv[])
    65. {
    66. QApplication app(argc, argv);
    67. MainWindow mw;
    68. mw.show();
    69. return app.exec();
    70. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by momesana; 7th November 2007 at 00:55.

  9. #9
    Join Date
    Jan 2007
    Posts
    326
    Thanks
    42
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    MacOS X

    Default Re: How to open a file in Read Write mode

    Thanx for the reply

    Actually i am not opening any directory or file , I am trying to open the drivename in read /write mode.

    I used all the commands like "chmod 775" "ls-l" etc. but nothing works.

    Is I need some special permissions to open " a drivename" in read/write mode.

    regards
    merry
    Always Believe in Urself
    Merry

  10. #10
    Join Date
    Jan 2006
    Posts
    14
    Thanked 2 Times in 2 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to open a file in Read Write mode

    Well, I hope you are not trying to open "c:\" using QFile, because I doubt it will succeed ;-)
    What do you mean by "drivename"? QFile opens _files_, so if you mean a device file as /dev/sda0 on Unix/Linux you are ok - as long as you have proper permissions.

    Either check the permissions using "ls -l" or Konqueror/Nautilus/whatever file manager, or use the QFile::error() return value to know what error occurred.

    Or, and this would be the best at this point, post the code snipped or some example you are using.

    IOW: to open a _file_ in r/w mode use QFile:pen(QIODevice::ReadWrite). And stop.
    There's noting else you can do in Qt. If you don't have proper permissions then you don't. It's not about Qt but about your operating system.

    If you need to open a directory use QDir. But then you don't need to concern about open modes.

  11. #11
    Join Date
    Nov 2007
    Posts
    5
    Thanks
    1
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: How to open a file in Read Write mode

    Are you trying to mount the drive, or just access it? OS X generally automounts so you should only be trying to open a folder (the mount point) or are you trying to work directly on a device to manipulate raw data (ie/ opening /dev/disk0 )? Is the drive a networked resource?

  12. #12
    Join Date
    Jan 2007
    Posts
    326
    Thanks
    42
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    MacOS X

    Default Re: How to open a file in Read Write mode

    Hi

    Thanx 4 d reply.

    Actually i am trying to open Ipod's hard drive in read/write mode , I am trying to open raw file (i.e /dev/rdisk2) using filedescriptor. Its not opening the drive and filedescriptor returns -1 value.

    I dont understand what to do and how to open the file in read/write mode..


    Pls help

    Regards
    Merry
    Always Believe in Urself
    Merry

  13. #13
    Join Date
    Jan 2007
    Posts
    326
    Thanks
    42
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    MacOS X

    Default Re: How to open a file in Read Write mode

    Ih any body knows then
    Please help ..........
    Always Believe in Urself
    Merry

  14. #14
    Join Date
    Jan 2006
    Posts
    14
    Thanked 2 Times in 2 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to open a file in Read Write mode

    First off, I don't have an ipod and I cant remember any distro creating a /dev/rdisk2 device, so I will assume /dev/rdisk2 exists and it points to your ipod. If it does not, it's not a Qt issue ;-)

    Usually, to access a device file (/dev/something) you will need to be root or to set the proper permissions for that file.

    I will assume that you have read/write privileges. If you don't know, open a shell/terminal and run the command "ls -l /dev/rdisk2". What is the output?

    Now you can either use QFile directly:

    Qt Code:
    1. QFile file("/dev/rdisk2");
    2. if (!file.open(QIODevice::ReadWrite)) {
    3. QMessageBox::warning(this, "Error", QString("Failed to open iPod device. Reason: ").append(file.errorString()));
    4. }
    To copy to clipboard, switch view to plain text mode 

    Or you can use a C file descriptor:

    Qt Code:
    1. FILE* fd = fopen("/dev/rdisk2"..... // get a file descriptor somehow
    2. QFile file;
    3. file.open(fd, QIODevice::ReadWrite); // or use an INT file descriptor
    4. if (!file.open(QIODevice::ReadWrite)) {
    5. QMessageBox::warning(this, "Error", QString("Failed to open iPod device. Reason: ").append(file.errorString()));
    To copy to clipboard, switch view to plain text mode 

    If this does not help, please post the code snippet you are using or we won't be able to help you.

Similar Threads

  1. How can I Read Write Excel File
    By xingshaoyong in forum Qt Programming
    Replies: 6
    Last Post: 13th July 2011, 20:16
  2. Read \Write Excel File using Qt
    By xingshaoyong in forum Qt Programming
    Replies: 4
    Last Post: 27th July 2007, 22:07
  3. FSWriteFork in MAC OS to write data to a file.
    By vishal.chauhan in forum General Programming
    Replies: 5
    Last Post: 2nd July 2007, 06:48
  4. qt-3.3.8 fail in scratchbox
    By nass in forum Installation and Deployment
    Replies: 0
    Last Post: 25th May 2007, 15:21
  5. How to open a Pdf file from Qt
    By vishal.chauhan in forum Newbie
    Replies: 2
    Last Post: 28th March 2007, 08:24

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.