Infinite while loop while renaming file(Qt C++) -
i've made if else structure checks if file exist or not. if should go while loop , add integer , check again if file exist. should work same broswer of windows "filename (1).ext"
. have following code:
qfile file("messages/" + name + ".txt"); if(!file.exists()) { file.open(qiodevice::writeonly | qiodevice::text); qdebug() << "file doesn't exist, saving complete"; qtextstream message_file(&file); message_file << body; file.close(); qmessagebox::about(0,"succes!","uw bericht opgeslagen en verstuurd!"); } else { int n = 0; qdebug() << "file exist"; while(file.exists()){ n++; qstring nstring = qstring::number(n); qdebug() << "file exists sequence: " + nstring; qfile file("messages/" + name + " (" + nstring +").txt"); } file.open(qiodevice::writeonly | qiodevice::text); qtextstream message_file(&file); message_file << body; file.close(); qmessagebox::about(0,"succes!","uw bericht opgeslagen en verstuurd!"); }
but code overwrites original file, original file name (of course). seems doesnt change path of qfile file
. qdebug()
uptil infinite:
file exists sequence: 1 file exists sequence: 2 on..
what did wrong?
i think problem inside while loop creating local variable named file, doesn't change file you're calling exists() on.
int n = 1; while (n) int n = 0;
will never terminate, needs be:
int n = 1; while (n) n = 0;
Comments
Post a Comment