PDA

View Full Version : file.open error



pheonixstorm
2nd July 2011, 04:42
Working on a new program to make backups of certain files but it will not open those files. The program starts by reading in a text file with a list of files to backup to the applications directory. The text file loads correctly when the application starts, but when i press on the backup button it gives me an error message: The filename, directory name, or volume label syntax is incorrect.

When I first started creating this app it would only read and copy one out of three files I was using for testing. After changing a few things around to attempt to fix the issue it wont even copy a single file. It is rather confusing on why one instance of QFile works while the other doesn't. I have tried changing the information in the text file from C:/test/filename to C:\test\file C:\\test\\file C://test//file and I get the same error.


void SunBak::onBackup()
{
while (it != FBak.end())
{
// Grab file
QByteArray data;
QString filename = it->FullPath();
QMessageBox::information(this, tr("Backing up file."),
tr("%1") .arg(filename));
QFile file(filename);
if (!file.open(QIODevice::ReadOnly))
{
QMessageBox::information(this, tr("Unable to open file"), file.errorString());
return;
}
data = file.readAll();
if (data.isEmpty()){ QMessageBox::warning(this, tr("Not reading data!"), tr("What the hell?")); }
file.close();

filename = it->getFile();
QMessageBox::information(this, tr("Test:"), tr("%1") .arg(filename));
QFile fileSave(filename);
//file.open(QIODevice::WriteOnly);
if (!fileSave.open(QIODevice::WriteOnly))
{
QMessageBox::information(this, tr("Unable to open file"), fileSave.errorString());
// return;
}
if (fileSave.write(data) == -1)
{
QMessageBox::critical(this, tr("Error!"), tr("File %1 not written!") .arg(filename));
}
it++;
}
}

mvuori
2nd July 2011, 09:15
It would be helpfull to show where in the code the error messages are produced and describe the variable "it" and what "it" is...

I'm making a guess: when the filenames are lines of a text file, they include the end-of-line character and thus are incorrect.

SixDegrees
2nd July 2011, 11:59
Near the top of your loop, you say


QString filename = it->FullPath();

Then, near the bottom, you say


filename = it->getFile();

I have no idea what 'it' is, but assuming it's an iterator I'll note that it is never changed between these two statements, so you are potentially attempting to read and write to the same file. Maybe it->fullPath() and it->getFile() are sufficiently different for this to work, but it certainly isn't clear without knowing more about what 'it' points to.

pheonixstorm
2nd July 2011, 20:53
the iterator doesnt mater, the filename doesnt seem to matter.. and as far as the error try looking at this line again....

QFile file(filename);
if (!file.open(QIODevice::ReadOnly))
{
QMessageBox::information(this, tr("Unable to open file"), file.errorString());
return;
}


when the app starts it calls load() to grab the list file for the filename and location. This file loads fine and I have no problems with it, its not until I try to load the other files to back them up that the problem shows up. The vector holds the filename and location. it->fullpath is the full filepath and filename, this can be swapped out for the actual path and not from the vector and it gives the same result... The vector is fine.. there is nothing wrong with the vector or iterator so those should be ignored.

If you reread the code you will see the file.errorstring.. that is where the message comes from. While reading the code look at ALL the QMessagebox statements. Each time something gets started a messagebox should pop up and tell me whats about to happen. In the code posted in this thread the if has a return, so the remaining code should be ignored. The entire function was posted to see how it is supposed to work.

Added after 5 minutes:



I have no idea what 'it' is, but assuming it's an iterator I'll note that it is never changed between these two statements, so you are potentially attempting to read and write to the same file. Maybe it->fullPath() and it->getFile() are sufficiently different for this to work, but it certainly isn't clear without knowing more about what 'it' points to.

Yes it is the same file. fullpath is the location of the original while getfile only used the filename itself so the file gets saved in the applications directory. Currently I am using C:\test as the file locationd and C:\test2 as the application directory. So the app will load file xxx.whatever from C:\test and save it to C:\test2 Eventually I will compress the files and save all of them in something like xxx.bak but for now I need the initial file.open to work...

SixDegrees
2nd July 2011, 21:48
Then your problem lies elsewhere than in the code you've fixated on. Run your program in a debugger.

pheonixstorm
3rd July 2011, 05:20
Nothing to be found in the debugger either.

pheonixstorm
5th July 2011, 18:28
No ideas why file.open would fail to open a file? Has no one encountered a problem like this before?

majorwoody
5th July 2011, 20:52
...
filename = it->getFile();
QMessageBox::information(this, tr("Test:"), tr("%1") .arg(filename));
QFile fileSave(filename);
//file.open(QIODevice::WriteOnly);
if (!fileSave.open(QIODevice::WriteOnly))
{
QMessageBox::information(this, tr("Unable to open file"), fileSave.errorString());
// return;
}
...

What is the output of the MessageBox with 'Test:'? Or what is in filename at that time?

pheonixstorm
5th July 2011, 21:12
for testing I was using a small savegame file, so the filename was either C:/test/flag1.gam (during the first file.open) or flag1.gam during the above. But, as the first file.open fails and the program does its return this section of code does not get executed.

