Page 2 of 2
Re:deleting file contents
Posted: Sun Feb 13, 2005 2:01 pm
by Schol-R-LEA
If you are working with very large files, or are dealing with large numbers of insertions and deletions, you may be better off 'cancelling' records without removing them. For example, if your fields are simple newline delimited strings, you can fill the record to be removed with newlines; you would then write your read and search functions to ignore successive newlines. You would still want to periodically copy the data to a fresh file to compact the data, but you would avoid the overhead of moving the data around with every single deletion.
This is especially useful when you are dealing with indexed data, because you don't need to update the indices as often, and there would be little impact on per record read time (because you would be seeking to a fixed position). This is how many database systems work, in fact, though most will also try to 'recover' cancelled space while running as well.
Re:deleting file contents
Posted: Sun Feb 27, 2005 11:43 am
by rich_m
there seems to be a bug in this coding (not suprising), well this is my function to delete the first 8 lines from a file if the file contains more than 8 lines . well it works, but at the end of both the files a dummy variable seems to be formed (somthing like a ':y' ), i have no idea wat to do bout it?? down below is my function
Code: Select all
//delete lines from the file if file contains >=8 lines
void trim_file(char *url2,char *turl2)
{
int rk=0;???//counter, to find no. of line
char ch;???//to store each char from file
FILE *pt,*ptr;//"pt" is the file^,"ptr" is the temp file^
//COPING NECESSARY DATA
?????????pt=fopen(url2,"r");
?????????ptr=fopen(turl2,"w");
?????????rk=0;
?????????do
?????????{
????????????ch=fgetc(pt);
????????????if(ch=='\n')
???????????????rk++;
????????????if(rk>8)
????????????{
???????????????putc(ch,ptr);
????????????}
?????????}while(ch!=EOF);
?????????fclose(ptr);
?????????fclose(pt);
//TRANSFERING FROM TEMP FILE
?????????ptr=fopen(turl2,"r");
?????????pt=fopen(url2,"w");
?????????do
?????????{
????????????ch=fgetc(ptr);
????????????putc(ch,pt);
?????????}while(ch!=EOF);
?????????fclose(ptr);
?????????fclose(pt);
}
Re:deleting file contents
Posted: Mon Feb 28, 2005 11:51 am
by rich_m
???
Re:deleting file contents
Posted: Mon Feb 28, 2005 11:04 pm
by AR
To take a guess:
Code: Select all
do
{
ch=fgetc(ptr);
putc(ch,pt);
}while(ch!=EOF);
This code is flawed, if you read an EOF, it then
writes the EOF out to the other file before exiting the loop, this bug is in both loops. It would be better set up like this:
Code: Select all
ch=fgetc(ptr);
while(ch!=EOF)
{
putc(ch,pt);
ch=fgetc(ptr);
}
Re:deleting file contents
Posted: Tue Mar 01, 2005 11:11 am
by rich_m
shucks.........how stupid of me to misuse the do{}while; :-\
:)thanks a lot ar.
Re:deleting file contents
Posted: Thu Mar 03, 2005 11:36 am
by rich_m
the program works but does'nt serve the purpose it deletes all except the last line of the file ???
Re:deleting file contents
Posted: Sat Mar 05, 2005 12:36 pm
by rm
whew.............got it workin,
Code: Select all
pt=fopen(url2,"r");
ptr=fopen(turl2,"w");
rk=0;
while(rk<8)
{
ch=fgetc(pt);
if(ch=='\n')
{
rk++;
ch=fgetc(pt);
}
}
while(ch!=EOF)
{
putc(ch,ptr);
ch=fgetc(pt);
}
fclose(ptr);
fclose(pt);
ptr=fopen(turl2,"r");
pt=fopen(url2,"w");
while((ch=fgetc(ptr))!=EOF)
{
putc(ch,pt);
}
fclose(ptr);
fclose(pt);