Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Cannot create directory and write text file



  • 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
    @


  • Lifetime Qt Champion

    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 ?



  • 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]



  • [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!



  • 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


  • Lifetime Qt Champion

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


Log in to reply