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. [SOLVED] QFile doesn't create the file
QtWS25 Last Chance

[SOLVED] QFile doesn't create the file

Scheduled Pinned Locked Moved General and Desktop
6 Posts 2 Posters 4.1k Views
  • 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.
  • M Offline
    M Offline
    mahmoud899
    wrote on last edited by
    #1

    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;;
    

    }
    @

    Mahmoud Ramy

    1 Reply Last reply
    0
    • sierdzioS Offline
      sierdzioS Offline
      sierdzio
      Moderators
      wrote on last edited by
      #2

      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.

      (Z(:^

      1 Reply Last reply
      0
      • M Offline
        M Offline
        mahmoud899
        wrote on last edited by
        #3

        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.

        Mahmoud Ramy

        1 Reply Last reply
        0
        • sierdzioS Offline
          sierdzioS Offline
          sierdzio
          Moderators
          wrote on last edited by
          #4

          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.

          (Z(:^

          1 Reply Last reply
          0
          • M Offline
            M Offline
            mahmoud899
            wrote on last edited by
            #5

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

            Mahmoud Ramy

            1 Reply Last reply
            0
            • sierdzioS Offline
              sierdzioS Offline
              sierdzio
              Moderators
              wrote on last edited by
              #6

              Haha, well spotted! Happy coding!

              (Z(:^

              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