PDA

View Full Version : How to create in memory QFile



pratham_shah
2nd August 2012, 12:17
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.

sonulohani
2nd August 2012, 12:50
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,



QFile *file;
file=new QFile("demo.txt");.
if (!file->open(QIODevice::WriteOnly | QIODevice::Text))
return;

QTextStream out(file);
out << "The magic number is: " << 49 << "\n";

file->close(); //for closing the file.

amleto
2nd August 2012, 13:33
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,



QFile *file;
file=new QFile("demo.txt");.
if (!file->open(QIODevice::WriteOnly | QIODevice::Text))
return;

QTextStream out(file);
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 qtextdocumentwriter

pratham_shah
9th August 2012, 06:29
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.

amleto
9th August 2012, 10:07
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...

Viper666
9th August 2012, 20:02
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,



QFile *file;
file=new QFile("demo.txt");.
if (!file->open(QIODevice::WriteOnly | QIODevice::Text))
return;

QTextStream out(file);
out << "The magic number is: " << 49 << "\n";

file->close(); //for closing the file.


file->flush();
flush is very important when you write to the file :D Sometime can bad things happen when you don't flush it

sonulohani
10th August 2012, 05:15
Who said that QFile should always be on stack? Its depend on you whether you want to allocate in stack or heap.

ChrisW67
10th August 2012, 08:16
QBuffer is the in-memory QIODevice you might want.

amleto
10th August 2012, 10:56
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.

sonulohani
10th August 2012, 12:01
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.

spirit
10th August 2012, 12:12
Okay, point out the line that is causing memory leak in my code.



...
if (!file->open(QIODevice::WriteOnly | QIODevice::Text))
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.

amleto
10th August 2012, 12:21
No, simplest way is to say `QFile file;`, not `QFile* file = new ...`

spirit
10th August 2012, 12:23
No, simplest way is to say `QFile file;`, not `QFile* file = new ...`
Yup, and change all "->" to "." if you have tons of code.

amleto
10th August 2012, 12:25
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.