Well, this is kind of a mountain of a question/bug. I'm using ftell() to ascertain the size of the file after a certain point, malloc() a buffer that size, use fread() to get the information, write my information in, write the information read earlier back in with fwrite()...and it seems like it works, but I get a massive white space overflow at the end of the file. For the complete code, copy this link into your browser: http://wryle.tripod.com/xml/news.zip. The following is the code in question, followed by the file I'm reading/writing from/to:
The file:Code:char *buf;
...
//inside a switch case
fp = fopen( MIni.GetCurrentData(), "r+" );
if (fp)
{
//set position to right after <entries>
//get size of file
fseek(fp, 0, SEEK_END);
flen = ftell(fp);
cout << "Whole file: " << flen << endl;
fseek(fp, 35L, SEEK_SET);
flen_2 = ftell(fp);
cout << "Header: " << flen_2 << endl;
flen = flen-flen_2;
cout << "Whole file - Header: " << flen << endl;
fseek(fp, 35L, SEEK_SET);
buf=(char *)malloc(flen);
//fill the buffer in
fread(buf, flen, 1, fp);
fp = fopen( MIni.GetCurrentData(), "w+" );
if (fp)
{
rewind(fp);
cout << "File \""<<MIni.GetCurrentData()<<"\" already exists.\nPosting...\n";
cout << "Message Title: ";
cin.getline(msg.title,256);
cout << "Message Body: ";
cin.getline(msg.message,4096);
cout << "E-Mail Address: ";
cin.getline(msg.email,256);
cout << "Username: ";
cin.getline(msg.user,256);
cout << "Date to be posted: ";
cin.getline(msg.date,256);
fprintf( fp, "<?xml version=\"1.0\"?>\n\n");
fprintf( fp, "<entries>\n");
fprintf( fp, "\t<entry>\n");
fprintf( fp, "\t\t<title>%s</title>\n", msg.title);
fprintf( fp, "\t\t<message>%s</message>\n", msg.message);
fprintf( fp, "\t\t<email>%s</email>\n", msg.email);
fprintf( fp, "\t\t<user>%s</user>\n", msg.user);
fprintf( fp, "\t\t<date>%s</date>\n", msg.date);
fprintf( fp, "\t</entry>");
fwrite(buf, flen, 1, fp);
fclose( fp );
cout << "Successfully added.\n";
}
...
Thanks in advance,Code:<?xml version="1.0"?>
<entries>
<entry>
<title>Test entry</title>
<message>Test entry message</message>
<email>[email protected]</email>
<user>elios</user>
<date>08.09.2003</date>
</entry>
</entries>
--elios