K
kindrain
the code checks whether a.txt has exact the same lines, then write
different lines into b.txt
Here it is:
(before that ,you should creat a.txt)
----------------------
#include<stdio.h>
#include<string.h>
void main(){
FILE *fp,*ftemp;
char ch[50],comp[50];
int lflag;
fp=fopen("a.txt","r");
ftemp=fopen("b.txt","w+");
/*the following checks whether fp has exact the same lines, then write
different lines into ftemp*/
while(!feof(fp)){
fgets(ch,50,fp);
lflag=1;
rewind(ftemp); /*the file point points to the beginning of ftemp*/
while(!feof(ftemp)){
fgets(comp,50,ftemp);
if(!strcmp(ch,comp)){
lflag=0;
break;
}
} /*check all lines in ftemp. if there is a string equals to ch,then
make lflag=0;*/
if(lflag){ fseek(ftemp,0,2);
fputs(ch,ftemp);
puts(ch);
} /*if lflag==1,then write string ch into ftemp, and output to the
screen.*/
}
fclose(fp);
fclose(ftemp);
}
------------------------
when a.txt has some chars, we get the right result;
but when a.txt is empty, we can see the following words both in b.txt
and in the screen:
ÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌP»B
now here comes a question:
if fp points to an empty file,then "feof(fp)" should be equal to
1,then how can it step into while?
I've thought it for several days.so--
Does every file has an EOF in the end?(I find empty files are 0k)
Does the file point points to the first char when the file is opened?
Are the first char and last char both EOF in an empty file?
Hoping for your help!
different lines into b.txt
Here it is:
(before that ,you should creat a.txt)
----------------------
#include<stdio.h>
#include<string.h>
void main(){
FILE *fp,*ftemp;
char ch[50],comp[50];
int lflag;
fp=fopen("a.txt","r");
ftemp=fopen("b.txt","w+");
/*the following checks whether fp has exact the same lines, then write
different lines into ftemp*/
while(!feof(fp)){
fgets(ch,50,fp);
lflag=1;
rewind(ftemp); /*the file point points to the beginning of ftemp*/
while(!feof(ftemp)){
fgets(comp,50,ftemp);
if(!strcmp(ch,comp)){
lflag=0;
break;
}
} /*check all lines in ftemp. if there is a string equals to ch,then
make lflag=0;*/
if(lflag){ fseek(ftemp,0,2);
fputs(ch,ftemp);
puts(ch);
} /*if lflag==1,then write string ch into ftemp, and output to the
screen.*/
}
fclose(fp);
fclose(ftemp);
}
------------------------
when a.txt has some chars, we get the right result;
but when a.txt is empty, we can see the following words both in b.txt
and in the screen:
ÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌP»B
now here comes a question:
if fp points to an empty file,then "feof(fp)" should be equal to
1,then how can it step into while?
I've thought it for several days.so--
Does every file has an EOF in the end?(I find empty files are 0k)
Does the file point points to the first char when the file is opened?
Are the first char and last char both EOF in an empty file?
Hoping for your help!