PDA

View Full Version : How to open a file in Read Write mode



merry
5th November 2007, 10:03
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

ChristianEhrlicher
5th November 2007, 10:12
Did you check if the file really exists/ you have to proper rights to open it in write mode? Don't think so...

merry
5th November 2007, 10:22
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.

merry
5th November 2007, 11:23
pls tell me how to get permissions for opening a file in read/write mode.

regards
merry

momesana
5th November 2007, 12:56
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.

merry
6th November 2007, 12:12
Hi

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

regards

merry

blue.death
6th November 2007, 15:35
Try to use this method after opening the file:


FileError QFile::error () const

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.

momesana
7th November 2007, 01:01
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.



#include <QApplication>
#include <QtGui>

class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow(QWidget* parent=0) : QMainWindow(parent),
msgCount(0)
{
resize(800, 600);
m_editor = new QTextEdit;
m_log = new QTextBrowser;
m_log->setStyleSheet("background-color:rgb(20,40,60); color:white;");

QSplitter* splitter = new QSplitter;
splitter->setOrientation(Qt::Vertical);
splitter->addWidget(m_editor);
splitter->addWidget(m_log);
QList<int> sizes;
sizes << 500 << 100;
splitter->setSizes(sizes);
setCentralWidget(splitter);

setupMenus();

}
private:
QTextEdit* m_editor;
QTextBrowser* m_log;
int msgCount;
void setupMenus()
{
QMenu* fileMenu = menuBar()->addMenu(tr("&File"));
fileMenu->addAction(tr("Open file"), this, SLOT(openFile()));
fileMenu->addAction(tr("Exit"), this, SLOT(close()));
}

public slots:
void openFile()
{
m_log->append(QString::number(++msgCount) + ". choose file");
QString filename = QFileDialog::getOpenFileName(this, tr("choose file"), QDir::homePath());

if (filename.isEmpty()) {
m_log->append(QString::number(++msgCount) + ". no file selected");
return;
}

m_log->append(QString::number(++msgCount) + ". filename:" + filename);
QFile f(filename);
if (!f.open(QIODevice::Text | QIODevice::ReadWrite)) {
m_log->append(QString::number(++msgCount) + ". error opening file:");
m_log->append( "* " + f.errorString());
return;
}

m_editor->setText(f.readAll());
f.close();
}
};

#include "main.moc"
int main(int argc, char* argv[])
{
QApplication app(argc, argv);
MainWindow mw;
mw.show();
return app.exec();
}

merry
7th November 2007, 06:03
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

blue.death
7th November 2007, 10:57
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::open(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.

natbobc
7th November 2007, 21:03
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?

merry
8th November 2007, 07:34
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

merry
12th November 2007, 06:30
Ih any body knows then
Please help ..........:(

blue.death
16th November 2007, 15:40
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:



QFile file("/dev/rdisk2");
if (!file.open(QIODevice::ReadWrite)) {
QMessageBox::warning(this, "Error", QString("Failed to open iPod device. Reason: ").append(file.errorString()));
}


Or you can use a C file descriptor:



FILE* fd = fopen("/dev/rdisk2"..... // get a file descriptor somehow
QFile file;
file.open(fd, QIODevice::ReadWrite); // or use an INT file descriptor
if (!file.open(QIODevice::ReadWrite)) {
QMessageBox::warning(this, "Error", QString("Failed to open iPod device. Reason: ").append(file.errorString()));


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