Results 1 to 15 of 15

Thread: file.open error

  1. #1
    Join Date
    Jan 2011
    Posts
    12
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Exclamation file.open error

    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.

    Qt Code:
    1. void SunBak::onBackup()
    2. {
    3. while (it != FBak.end())
    4. {
    5. // Grab file
    6. QByteArray data;
    7. QString filename = it->FullPath();
    8. QMessageBox::information(this, tr("Backing up file."),
    9. tr("%1") .arg(filename));
    10. QFile file(filename);
    11. if (!file.open(QIODevice::ReadOnly))
    12. {
    13. QMessageBox::information(this, tr("Unable to open file"), file.errorString());
    14. return;
    15. }
    16. data = file.readAll();
    17. if (data.isEmpty()){ QMessageBox::warning(this, tr("Not reading data!"), tr("What the hell?")); }
    18. file.close();
    19.  
    20. filename = it->getFile();
    21. QMessageBox::information(this, tr("Test:"), tr("%1") .arg(filename));
    22. QFile fileSave(filename);
    23. //file.open(QIODevice::WriteOnly);
    24. if (!fileSave.open(QIODevice::WriteOnly))
    25. {
    26. QMessageBox::information(this, tr("Unable to open file"), fileSave.errorString());
    27. // return;
    28. }
    29. if (fileSave.write(data) == -1)
    30. {
    31. QMessageBox::critical(this, tr("Error!"), tr("File %1 not written!") .arg(filename));
    32. }
    33. it++;
    34. }
    35. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    May 2011
    Posts
    239
    Thanks
    4
    Thanked 35 Times in 35 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Symbian S60

    Default Re: file.open error

    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.

  3. #3
    Join Date
    Apr 2010
    Posts
    769
    Thanks
    1
    Thanked 94 Times in 86 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: file.open error

    Near the top of your loop, you say

    Qt Code:
    1. QString filename = it->FullPath();
    To copy to clipboard, switch view to plain text mode 

    Then, near the bottom, you say

    Qt Code:
    1. filename = it->getFile();
    To copy to clipboard, switch view to plain text mode 

    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.

  4. #4
    Join Date
    Jan 2011
    Posts
    12
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: file.open error

    the iterator doesnt mater, the filename doesnt seem to matter.. and as far as the error try looking at this line again....
    Qt Code:
    1. QFile file(filename);
    2. if (!file.open(QIODevice::ReadOnly))
    3. {
    4. QMessageBox::information(this, tr("Unable to open file"), file.errorString());
    5. return;
    6. }
    To copy to clipboard, switch view to plain text mode 

    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:


    Quote Originally Posted by SixDegrees View Post
    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...
    Last edited by pheonixstorm; 2nd July 2011 at 20:53.

  5. #5
    Join Date
    Apr 2010
    Posts
    769
    Thanks
    1
    Thanked 94 Times in 86 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: file.open error

    Then your problem lies elsewhere than in the code you've fixated on. Run your program in a debugger.

  6. #6
    Join Date
    Jan 2011
    Posts
    12
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: file.open error

    Nothing to be found in the debugger either.

  7. #7
    Join Date
    Jan 2011
    Posts
    12
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: file.open error

    No ideas why file.open would fail to open a file? Has no one encountered a problem like this before?

  8. #8
    Join Date
    Feb 2010
    Posts
    18
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Unhappy Re: file.open error

    Quote Originally Posted by pheonixstorm View Post
    Qt Code:
    1. ...
    2. filename = it->getFile();
    3. QMessageBox::information(this, tr("Test:"), tr("%1") .arg(filename));
    4. QFile fileSave(filename);
    5. //file.open(QIODevice::WriteOnly);
    6. if (!fileSave.open(QIODevice::WriteOnly))
    7. {
    8. QMessageBox::information(this, tr("Unable to open file"), fileSave.errorString());
    9. // return;
    10. }
    11. ...
    To copy to clipboard, switch view to plain text mode 
    What is the output of the MessageBox with 'Test:'? Or what is in filename at that time?

  9. #9
    Join Date
    Jan 2011
    Posts
    12
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: file.open error

    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.

  10. #10
    Join Date
    Feb 2010
    Posts
    18
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: file.open error

    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()

  11. #11
    Join Date
    Jan 2011
    Posts
    12
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: file.open error

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

  12. #12
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: file.open error

    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.

  13. #13
    Join Date
    Feb 2010
    Posts
    18
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: file.open error

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

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

  14. #14
    Join Date
    Jan 2011
    Posts
    12
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: file.open error

    Quote Originally Posted by ChrisW67 View Post
    People have plenty of ideas and have expressed them in this thread. Here is a summary of the usual suspects:

    QFile:pen() 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:pen() 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.

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

  15. #15
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: file.open error

    Build this:
    Qt Code:
    1. #include <QtCore>
    2. #include <QDebug>
    3.  
    4. int main(int argc, char *argv[])
    5. {
    6. QCoreApplication app(argc, argv);
    7.  
    8. QFile in("C:/windows/system32/user32.dll");
    9. QFile out(QDir::tempPath() + "/user32.dll");
    10. if (in.open(QIODevice::ReadOnly)) {
    11. if (out.open(QIODevice::WriteOnly)) {
    12. QByteArray data = in.readAll();
    13. qint64 written = out.write(data);
    14. qDebug() << "Read:" << in.size() << "Wrote:" << written;
    15. out.close();
    16. }
    17. else {
    18. qDebug() << "Cannot open output file" << out.errorString();
    19. }
    20. }
    21. else {
    22. qDebug() << "Cannot open input file" << in.errorString();
    23. }
    24. return 0;
    25. }
    To copy to clipboard, switch view to plain text mode 

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

Similar Threads

  1. fatal error LNK1104: cannot open file 'libgles_cm.lib
    By danmaliki in forum Installation and Deployment
    Replies: 2
    Last Post: 24th March 2011, 13:17
  2. Replies: 3
    Last Post: 1st November 2010, 16:33
  3. Replies: 0
    Last Post: 3rd June 2010, 04:54
  4. Replies: 4
    Last Post: 9th May 2010, 16:18
  5. Qt 4.1.4 on VS2005 error- cannot open input file 'qtmain.lib'
    By Ashish in forum Installation and Deployment
    Replies: 10
    Last Post: 11th October 2006, 16:05

Tags for this Thread

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.