[SOLVED] QFile doesn't create the file



  • Hello, I am writing a simple program. What I want to do is append two QStrings together and send them to the constructor of the QFile variable. Before I do that I check one of the QStrings if it has "/" at the end, if it doesn't I append it to it. Then I append the two strings together and send them to the constructor of the QFile variable. When I run it prints out that if failed to create the file, but when I type the string itself in the constructor of the QFile variable "QFile myFile("/home/mahmoud/Desktop/mahmoud.txt");" it creates the file. I want to take QStrings from the user and send them to the constructor, not type the the String itself in the constructor. How can I solve that problem.

    @
    #include <QApplication>
    #include <QString>
    #include <QTextStream>
    #include <QFile>

    int main(int argc, char *argv[])
    {
    QApplication a(argc, argv);

    QTextStream output(stdout);
    
    QString location = "/home/mahmoud/Destkop";
    QString name = "mahmoud.txt";
    QString appendAll;
    QString fileName;
    
    if( !location.endsWith("/") )
    {
        location.append("/");
    }
    
    appendAll = location.append(name);
    
    output << appendAll << endl;
    
    QFile myFile&#40;appendAll&#41;;
    
    if(myFile.open(QIODevice::WriteOnly | QIODevice::Text &#41;)
    {
        output << "File Has Been Created" << endl;
    }
    else
    {
        output << "Failed to Create File" << endl;
    }
    
    QTextStream writeToFile&#40;&myFile&#41;;
    
    writeToFile << "Hello World" << endl;
    
    myFile.close(&#41;;
    
    return a.exec(&#41;;
    

    }
    @


  • Moderators

    You don't have any code that actually reads user input. If you want your app to be console-base, then simply use app arguments to get input from the user ("QCoreApplication::arguments()":http://qt-project.org/doc/qt-5.0/qtcore/qcoreapplication.html#arguments).

    Or make a GUI with some QLineEdits used to read user input.

    If you mean that you want to grab user input during runtime, you need to resort to standard C++ cin statement, I guess. IMO taking the arguments from arguments list is better.



  • Hello, what I've meant by taking the QStrings from the user is that I want to do that later on in another project, for now I am just having QStrings and appending them and sending them to the constructor of the QFile variable. My problem is when I append two QStrings and send them to the QFile variable constructor it doesn't work

    @
    QString location = "/home/mahmoud/Destkop";
    QString name = "mahmoud.txt";
    QString appendAll;
    QString fileName;

    if( !location.endsWith("/") )
    {
        location.append("/");
    }
    
    appendAll = location.append(name);
    
    output << appendAll << endl;
    
    QFile myFile&#40;appendAll&#41;;
    

    @

    but when I type the String myself in the QFile variable constructor it works

    @
    QString location = "/home/mahmoud/Destkop";
    QString name = "mahmoud.txt";
    QString appendAll;
    QString fileName;

    if( !location.endsWith("/") )
    {
        location.append("/");
    }
    
    appendAll = location.append(name);
    
    output << appendAll << endl;
    
    QFile myFile&#40;"/home/mahmoud/Desktop/mahmoud.txt"&#41;;
    

    @

    This one works fine and it creates the file I want.


  • Moderators

    Strange. What happens when you pass 'location' to Your QFile constructor? I'm writing from a cell phone and can't check myself. Line 11 looks a bit suspicious to me.



  • Hi, I have found the problem, Line 13 in the code I have posted, a typing error, I wrote Destkop instead of Desktop.


  • Moderators

    Haha, well spotted! Happy coding!


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.