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. Get an FileError(5) when i use QFile object to open a txt file at second time

Get an FileError(5) when i use QFile object to open a txt file at second time

Scheduled Pinned Locked Moved General and Desktop
9 Posts 2 Posters 2.9k 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.
  • A Offline
    A Offline
    andy65007
    wrote on last edited by
    #1

    QFile object returns error when i use it to open a txt file, (the errorString() returns: system cannot find specific path in chinese) ps: my code was copied from the qt document samples.

    Here is my code, hope someone can help me out!

    @
    //declare in header file CommonHelper.h
    static QString readTextFile(QString name);

    //implementation in CommonHelper.cpp file
    QString CommonHelper::readTextFile(QString name){
    QString data;
    QFile f(name);
    if(!f.open(QIODevice::ReadOnly | QIODevice::Text)){
    return data; //always get errors when call this function at second time!!(ps: the errorString() returns: system cannot find specific path in chinese)
    }
    QTextStream in(&f);
    data = in.readAll();
    f.close();

    return data;
    

    }@

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi and welcome to devnet,

      Are you reading the same file again ?

      If not, are you sure that the file given exists ?

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • A Offline
        A Offline
        andy65007
        wrote on last edited by
        #3

        yes, i'm reading the same file(it's a html template for printing), and i'm sure the file exists. in my program, click the button twince, it works just at first time click.

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          Then can you show the code where you use readTextFile ?

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          0
          • A Offline
            A Offline
            andy65007
            wrote on last edited by
            #5

            Below is the code used to call method readTextFile(QString str):

            @bool PrintableHistoryObject::printing(){
            //get template
            QString strTemplate = CommonHelper::readTextFile(AppDef::SYS_HISTORY_TEMPLATE_FILE_NAME);
            if(strTemplate.isEmpty()){
            return false; //the value of strTemplate will be set to empty string at second time call
            }
            //replace parameters
            strTemplate = strTemplate.replace("[seqNum]", seqNum.isEmpty()?"":seqNum);

            //other source codes was omitted

            }@

            1 Reply Last reply
            0
            • SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on last edited by
              #6

              what is SYS_HISTORY_TEMPLATE_FILE_NAME ?

              Interested in AI ? www.idiap.ch
              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

              1 Reply Last reply
              0
              • A Offline
                A Offline
                andy65007
                wrote on last edited by
                #7

                it's a static variable QString:

                //AppDef.h
                @static QString SYS_HISTORY_TEMPLATE_FILE_NAME;@

                //AppDef.cpp
                @QString AppDef::SYS_HISTORY_TEMPLATE_FILE_NAME = "data/historyTemplate.htm";@

                1 Reply Last reply
                0
                • SGaistS Offline
                  SGaistS Offline
                  SGaist
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

                  Using a relative path can be dangerous, if this file is always in the same place you should use an absolute path. You could be changing something somewhere in your software like the current working directory and then your file will not be found.

                  Interested in AI ? www.idiap.ch
                  Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                  1 Reply Last reply
                  0
                  • A Offline
                    A Offline
                    andy65007
                    wrote on last edited by
                    #9

                    understood. thank you very much! I'll try this in my program.

                    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