Results 1 to 3 of 3

Thread: write in a file next line each time

  1. #1
    Join Date
    Aug 2010
    Posts
    1
    Qt products
    Qt3
    Platforms
    Windows

    Question write in a file next line each time

    hello,

    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:

    Qt Code:
    1. unsigned char buffer[8]={0};
    2. int cmd=1;
    3. int tt=100;
    4. tt<<16;
    5. tt|cmd;
    6. sprintf((char*)buffer,"%d",tt);
    7. FILE *fp;
    8. fp=fopen("root/test.txt","a");
    9. if(fp==NULL)
    10. perror("file pointer:");
    11. fwrite(buffer 8,1,fp);
    12. fclose(fp);
    To copy to clipboard, switch view to plain text mode 

    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
    Last edited by Lykurg; 30th August 2011 at 06:28. Reason: missing [code] tags

  2. #2
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: write in a file next line each time

    Why don't you use the Qt classes like QFile for your task? Also see QIODevice::Append.

  3. #3
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,594
    Thanks
    13
    Thanked 1,587 Times in 1,516 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: write in a file next line each time

    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).
    Qt Code:
    1. int tt=100;
    2. FILE *fp = NULL;
    3. if (fp = fopen("test.txt", "a")) {
    4. fprintf(fp, "%d\n", tt);
    5. }
    To copy to clipboard, switch view to plain text mode 
    or using Qt:
    Qt Code:
    1. int tt=100;
    2.  
    3. QFile fp("test.txt");
    4.  
    5. if (fp.open(QIODevice::Text | QIODevice::Append)) {
    6. QTextStream s(&fp);
    7. s << tt << endl;
    8. }
    To copy to clipboard, switch view to plain text mode 

    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.

Similar Threads

  1. How to write something on a text file on a certain line?
    By Bong.Da.City in forum Qt Programming
    Replies: 1
    Last Post: 17th December 2010, 21:01
  2. Write one value per line in text file
    By babygal in forum Newbie
    Replies: 11
    Last Post: 1st December 2010, 06:47
  3. How to read and write line by line?
    By Alex Snet in forum Qt Programming
    Replies: 3
    Last Post: 28th November 2010, 15:49
  4. QFile write and file modyfication time
    By Talei in forum Newbie
    Replies: 1
    Last Post: 9th May 2010, 20:46
  5. How to write something in line of txt file
    By Zergi in forum Qt Programming
    Replies: 2
    Last Post: 24th December 2007, 10:02

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.