changes in a binary file

Hey
i am programming with c language
i want to write a function that takes a binary file and remove line number X from it.
i have wrote this function:
* every 6 chars or unsigned chars are one line




void remov(FILE *fr, long X)
{
FILE *ft=fopen("perm.lst","wb");
unsigned char hex[6] = {0};
long i=0;

while(!feof(fr))
{
fread(hex, 1, 6, fr);
if(i!=(X-1))
fwrite(hex, 1, 6, ft);

i++;
}
fr=fopen("perm.lst","rb");
fclose(ft);
}

can anyone tell what is wrong in this function, or how can i make better.
thanks
If you open the same file in "r" mode, then in "w" mode in 2 differnet file descriptors, the opening in "w"mode will erase previous content and you wont be able to do anything.

Then, if you know the exact size of a line, you can calculate the position in the file of the line to ignore. So you can make 1 read of what is before, 1 read of what is after the file

Then only, reopen the file in 'w' mode to reset it's size to 0, and write the first and last part you read previously

EDIT : i never tried it, but it seems there is a truncate() function to reduce a file size,
So you could read only the part after the line to ignore in memory, then truncate the file to its orignnal size minus 6 bytes, and finally reopen it in "r+" mode to write the part after the line 6 bytes before it's initial position
Last edited on
man first of all thanks for replying
what you said about making the mode "w" will erase the content of the file is right but perm.lst is empty.
first of all the mode was "w" and then it turned to "r" i dont think taht will erase the content.
anyway, i would appreciate if u rewrite the function, because till now i didnt understand where should i use the freopen function.
Topic archived. No new replies allowed.