QFile not opening file
-
Hey I'm making a multi file Find And Replace program. Users select one file after another and all the files they select get written into a QPlainTextEdit with a "---------------------------" between each file in the textedit to make it look separated. Now, I use QString::split() and write it all to a QStringList. Then, to access all of the elements of the QStringList, open them, and perform the text replacement, I use a For Loop.
However, for some reason, when I access the QStringList element with "file_list.at(x)", the file won't open, and this happens outside a For Loop too.
Here's a bit of the code :
@
file_list = textboxdata.split("------------------------------------");for (int i = 0; i < file_list.size(); i++) { file.setFileName(file_list.at(i)); if (!file.open(QIODevice::ReadWrite) || !isValidFile(file_list.at(i))) { QSound error("C://Windows//Media//Windows Critical Stop.wav"); error.play(); QMessageBox msg; msg.setText("File could not be opened."); msg.setWindowTitle("Error"); msg.exec(); return; }
@
I appreciate your help everyone :) Thanks!
-
The first step before asking in a forum would be to print out the file path you try to open:
add this before the file.setFilename call:
@
qDebug() << "trying to open file" << file_list.at(i);
@-end the I would add an else branch in case the file couldn't be opened:-
EDIT - sorry, the error handling is in the if-branch, of course, so add this in the if-branch:@
qDebug() << file.errorString();
@ -
Thanks Volker.
Here is my error string :
"The filename, directory name, or volume label syntax is incorrect."
and here is the file I'm trying to open :
""C:/Users/Wonopon/Desktop/finalfile.txt
"
The quotation appears on the next line for some reason, though I'm not sure if that's something I should be worried about. Thanks again. -
Is the whole contents of your QPlainTextEdit in one line?
As it looks it would for contents like:
@
c:/user/bla/a.txt------------------------------------c:/tmp/anothr.txt------------------------------------c:/etc.txt
@
You should be aware that a content with newlines as of
@
c:/user/bla/a.txtc:/tmp/anothr.txt
c:/etc.txt
@
will perform differently. Probably you should post also an example of the contents you are splitting. -
You have a newline character at the end of your path name, that's the reason why it is invalid and why the closing quotation mark is on a new line.
you should split on the dashes + newline character:
@
file_list = textboxdata.split("------------------------------------\n");
@Just some additional advice:
If your users don't need to edit the actual paths of files, then it would be much more appropriate to put the list into a [[Doc:QListWidget]] or [[Doc:QTreeWidget]] (only using toplevel items). -
quick fix could also be to string.remove(" ") or string.remove("\n") to get rid of any spaces or carriage returns while reading from the string.
Also, instead of using filename.at(i) you can access the StringList elements by just using filename[i] like normal array elements incase of need to do conversions and such.
so if you split on " " and remove "\n" you should get the filenames. Then print out the filenames using the above method (qDebug()) to make sure they are correct.
Cheers.