一、概念
二、标准IO
练习题
1.使用标准IO函数,实现文件的拷贝
#include <head.h>
int main(int argc, const char *argv[])
{
FILE *p=fopen("./1.c","r");
FILE *fp=fopen("./2.c","r+");
if(p==NULL)
PRINT_ERROR("fopen error");
while(p)
{
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 *fp=fopen("./test1.c","r");
if(NULL==fp)
{
PRINT_ERROR("fopen error");
return -1;
}
char buf[128]={0};
while(fgets(buf,sizeof(buf),fp)!=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;
}