Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

fshit()

Name: Anonymous 2009-08-14 14:02

It's time for a public game of FIND THE MISTAKE, since I'm starting to suspect this c library is a bit on the broken side.

   unsigned char *mf;
   FILE *fp;
   mf=malloc(1024*1024);

   /* blah blah blah
      fill mf with some useful data */

   fp=fopen("file1", "ab");
   fseek(fp, 4096, SEEK_SET);
   fwrite(mf+4096, sizeof(uint32), 1, fp);
   fclose(fp);

   fp=fopen("file2", "wb");
   fwrite(mf, 1024*1024, 1, fp);
   fclose(fp);

   fp=fopen("file3", "wb");
   fseek(fp, 4096, SEEK_SET);
   fwrite(mf+4096, sizeof(uint32), 1, fp);
   fclose(fp);


None of the called funcions return an error code. Both fwrite()s return 1 (for 1 record written).

file1, file2 and file3 are all 1 megabyte in size before execution, and are identical. What I want is write bytes 4096-4100 of fp in the same offset of the file.

The file1 code in theory does this. In practice it doesn't work: the file does not change at all, even though its timestamp does.

The file2 code works: it truncates the file and writes the entire buffer on it. Not what I want: I just want the four bytes.

The file3 code also works: it truncates the file, pads it with 4096 zeros and then writes the four bytes I want for a final filesize of 4100 (all numbers are decimal).

Any ideas?

Name: Anonymous 2009-08-14 16:14

>>13
That's only relevant to kernel or some kinds of embedded developers. You seem to be confused about how file I/O works.

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List