[SOLVED] QFile doesn't create the file
-
wrote on 9 Jun 2013, 12:57 last edited by
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(appendAll); if(myFile.open(QIODevice::WriteOnly | QIODevice::Text )) { output << "File Has Been Created" << endl; } else { output << "Failed to Create File" << endl; } QTextStream writeToFile(&myFile); writeToFile << "Hello World" << endl; myFile.close(); return a.exec();
}
@ -
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.
-
wrote on 9 Jun 2013, 17:36 last edited by
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(appendAll);
@
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("/home/mahmoud/Desktop/mahmoud.txt");
@
This one works fine and it creates the file I want.
-
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.
-
wrote on 9 Jun 2013, 18:39 last edited by
Hi, I have found the problem, Line 13 in the code I have posted, a typing error, I wrote Destkop instead of Desktop.
-
Haha, well spotted! Happy coding!
5/6