C reading/writing to a file in binary mode
I created a File of 4000 blocks with a blocksize of 4096 Bytes. Now I want
to manipulate single blocks and read them again without changeing the
files' size. Actually I want to write blocks out of another file to
specific blocks in the file I created. Therefore I am opening the Files in
binarymode like this:
FILE * storeFile=fopen(targetFile, "wb"); // this one I created before
FILE * sourceFILE=fopen(sourceFile,"rb");
now I am trying to read stuff to a pointer
char * ptr=malloc(4096);
...
for(i=0; i<blocks_needed; i++)
{
fread(ptr,4096,1,sourceFile);
// now I am going to the position of the blocks I want to write to
fseek(storeFile,freeBlocks[i]*4096,SEEK_SET);
// and now I am writing it to the File I created before
fwrite(ptr,4096,1,storeFile);
...
}
For some reason the File I created before changes it's size and becomes a
copy of the file I wanted to write into it.
What am I doing wrong?
Thank you in advance!
No comments:
Post a Comment