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.
Re: Appending text to a file
Hi,
thx, I tried this:
Code:
while (!in.atEnd()) {
if (in.readLine() == "END:VCALENDAR") {
//here I need to overwrite - how?
in << "TEST\nEND:VCALENDAR\n";
break;
}
}
}
the end of the file looks like this:
Code:
END:VCALENDARTEST
END:VCALENDAR
How can move "backwards" to overwrite the line? Start reading the file from the end does not work, I guess ...
Re: Appending text to a file
Seems to work like this:
Code:
while (!in.atEnd()) {
qint64 last = in.pos();
if (in.readLine() == "END:VCALENDAR") {
in.seek(last);
in << "TEST\nEND:VCALENDAR\n";
break;
}
}
}
is this the best way to achieve that?
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.
Re: Appending text to a file
Re: Appending text to a file
Finally ... not perfect i guess
Code:
int strSize
= QString("END:VCALENDAR").
size();
qint64 fileSize = file.size();
in.seek(fileSize - strSize);
if (in.readLine() == "END:VCALENDAR") {
in.seek(fileSize - strSize);
in << "TEST\nEND:VCALENDAR";
}
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
Code:
BEGIN:VCALENDAR
PRODID:ID
VERSION:2.0
BEGIN:VEVENT //in this line i try to start writing the event text
code
Code:
event.append("BEGIN:VEVENT");
event.append("\n");
event.append("UID:").append(DTSTAMP).append("-@cutefarm.de");
event.append("\n");
event.append("DTSTAMP:").append(DTSTAMP);
event.append("\n");
event.append("DTSTART:").append(DTSTAMP);
event.append("\n");
event.append("DTEND:").append(DTSTAMP);
event.append("\n");
event.append("SUMMARY:").append(SUMMARY);
event.append("\n");
event.append("DESCRIPTION:").append(DESCRIPTION);
event.append("\n");
event.append("CLASS:PRIVATE");
event.append("\n");
event.append("CATEGORIES:BUSINESS,HUMAN RESOURCES");
event.append("\n");
event.append("END:VEVENT");
event.append("\n");
event.append("END:VCALENDAR");
....
if (in.readLine() == "END:VCALENDAR") {
in.
seek(in.
pos() - QString("END:VCALENDAR").
size());
in << event;
}
}
How can I avoid these characters?
Re: Appending text to a file
google is my friend :-)
in.setGenerateByteOrderMark(false) did it.