一、思维导图
二、练习
1.使用标准IO函数,实现文件的拷贝
#include <head.h>
int main(int argc, const char *argv[])
{
FILE *p=fopen("./one.txt","r");
FILE *fp=fopen("./two.txt","r+");
if(p==NULL)
PRINT_ERROR("fopen error");
while(1)
{
int res=fgetc(p);
fputc(res,fp);
if(res==EOF)
{
return -1;
}
}
return 0;
}
2.使用fgets函数,打印一个文件,类似cat
#include <head.h>
int main(int argc, const char *argv[])
{
FILE *p=fopen("./one.txt","r");
if(p==NULL)
PRINT_ERROR("fopen error");
char buf[128]={0};
while(fgets(buf,sizeof(buf),p)!=NULL){
printf("%s",buf);
}
return 0;
}
3.计算文件的行数
#include <head.h>
int main(int argc, const char *argv[])
{
FILE *p=fopen("./one.txt","r");
if(p==NULL)
PRINT_ERROR("fopen error");
int count=0;
int res;
while((res=fgetc(p))!=EOF){
if(res=='\n')
count++;
}
printf("%d\n",count);
return 0;
}