Results 1 to 9 of 9

Thread: Appending text to a file

  1. #1
    Join Date
    Mar 2008
    Posts
    141
    Thanks
    10
    Thanked 9 Times in 9 Posts

    Default Appending text to a file

    Hi,

    I would like to crate a ics (iCalendar) file. The file content is something like this.

    Qt Code:
    1. BEGIN:VCALENDAR
    2. VERSION:2.0
    3. PRODID:http://www.example.com/calendarapplication/
    4. METHOD:PUBLISH
    5. BEGIN:VEVENT
    6. UID:461092315540@example.com
    7. ORGANIZER;CN="Alice Balder, Example Inc.":MAILTO:alice@example.com
    8. SUMMARY:Eine Kurzinfo
    9. DESCRIPTION:Beschreibung des Termines
    10. CLASS:PUBLIC
    11. DTSTART:20060910T220000Z
    12. DTEND:20060919T215900Z
    13. DTSTAMP:20060812T125900Z
    14. END:VEVENT
    15. END:VCALENDAR
    To copy to clipboard, switch view to plain text mode 

    What is the best way to add an VEVENT in between BEGIN:VCALENDAR and END:VCALENDAR?

    Do I need to read the whole file, remove END:VCALENDAR, append the new VEVENT + END:VCALENDAR and then write the whole thing again? When the calendar grows from week to week this might not be the best way.

  2. #2
    Join Date
    Jan 2006
    Posts
    132
    Thanked 16 Times in 16 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Appending text to a file

    You can open the file for read/write, find END:VCALENDAR by seeking from the end and then just write over it.

  3. #3
    Join Date
    Mar 2008
    Posts
    141
    Thanks
    10
    Thanked 9 Times in 9 Posts

    Default Re: Appending text to a file

    Hi,

    thx, I tried this:

    Qt Code:
    1. QFile file("inout.txt");
    2. if (file.open(QIODevice::ReadWrite | QIODevice::Text)) {
    3.  
    4. QTextStream in(&file);
    5.  
    6. while (!in.atEnd()) {
    7.  
    8. if (in.readLine() == "END:VCALENDAR") {
    9. //here I need to overwrite - how?
    10. in << "TEST\nEND:VCALENDAR\n";
    11. break;
    12. }
    13. }
    14. }
    To copy to clipboard, switch view to plain text mode 

    the end of the file looks like this:
    Qt Code:
    1. END:VCALENDARTEST
    2. END:VCALENDAR
    To copy to clipboard, switch view to plain text mode 

    How can move "backwards" to overwrite the line? Start reading the file from the end does not work, I guess ...

  4. #4
    Join Date
    Mar 2008
    Posts
    141
    Thanks
    10
    Thanked 9 Times in 9 Posts

    Default Re: Appending text to a file

    Seems to work like this:

    Qt Code:
    1. QFile file("inout.txt");
    2. if (file.open(QIODevice::ReadWrite | QIODevice::Text)) {
    3.  
    4. QTextStream in(&file);
    5.  
    6. while (!in.atEnd()) {
    7. qint64 last = in.pos();
    8. if (in.readLine() == "END:VCALENDAR") {
    9. in.seek(last);
    10. in << "TEST\nEND:VCALENDAR\n";
    11. break;
    12. }
    13. }
    14. }
    To copy to clipboard, switch view to plain text mode 

    is this the best way to achieve that?

  5. #5
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Appending text to a file

    You are still reading the whole file line by line. You should seek at QFile::size() minus something at start writing from there.
    J-P Nurmi

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Appending text to a file

    Or there is libical...

  7. #7
    Join Date
    Mar 2008
    Posts
    141
    Thanks
    10
    Thanked 9 Times in 9 Posts

    Default Re: Appending text to a file

    Finally ... not perfect i guess

    Qt Code:
    1. int strSize = QString("END:VCALENDAR").size();
    2. qint64 fileSize = file.size();
    3.  
    4. QTextStream in(&file);
    5.  
    6. in.seek(fileSize - strSize);
    7.  
    8. if (in.readLine() == "END:VCALENDAR") {
    9. in.seek(fileSize - strSize);
    10. in << "TEST\nEND:VCALENDAR";
    11. }
    To copy to clipboard, switch view to plain text mode 

  8. #8
    Join Date
    Mar 2008
    Posts
    141
    Thanks
    10
    Thanked 9 Times in 9 Posts

    Default Re: Appending text to a file

    Searching the right position seems to work, but when appending event text i get "strange" symbols () in the text file:

    file
    Qt Code:
    1. BEGIN:VCALENDAR
    2. PRODID:ID
    3. VERSION:2.0
    4. BEGIN:VEVENT //in this line i try to start writing the event text
    To copy to clipboard, switch view to plain text mode 

    code
    Qt Code:
    1. QString event;
    2. event.append("BEGIN:VEVENT");
    3. event.append("\n");
    4. event.append("UID:").append(DTSTAMP).append("-@cutefarm.de");
    5. event.append("\n");
    6. event.append("DTSTAMP:").append(DTSTAMP);
    7. event.append("\n");
    8. event.append("DTSTART:").append(DTSTAMP);
    9. event.append("\n");
    10. event.append("DTEND:").append(DTSTAMP);
    11. event.append("\n");
    12. event.append("SUMMARY:").append(SUMMARY);
    13. event.append("\n");
    14. event.append("DESCRIPTION:").append(DESCRIPTION);
    15. event.append("\n");
    16. event.append("CLASS:PRIVATE");
    17. event.append("\n");
    18. event.append("CATEGORIES:BUSINESS,HUMAN RESOURCES");
    19. event.append("\n");
    20. event.append("END:VEVENT");
    21. event.append("\n");
    22. event.append("END:VCALENDAR");
    23.  
    24. ....
    25.  
    26. if (in.readLine() == "END:VCALENDAR") {
    27. in.seek(in.pos() - QString("END:VCALENDAR").size());
    28. in << event;
    29. }
    30. }
    To copy to clipboard, switch view to plain text mode 

    How can I avoid these characters?
    Last edited by janus; 25th March 2009 at 08:10.

  9. #9
    Join Date
    Mar 2008
    Posts
    141
    Thanks
    10
    Thanked 9 Times in 9 Posts

    Default Re: Appending text to a file

    google is my friend :-)

    in.setGenerateByteOrderMark(false) did it.

Similar Threads

  1. Unhandled exception in qatomic
    By NewGuy in forum Qt Programming
    Replies: 14
    Last Post: 23rd July 2013, 09:49
  2. Opening text file fails after autostartup on windows
    By yogourta in forum Qt Programming
    Replies: 2
    Last Post: 18th October 2008, 03:52
  3. Replies: 1
    Last Post: 3rd September 2008, 14:16
  4. Text file to PDF convertion using C++ code
    By joseph in forum General Discussion
    Replies: 2
    Last Post: 21st August 2008, 01:28
  5. Huge Text File
    By mcosta in forum Qt Programming
    Replies: 3
    Last Post: 11th January 2008, 19:23

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.