Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QFile not opening file
Qt 6.11 is out! See what's new in the release blog

QFile not opening file

Scheduled Pinned Locked Moved General and Desktop
7 Posts 4 Posters 8.8k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • W Offline
    W Offline
    wonopon12
    wrote on last edited by
    #1

    Hey I'm making a multi file Find And Replace program. Users select one file after another and all the files they select get written into a QPlainTextEdit with a "---------------------------" between each file in the textedit to make it look separated. Now, I use QString::split() and write it all to a QStringList. Then, to access all of the elements of the QStringList, open them, and perform the text replacement, I use a For Loop.

    However, for some reason, when I access the QStringList element with "file_list.at(x)", the file won't open, and this happens outside a For Loop too.

    Here's a bit of the code :
    @
    file_list = textboxdata.split("------------------------------------");

        for (int i = 0; i < file_list.size(); i++)
        {
            file.setFileName(file_list.at(i));
    
            if (!file.open(QIODevice::ReadWrite) || !isValidFile&#40;file_list.at(i&#41;))
            {
                QSound error("C://Windows//Media//Windows Critical Stop.wav");
                error.play();
    
                QMessageBox msg;
                msg.setText("File could not be opened.");
                msg.setWindowTitle("Error");
                msg.exec&#40;&#41;;
    
                return;
            }
    

    @

    I appreciate your help everyone :) Thanks!

    1 Reply Last reply
    0
    • G Offline
      G Offline
      goetz
      wrote on last edited by
      #2

      The first step before asking in a forum would be to print out the file path you try to open:

      add this before the file.setFilename call:
      @
      qDebug() << "trying to open file" << file_list.at(i);
      @

      -end the I would add an else branch in case the file couldn't be opened:-
      EDIT - sorry, the error handling is in the if-branch, of course, so add this in the if-branch:

      @
      qDebug() << file.errorString();
      @

      http://www.catb.org/~esr/faqs/smart-questions.html

      1 Reply Last reply
      0
      • W Offline
        W Offline
        wonopon12
        wrote on last edited by
        #3

        Thanks Volker.

        Here is my error string :

        "The filename, directory name, or volume label syntax is incorrect."

        and here is the file I'm trying to open :

        ""C:/Users/Wonopon/Desktop/finalfile.txt
        "
        The quotation appears on the next line for some reason, though I'm not sure if that's something I should be worried about. Thanks again.

        1 Reply Last reply
        0
        • K Offline
          K Offline
          koahnig
          wrote on last edited by
          #4

          Is the whole contents of your QPlainTextEdit in one line?
          As it looks it would for contents like:
          @
          c:/user/bla/a.txt------------------------------------c:/tmp/anothr.txt------------------------------------c:/etc.txt
          @
          You should be aware that a content with newlines as of
          @
          c:/user/bla/a.txt

          c:/tmp/anothr.txt

          c:/etc.txt
          @
          will perform differently. Probably you should post also an example of the contents you are splitting.

          Vote the answer(s) that helped you to solve your issue(s)

          1 Reply Last reply
          0
          • G Offline
            G Offline
            goetz
            wrote on last edited by
            #5

            You have a newline character at the end of your path name, that's the reason why it is invalid and why the closing quotation mark is on a new line.

            you should split on the dashes + newline character:

            @
            file_list = textboxdata.split("------------------------------------\n");
            @

            Just some additional advice:
            If your users don't need to edit the actual paths of files, then it would be much more appropriate to put the list into a [[Doc:QListWidget]] or [[Doc:QTreeWidget]] (only using toplevel items).

            http://www.catb.org/~esr/faqs/smart-questions.html

            1 Reply Last reply
            0
            • V Offline
              V Offline
              vezprog
              wrote on last edited by
              #6

              quick fix could also be to string.remove(" ") or string.remove("\n") to get rid of any spaces or carriage returns while reading from the string.

              Also, instead of using filename.at(i) you can access the StringList elements by just using filename[i] like normal array elements incase of need to do conversions and such.

              so if you split on " " and remove "\n" you should get the filenames. Then print out the filenames using the above method (qDebug()) to make sure they are correct.

              Cheers.

              1 Reply Last reply
              0
              • W Offline
                W Offline
                wonopon12
                wrote on last edited by
                #7

                Sweet thanks everyone!

                1 Reply Last reply
                0

                • Login

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • Users
                • Groups
                • Search
                • Get Qt Extensions
                • Unsolved