课堂笔记整理思维导图
二、使用fgets统计给定文件的行号
int main(int argc, const char *argv[])
{
int count=0;
FILE* file=fopen("./text.txt","r");
char buf[20]="";
char *data=fgets(buf,sizeof(buf),file);
while(data!=NULL){
int index=strlen(data);
if(buf[index-1]=='\n'){
count++;
}
data=fgets(buf,sizeof(buf),file);
}
printf("文件有%d行\n",count+1);
return 0;
}
三、使用fgets、fputs完成两个文件的拷贝
int main(int argc, const char *argv[])
{
if(argc!=3){
return -1;
}
FILE * sfile=fopen(argv[1],"r");
FILE * tfile=fopen(argv[2],"w");
char buf[20]="";
char *data=fgets(buf,sizeof(buf),sfile);
while(data!=NULL){
fputs(data,tfile);
data=fgets(buf,sizeof(buf),sfile);
}
return 0;
}
标签:int,data,7.26,作业,char,课后,fgets,sizeof,buf
From: https://blog.csdn.net/m0_65591011/article/details/140724179