How to create in memory QFile
Hi,
I want to create a QFile that currently resides in memory. This is required as i want to add it as a resource to a QTextDocument.
On some user action i want to write the QTextDocument and in the same directory i also want the QFile created in memory to be written.
Please provide some pointers.
Re: How to create in memory QFile
Ok. For that, i think dont add the file in the resource folder,except that, directly create it in your project folder and access it from there. Because if you put your file in the resource and it will get compile and you'll not able to edit it. For that,
Code:
file=new QFile("demo.txt");.
return;
out << "The magic number is: " << 49 << "\n";
file->close(); //for closing the file.
Re: How to create in memory QFile
Quote:
Originally Posted by
sonulohani
Ok. For that, i think dont add the file in the resource folder,except that, directly create it in your project folder and access it from there. Because if you put your file in the resource and it will get compile and you'll not able to edit it. For that,
Code:
file=new QFile("demo.txt");.
return;
out << "The magic number is: " << 49 << "\n";
file->close(); //for closing the file.
contains leaks...
Why put QFile on the heap at all?
If op wants to write/print QTextDocument, then he should familiarise himself with http://doc.qt.io/qt-4.8/qtextdocumentwriter
Re: How to create in memory QFile
I am using QTextDocumentWriter to write my QTextDocument, but here along with the document i want to write a file and add a link to the file in the document. I have added the link and it is working, but i want to write the document or not depends on whether user does export. For that I want to add the file to the document resource using the QTextDocument::addResource which will hold the QFile so that i could get the file when i want to write it.
Re: How to create in memory QFile
QFile doesnt actually hold the data of the file so not sure that will help you much. You might as well just be saving the file name...
Re: How to create in memory QFile
Quote:
Originally Posted by
sonulohani
Ok. For that, i think dont add the file in the resource folder,except that, directly create it in your project folder and access it from there. Because if you put your file in the resource and it will get compile and you'll not able to edit it. For that,
Code:
file=new QFile("demo.txt");.
return;
out << "The magic number is: " << 49 << "\n";
file->close(); //for closing the file.
flush is very important when you write to the file :D Sometime can bad things happen when you don't flush it
Re: How to create in memory QFile
Who said that QFile should always be on stack? Its depend on you whether you want to allocate in stack or heap.
Re: How to create in memory QFile
QBuffer is the in-memory QIODevice you might want.
Re: How to create in memory QFile
Quote:
Originally Posted by
sonulohani
Who said that QFile should always be on stack? Its depend on you whether you want to allocate in stack or heap.
nobody said that.
If, however, you are going to write an example that looks like a java programmer wrote it, perhaps you should expect some comeback.
Re: How to create in memory QFile
Then do coding in java, why r u here? Its depend on your program methodology. And if someone is here than atleast he should know the basic concept of c++(memory allocation). You shouldnt have to think about what they do with this code or if he /she will understand this code or not. Okay, point out the line that is causing memory leak in my code.
Re: How to create in memory QFile
Quote:
Originally Posted by
sonulohani
Okay, point out the line that is causing memory leak in my code.
Code:
...
return; //memoy leak if a file can't be opened
...
The simplest way to fix this without using delete it's just provide a parent in QFile's ctor.
Re: How to create in memory QFile
No, simplest way is to say `QFile file;`, not `QFile* file = new ...`
Re: How to create in memory QFile
Quote:
Originally Posted by
amleto
No, simplest way is to say `QFile file;`, not `QFile* file = new ...`
Yup, and change all "->" to "." if you have tons of code.
Re: How to create in memory QFile
Quote:
Originally Posted by
sonulohani
Then do coding in java, why r u here? Its depend on your program methodology. And if someone is here than atleast he should know the basic concept of c++(memory allocation). You shouldnt have to think about what they do with this code or if he /she will understand this code or not. Okay, point out the line that is causing memory leak in my code.
You are the one writing like java: Allocating on the heap when only a local variable is needed - check. Using new without delete - check.
Your leak has been pointed out already, although why you couldn't see it for yourself is concerning.