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

Pages: 1-

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: =+=*=F=R=O=Z=E=N==V=O=I=D=*=+= !frozEn/KIg 2009-08-14 14:11

Use fputc()


_________________________________
http://xs135.xs.to/xs135/09042/av922.jpg
Velox Et Astrum gamedev forum: http://etastrum.phpbb3now.com
These people are obviously freaks.

Name: !Anus3nMVO2 2009-08-14 14:19

>>2
Use fioc
Posted by =+=*=F=R=O=Z=E=N==V=O=I=D=*=+=!frozEn/KIg : 2009-08-14 14:11

Name: =+=*=F=R=O=Z=E=N==V=O=I=D=*=+= !frozEn/KIg 2009-08-14 14:20

Somethign like this should work.
unsigned char arr_2[4]={value1,value2,value3,value4} //fill useful values.
fp=fopen("file","wb");fseek(fp,Offset,SEEK_SET);
for(loop=0;loop<4;loop++){fputc(arr_2[loop],fp);}
fclose(fp);



__________________________________________
http://xs135.xs.to/xs135/09042/av922.jpg
Velox Et Astrum gamedev forum: http://etastrum.phpbb3now.com
orbis terrarum delenda est

Name: =+=*=F=R=O=Z=E=N==V=O=I=D=*=+= !frozEn/KIg 2009-08-14 14:41

Doesn't work apparently. Just load it in memory and patch the offsets there.



______________________________
http://xs135.xs.to/xs135/09042/av922.jpg
Velox Et Astrum gamedev forum: http://etastrum.phpbb3now.com
It doesn't matter how you feel inside, you know. It's what shows up on the surface that counts.

Name: Anonymous 2009-08-14 14:51

>>1
You forgot to fsync, none of the data will ever be written to the files.

Name: Anonymous 2009-08-14 14:54

Oh and try opening with "r+b".

Name: =+=*=F=R=O=Z=E=N==V=O=I=D=*=+= !frozEn/KIg 2009-08-14 15:00

>>1 this works:
#include <stdio.h>
#include <stdlib.h>
#define OFFSET 3000
//write 4 bytes
void main(int argc,char**argv){
unsigned char arr[4]={1,2,3,4};//bytes
FILE *fp=fopen("file.txt","r+b");fseek(fp,OFFSET,SEEK_SET);fwrite(arr,1,4,fp);fclose(fp);

}

______________________________
http://xs135.xs.to/xs135/09042/av922.jpg
Velox Et Astrum gamedev forum: http://etastrum.phpbb3now.com
The substructure of the universe regresses infinitely towards smaller and smaller components. Behind atoms we find electrons, and behind electrons, quarks. Each layer unraveled reveals new secrets, but also new mysteries.

Name: Anonymous 2009-08-14 15:32

>>6
fclose flushes the stream before it closes. Unless his library is broken, he doesn't need fsync.

>>7
Smells like cargo cult programming.

Name: Anonymous 2009-08-14 15:48

>>9
Where(wwwwwwwwwww?

http://sourceware.org/git/?p=glibc.git;a=blob;f=libio/iofclose.c

Name: Anonymous 2009-08-14 15:51

Name: Anonymous 2009-08-14 15:54

>>10
In one of those macros, presumably. Read the fucking man page:

DESCRIPTION
       The  fclose()  function will flush the stream pointed to by fp (writing
       any buffered output data using fflush(3)) and close the underlying file
       descriptor.

Name: Anonymous 2009-08-14 16:04

>>12
>NOTES
       Note that fflush() only flushes the user space buf‐
       fers provided by the C library.  To ensure that the
       data is physically stored on disk the  kernel  buf‐
       fers must be flushed too, for example, with sync(2)
       or fsync(2).

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.

Name: Anonymous 2009-08-14 16:15

>>14
Maybe YOU are!

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