[Solved] Problem with creating new File!!!
-
Hello everyone,
It is almost about 2 days that I tried to create new text file in my project but it does not work!! I would like to create a textfile in my current project, which already is not existed.
Here is my code:
@
QFile file;
file.setFileName(file_name +".txt");//file_name is the QString, which I get as aparameter
file.open(QIODevice::ReadWrite | QIODevice::Text);
QTextStream stream(&file);
stream<<"Create Document";
QTextStream(stdout) << this->file_name; //To see the name of the file, which has just been typed
file.close();
@I would be happy if someone could give me the tip.
ps: My operating system is Linux.
-
@QString fileName = QFileDialog(this,Qt::Widget).getSaveFileName(this,"Salvar Arquivo Txt",getenv("HOME"),"Arquivos de Texto(*.txt)");
QFile arquivo(fileName);
arquivo.open(QIODevice::WriteOnly | QIODevice::Text);
arquivo.write(textoQueEuQueroSalvar.toLatin());
arquivo.close();@ -
Did you check if the file is created but in another directory than the one you expect it to be? Try passing an absolute path as parameter.
-
On second thought add that , to check where do you actually save
@QFileInfo fi(file);
QTextStream(stdout)<<fi.absoluteFilePath();@ -
try use that to
@QString filename = QFileDialog::getSaveFileName(this,"Salvar relatório ...",diretorio+"\Desktop","Arquivos Pdf(*.pdf)");@ -
suggestion, check the return from your QFile::open(). That can tell you if your file actually opened correctly...
@
QFile file(QString(file_name).append(".txt"));
qDebug() << "File location: " << QString(file_name).append(".txt");
if (!file.open(QIODevice::ReadWrite | QIODevice::Text)){
qDebug() << "File open failed";
return -1;
}
QTextStream stream(&file);
stream << "Create Document";
QTextStream(stdout) << this->file_name; //To see the name of the file, which has just been typed
file.close();
return 0;
@