QFile can not open file ,WHY???
I want to save something in local directory at MyDocs/
This is my code,the first code below is correct and add and create txt and content to it.
However the second code went wrong.
[CODE]QDir::setCurrent("/home/user/MyDocs/Documents/"); //Go to this directory
QFile file("lovenote_exp.txt"); //open file,all done correctly
if (!file.open(QIODevice::WriteOnly|QIODevice::Append)) {
qDebug()<< "Cannot open file for writing: " ;
return;
}
QTextStream out(&file);
out<<"\t"<<date<<"\t"<<title<<endl<<"\t"<<text<<endl;
file.close();[/CODE]
The below is the second code, it went wrong.
[CODE]QDir::setCurrent("/home/user/MyDocs/Documents/"); //Go to this directory
QString myfilename="myfile.txt"; // create a QString variant with this value.
QFile file(myfilename); // this is the difference,it went wrong there
if (!file.open(QIODevice::WriteOnly|QIODevice::Append)) {
qDebug()<< "Cannot open file for writing: " ;
return;
}
QTextStream out(&file);
out<<"\t"<<date<<"\t"<<title<<endl<<"\t"<<text<<endl;
file.close();[/CODE]
To be truth,i really dont know what went wrong ....But the second codes still didnt work .Anyone could help me???
It makes me confused.
Help!!!
Re: QFile can not open file ,WHY???
Does the file exist in /home/user/MyDocs/Documents/?
It clearly says you open for writeonly + append; but you do not give details on how to behave if the file does *not* exist...
[QUOTE=rhythmkay;905973]I want to save something in local directory at MyDocs/
This is my code,the first code below is correct and add and create txt and content to it.
However the second code went wrong.
[CODE]QDir::setCurrent("/home/user/MyDocs/Documents/"); //Go to this directory
QFile file("lovenote_exp.txt"); //open file,all done correctly
if (!file.open(QIODevice::WriteOnly|QIODevice::Append)) {
qDebug()<< "Cannot open file for writing: " ;
return;
}
QTextStream out(&file);
out<<"\t"<<date<<"\t"<<title<<endl<<"\t"<<text<<endl;
file.close();[/CODE]
The below is the second code, it went wrong.
[CODE]QDir::setCurrent("/home/user/MyDocs/Documents/"); //Go to this directory
QString myfilename="myfile.txt"; // create a QString variant with this value.
QFile file(myfilename); // this is the difference,it went wrong there
if (!file.open(QIODevice::WriteOnly|QIODevice::Append)) {
qDebug()<< "Cannot open file for writing: " ;
return;
}
QTextStream out(&file);
out<<"\t"<<date<<"\t"<<title<<endl<<"\t"<<text<<endl;
file.close();[/CODE]
To be truth,i really dont know what went wrong ....But the second codes still didnt work .Anyone could help me???
It makes me confused.
Help!!![/QUOTE]