View Full Version : write in a file next line each time

30th August 2011, 05:52

In my code i opened a file and write in it now i want each time i reopen it,data must be written from next line.
my code snippet is:

unsigned char buffer[8]={0};
int cmd=1;
int tt=100;
FILE *fp;
perror("file pointer:");
fwrite(buffer 8,1,fp);

I also searched but did'nt got any right answer. I want my new text will come at the beginning of next line each time i write. please help me out.
Thanks in advance

30th August 2011, 06:28
Why don't you use the Qt classes like QFile for your task? Also see QIODevice::Append.

30th August 2011, 06:54
If you want basic C programming help then a Qt specific forum is not the best place to look.

If you want a text file with new line characters between lines then you should actually put a new line character in the output and use the text file write functions like fprintf() (or C++ streams).

int tt=100;
FILE *fp = NULL;
if (fp = fopen("test.txt", "a")) {
fprintf(fp, "%d\n", tt);

or using Qt:

int tt=100;

QFile fp("test.txt");

if (fp.open(QIODevice::Text | QIODevice::Append)) {
QTextStream s(&fp);
s << tt << endl;

Unrelated to your question:

Lines 4 and 5 do nothing (generate compiler warnings here)
Line 11 will not compile as supplied, and the arguments are possibly the wrong way around.