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. Cannot create directory and write text file
Forum Updated to NodeBB v4.3 + New Features

Cannot create directory and write text file

Scheduled Pinned Locked Moved General and Desktop
6 Posts 3 Posters 4.3k 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.
  • E Offline
    E Offline
    elijah_lau
    wrote on last edited by
    #1

    I have encounted the following problem. My system is window 8.

    1. To create a folder I always have to constant declare the folder name before created the folder. I cannot used the parameter QString to created the folder but when I used the folder_1 , the required folder is created.

    2. The following code always crash my program. I cannot find where the problem is. Hope somebody can help me.
      @
      QString LibFileIO::writeTextFile( QString folder, QString filename , QStringList TextContent ){
      bool no_error = true;

      // This will work. If I used the parameter folder it will not be created.
      QString folder_1 = "C:/wamp/www/test_ikeepintouch_com/laravel/app/views/hello";
      QDir dir( folder_1 );

      if( dir.exists() == false ){
      if( dir.mkdir( folder_1 ) == false ){
      qDebug() << " Directory cannot be created = " + folder_1;
      }else{
      qDebug() << " Directory created = " + folder_1;
      };
      }

      QFile file( folder_1 + "/" + filename );
      file.open( QIODevice::WriteOnly | QIODevice::Text );
      if( file.isOpen() == false ){
      qDebug() << " Error open file " + file.errorString();
      return file.errorString();
      }

      QTextStream out(&file);
      foreach( QString str , TextContent ){
      out << "HELLO";
      }

      qDebug() << " Closing" ;
      // optional, as QFile destructor will already do it:
      out.flush();
      file.close();

    }
    @

    @
    //=============================================================

    #ifndef LIBFILEIO_H
    #define LIBFILEIO_H

    #include <QObject>
    #include <QString>

    class LibFileIO : public QObject
    {
    Q_OBJECT
    public:
    explicit LibFileIO(QObject *parent = 0);
    static QString writeTextFile( QString folder, QString filename , QStringList TextContent );
    signals:

    public slots:

    };

    #endif // LIBFILEIO_H
    @

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

      Hi and welcome to devnet,

      Please enclose your code in coding tags (one @ at the beginning and one at the end) It will make your code readable.

      What error are you getting ? Did you run your application with a debugger to see where it crashes ?

      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
      • E Offline
        E Offline
        elijah_lau
        wrote on last edited by
        #3

        Please disregard this. Extra hidden space cause the problem.

        [quote author="elijah_lau" date="1389251019"]I have encounted the following problem. My system is window 8.

        1. To create a folder I always have to constant declare the folder name before created the folder. I cannot used the parameter QString to created the folder but when I used the folder_1 , the required folder is created.

        2. The following code always crash my program. I cannot find where the problem is. Hope somebody can help me.

        QString LibFileIO::writeTextFile( QString folder, QString filename , QStringList TextContent ){
        bool no_error = true;

        // This will work. If I used the parameter folder it will not be created.
        QString folder_1 = "C:/wamp/www/test_ikeepintouch_com/laravel/app/views/hello";
        QDir dir( folder_1 &#41;;
        
        if( dir.exists() == false ){
            if( dir.mkdir( folder_1 ) == false ){
                qDebug() << " Directory cannot be created = " + folder_1;
            }else{
                qDebug() << " Directory created = " + folder_1;
            };
        }
        
        QFile file&#40; folder_1 + "/" + filename &#41;;
        file.open( QIODevice::WriteOnly | QIODevice::Text &#41;;
        if( file.isOpen() == false ){
            qDebug() << " Error open file " + file.errorString();
            return file.errorString();
        }
        
        QTextStream out(&file);
        foreach( QString str , TextContent ){
           out << "HELLO";
        }
        
        qDebug() << " Closing" ;
        // optional, as QFile destructor will already do it:
        out.flush();
        file.close();
        

        }

        //=============================================================

        #ifndef LIBFILEIO_H
        #define LIBFILEIO_H

        #include <QObject>
        #include <QString>

        class LibFileIO : public QObject
        {
        Q_OBJECT
        public:
        explicit LibFileIO(QObject *parent = 0);
        static QString writeTextFile( QString folder, QString filename , QStringList TextContent );
        signals:

        public slots:

        };

        #endif // LIBFILEIO_H[/quote]

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

          [quote author="elijah_lau" date="1389255053"]Please disregard this. Extra hidden space cause the problem.

          [quote author="elijah_lau" date="1389251019"]I have encounted the following problem. My system is window 8.

          1. To create a folder I always have to constant declare the folder name before created the folder. I cannot used the parameter QString to created the folder but when I used the folder_1 , the required folder is created.

          2. The following code always crash my program. I cannot find where the problem is. Hope somebody can help me.

          QString LibFileIO::writeTextFile( QString folder, QString filename , QStringList TextContent ){
          bool no_error = true;

          // This will work. If I used the parameter folder it will not be created.
          QString folder_1 = "C:/wamp/www/test_ikeepintouch_com/laravel/app/views/hello";
          QDir dir( folder_1 &#41;;
          
          if( dir.exists(&#41; == false ){
              if( dir.mkdir( folder_1 ) == false ){
                  qDebug() << " Directory cannot be created = " + folder_1;
              }else{
                  qDebug() << " Directory created = " + folder_1;
              };
          }
          
          QFile file&#40; folder_1 + "/" + filename &#41;;
          file.open( QIODevice::WriteOnly | QIODevice::Text &#41;;
          if( file.isOpen(&#41; == false ){
              qDebug() << " Error open file " + file.errorString();
              return file.errorString();
          }
          
          QTextStream out(&file);
          foreach( QString str , TextContent ){
             out << "HELLO";
          }
          
          qDebug() << " Closing" ;
          // optional, as QFile destructor will already do it:
          out.flush();
          file.close();
          

          }

          //=============================================================

          #ifndef LIBFILEIO_H
          #define LIBFILEIO_H

          #include <QObject>
          #include <QString>

          class LibFileIO : public QObject
          {
          Q_OBJECT
          public:
          explicit LibFileIO(QObject *parent = 0);
          static QString writeTextFile( QString folder, QString filename , QStringList TextContent );
          signals:

          public slots:

          };

          #endif // LIBFILEIO_H[/quote]

          [/quote]

          SGaist had the problem to read properly your code, since the code tags were missing. AFAIK, he cannot add code tags and ask you to add them for better reading of your code section. Please refer to forum help on "how to format your code in forum posts. ":http://qt-project.org/wiki/ForumHelp#e3f82045ad0f480d3fb9e0ac2d58fb01

          Check out your original code post. I have added code tags for you. Compare to the quote of your second post showing the old mess which cannot be read easily.

          If you want help, help others to help you!

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

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

            Checkout the string declaring the folder
            @
            QString folder_1 = "C:/wamp/www/test_ikeepintouch_com/laravel/app/views/hello";
            @

            This looks fairly and you may miss a forward slash.

            mkdir does add only one subfolder at a time. So "C:/wamp/www/test_ikeepintouch_com/laravel/app/views" has to exist already and you can add the subfolder "hello".

            If for example "test_ikeepintouch_com" is not existing because of typing error, you are going to fail. If you want to create more than one subfolder at a time consider "mkpath":http://qt-project.org/doc/qt-5/qdir.html#mkpath

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

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

              .@ koahnig indeed I'm not big cheese enough to add them :)

              .@ elijah_lau are you saying that hidden space in your path variable caused the problem ?

              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

              • Login

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