一、思维导图
二、作业
1.使用标准IO函数,实现文件拷贝
#include <head.h>
//""表示在当前目录,<>表示在库里找
int main(int argc, const char *argv[])
{
//打开
FILE* fp=fopen("./one.txt","r");
if(fp==NULL)
{
PRINT_ERROR("fopen");
}
FILE* fp_two=fopen("./two.txt","w");
if(fp_two==NULL)
{
PRINT_ERROR("fopen");
}
while(1)
{
int res=fgetc(fp);
if(res==EOF)
{
break;
}
fputc(res,fp_two);
}
//关闭
if(fclose(fp)==EOF)
{
PRINT_ERROR("fclose");
}
if(fclose(fp_two)==EOF)
{
PRINT_ERROR("fclose");
}
return 0;
}
2.使用fgets函数打印文件
#include <head.h>
//""表示在当前目录,<>表示在库里找
int main(int argc, const char *argv[])
{
//打开
FILE* fp=fopen("./one.txt","r");
if(fp==NULL)
{
PRINT_ERROR("fopen");
}
//读取
int res;
while(res!=EOF)
{
res=fgetc(fp);//会自动后移
if(res==EOF)
{
return -1;
}
printf("%c",res);
}
printf("%ld",fp->_IO_buf_end-fp->_IO_buf_base);
//关闭
if(fclose(fp)==EOF)
{
PRINT_ERROR("fclose");
}
//printf("%ld",fp->_IO_buf_end-fp->_IO_buf_base);
return 0;
}
3.计算文件行数
#include <head.h>
//""表示在当前目录,<>表示在库里找
int main(int argc, const char *argv[])
{
//打开
FILE* fp_two=fopen("./two.txt","r");
int res=fgetc(fp_two);
if(res==EOF)
{
printf("该文件0行\n");
return -1;
}
int count=1;
while(1)
{
res=fgetc(fp_two);
if(res==10)
{
count+=1;
}
else if(res==EOF)
{
break;
}
}
printf("该文件有%d行\n",count);
//关闭
if(fclose(fp_two)==EOF)
{
PRINT_ERROR("fclose");
}
return 0;
}