D
Don Pasquale
The following function intends to delete "numberoflines" lines from a text
file, named "s" (string pointer) and pointed to by file pointer "fp", starting
from line "line".
Now, the function works an inteded, but I think that this is purely
coincidential.
I'm using a temp file to copy the contents of the text file, and then, copy
the last lines that i want to keep, on top of those i want to delete.I fwrite
an EOF character after the copied lines, so as to discard the rest of the
file.But this doesn't work!When I open the file with a text editor, I can see
the rest of the lines after a strange character, which evidently represents
EOF.
So, I reopened the temp file (discarding all previous content) and copied the
file again in to it (from start, till EOF, then fclosed it), changed its name
with that of the original file, and then fclose its pointer, and fopened it
using the original files pointer!And it works!The rest of the program uses the
"new" fp...., like it was the originaly fopened one, though its a different
pointer to a different file, only with the same name.
And that leads to the question:when we pass file pointers to functions, isn't
a local copy created?if I fclose a local copy of a file pointer inside a
function, then the "real" file pointer isn't left there dangling? Because from
this program's behavior, it seems like pass by reference is automaticaly used
for file pointers.
int delete_string_f(char* s,FILE* fp,int line,int numberoflines)
{
int check=1;
FILE* fp2=fopen("bak.txt","w+");
rewind(fp);
rewind(fp2);
/*copy the contents of the file pointed by fp to bak.txt*/
filecopy(fp,fp2);
/*move fp to the start of the line we want to delete*/
check=gotoline(line-1,&fp);
if(check==0)
{
printf("EOF encountered\n");
return -1;
}
/*move fp2 to the start of the line after the ones we want to delete*/
check=gotoline(line-1+numberoflines,&fp2);
/*copy the rest of the lines from fp2 on top of the lines we want to delete
from fp*/
if(check!=0)filecopy(fp2,fp);
/*make the file end after the lines we 've copied*/
fprintf(fp,"%c",EOF);
freopen("bak.txt","w+",fp2);
rewind(fp);
/*copying the file from start till the EOF we fprinted in to another file*/
filecopy(fp,fp2);
fclose(fp);
fclose(fp2);
/*remove the original file*/
remove(s);
/*rename the temp file with the original file's name*/
rename("bak.txt",s);
/*change where fp points*/
fp=fopen(s,"r+");
return 0;
}
file, named "s" (string pointer) and pointed to by file pointer "fp", starting
from line "line".
Now, the function works an inteded, but I think that this is purely
coincidential.
I'm using a temp file to copy the contents of the text file, and then, copy
the last lines that i want to keep, on top of those i want to delete.I fwrite
an EOF character after the copied lines, so as to discard the rest of the
file.But this doesn't work!When I open the file with a text editor, I can see
the rest of the lines after a strange character, which evidently represents
EOF.
So, I reopened the temp file (discarding all previous content) and copied the
file again in to it (from start, till EOF, then fclosed it), changed its name
with that of the original file, and then fclose its pointer, and fopened it
using the original files pointer!And it works!The rest of the program uses the
"new" fp...., like it was the originaly fopened one, though its a different
pointer to a different file, only with the same name.
And that leads to the question:when we pass file pointers to functions, isn't
a local copy created?if I fclose a local copy of a file pointer inside a
function, then the "real" file pointer isn't left there dangling? Because from
this program's behavior, it seems like pass by reference is automaticaly used
for file pointers.
int delete_string_f(char* s,FILE* fp,int line,int numberoflines)
{
int check=1;
FILE* fp2=fopen("bak.txt","w+");
rewind(fp);
rewind(fp2);
/*copy the contents of the file pointed by fp to bak.txt*/
filecopy(fp,fp2);
/*move fp to the start of the line we want to delete*/
check=gotoline(line-1,&fp);
if(check==0)
{
printf("EOF encountered\n");
return -1;
}
/*move fp2 to the start of the line after the ones we want to delete*/
check=gotoline(line-1+numberoflines,&fp2);
/*copy the rest of the lines from fp2 on top of the lines we want to delete
from fp*/
if(check!=0)filecopy(fp2,fp);
/*make the file end after the lines we 've copied*/
fprintf(fp,"%c",EOF);
freopen("bak.txt","w+",fp2);
rewind(fp);
/*copying the file from start till the EOF we fprinted in to another file*/
filecopy(fp,fp2);
fclose(fp);
fclose(fp2);
/*remove the original file*/
remove(s);
/*rename the temp file with the original file's name*/
rename("bak.txt",s);
/*change where fp points*/
fp=fopen(s,"r+");
return 0;
}