The entire function is supposed to read in the file to be backed up then save the file in the same directory as the application. The problem is when file.open is called the first time and fails it pops up the messagebox spits out: The filename, directory name, or volume label syntax is incorrect and then returns to the apps starting point. It is even more frustrating that for awhile I had to making a backup of 1 file out of three though this hasn't even happened in awhile... and another program I wrote has no issues reading in a file (not to mention the function call to load() in this app that has to load the filelist.txt).

Once I get my other computer back up and running I plan on trying to test the app on it.. maybe its an issue with the OS or something with the hardware, but for now I have nothing to go on.

majorwoody
5th July 2011, 21:29
And what does filename contain in this line? You could write small test program, where you only open the file with several path notations - I'm sure it has something to do with the return value of it->FullPath()

pheonixstorm
6th July 2011, 00:43
->FullPath() puts the location and filename together to go into filename in the first block. But, it doesn't seem to matter if I do fielname = "C:/test/flag1.gam" or filename = it->FullPath() it still gives the same error returned by file.errorstring, it also doesnt seem to matter if the application is loaded onto my usb drive (H) or on the primary drive C

As I said though, when I do filename = filelist.txt then file.open etc during the load function there are no errors. It only happens during the backup function (I haven't written the restore function yet). I think i'm going to create a new app w/o the load() function and just have the files as const strings and see if that changes anything.. probably not but who knows... it's really driving me nuts how it works once but not again or once and then only for the last file from the list...

ChrisW67
6th July 2011, 01:16
No ideas why file.open would fail to open a file? Has no one encountered a problem like this before?

People have plenty of ideas and have expressed them in this thread. Here is a summary of the usual suspects:

QFile::open() will fail to open a file for reading because;

the file does not exist (often because you are using a relative file name and assuming the current working directory is what you think it is), or
because you have no rights to open the file.

QFile::open() will fail to open a file for writing because;

you have no rights to write in the location specified,
a file already exists there but is read-only,
the location specified makes no sense (i.e. trying to save to /foo/bar when /foo does not exist).


You have more-or-less refused to consider the possibility that your code is feeding dud file names QFile through whatever it is that "it" points at. There's not much more we can add.

majorwoody
6th July 2011, 07:36
->FullPath() puts the location and filename together to go into filename in the first block. But, it doesn't seem to matter if I do fielname = "C:/test/flag1.gam" or filename = it->FullPath() it still gives the same error returned by file.errorstring, it also doesnt seem to matter if the application is loaded onto my usb drive (H) or on the primary drive C

And you tested all possibilities for the path as constant String?(like c:\test\flag1.gam and c:\\test\\flag1.gam ...)



it's really driving me nuts how it works once but not again or once and then only for the last file from the list...

That sounds extremely as if there are some cr/lf characters in the filename as considered by mvuori. First the last line worked, because there was no linebreak after the last line and now there is one.

pheonixstorm
7th July 2011, 17:37
People have plenty of ideas and have expressed them in this thread. Here is a summary of the usual suspects:

QFile::open() will fail to open a file for reading because;

the file does not exist (often because you are using a relative file name and assuming the current working directory is what you think it is), or
because you have no rights to open the file.

QFile::open() will fail to open a file for writing because;

you have no rights to write in the location specified,
a file already exists there but is read-only,
the location specified makes no sense (i.e. trying to save to /foo/bar when /foo does not exist).
This is actually the problem, though it isn't as clear cut as you would think. I have tried both relative and absolute path but neither work... As I stated in a post above (toward the beginning i think) no path I have tried has worked weather its using \ \\ / or //


You have more-or-less refused to consider the possibility that your code is feeding dud file names QFile through whatever it is that "it" points at. There's not much more we can add. And I have stated also that I have tried both via filelist.txt as well as directly naming the file. Neither works. At this point i'm beginning to wonder if something may have been corrupted in the OS or my installation of QT.


And you tested all possibilities for the path as constant String?(like c:\test\flag1.gam and c:\\test\\flag1.gam ...)

That sounds extremely as if there are some cr/lf characters in the filename as considered by mvuori. First the last line worked, because there was no linebreak after the last line and now there is one.
Yes, i've tried it all and no its not pulling in linefeeds or if it is they are not the problem as I can do filename = "flag1.gam" and still get the same problem.

As soon as I encountered the problem I setup multiple QMessagebox in the code to show what was about to happen with both sections of file.open. In every one the messagebox spit out exactly what it should have yet I still got that frustrating error message. Ive even tried to find anything related to UAC (user access control) to see if that could be a cause but have yet to find anything even remotely close.

ChrisW67
9th July 2011, 05:12
Build this:


#include <QtCore>
#include <QDebug>

int main(int argc, char *argv[])
{
QCoreApplication app(argc, argv);

QFile in("C:/windows/system32/user32.dll");
QFile out(QDir::tempPath() + "/user32.dll");
if (in.open(QIODevice::ReadOnly)) {
if (out.open(QIODevice::WriteOnly)) {
QByteArray data = in.readAll();
qint64 written = out.write(data);
qDebug() << "Read:" << in.size() << "Wrote:" << written;
out.close();
}
else {
qDebug() << "Cannot open output file" << out.errorString();
}
}
else {
qDebug() << "Cannot open input file" << in.errorString();
}
return 0;
}


Run it. Tell us which bit doesn't work.