1.
2.使用标准IO函数实现拷贝
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define PRINT_ERROR(msg) do{perror(msg);return -1;}while(0)
int main(int argc, const char *argv[])
{
FILE* fp=fopen("test1.c","r");
if(NULL==fp)
printf("fopen error");
FILE* gp=fopen("cptest.c","w");
if(NULL==gp)
printf("fopen error too");
int res;
while((res=fgetc(fp))!=EOF)
{
int ges=fgetc(gp);
ges=res;
printf("%c",ges);
}
if(fclose(fp)==EOF)
PRINT_ERROR("flcose error");
return 0;
}
3.使用fgets打印文件
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <errno.h>
#define PRINT_ERROR(msg) do{perror(msg); return -1;}while(0)
int main(int argc, const char *argv[])
{
FILE* fp= fopen("3_3_1.c","r");
if(NULL==fp)
{
printf("fopen error");
}
int res;
while((res=fgetc(fp))!=EOF)
{
printf("%c",res);
}
if(fclose(fp)==EOF)
PRINT_ERROR("flcose error");
return 0;
}
4.计算文件行数
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define PRINT_ERROR(msg) do{perror(msg);return-1;}while(0)
int main(int argc, const char *argv[])
{
FILE* fp=fopen("3_3_1.c","r");
if(NULL==fp)
printf("fopen error");
int res;
int count=0;
while((res=fgetc(fp))!=EOF)
{
if(res=='\n')
{
count++;
}
else if(res==EOF)
break;
}
printf("有%d行",count);
if(fclose(fp)==EOF)
PRINT_ERROR("fclose error");
return 0;
}