1.输入一个文件名,默认文件存在,实现这个文件的下载(循环读取输入的文件,把读取的数据存到另一个文件中)
#ifndef __HIGH__
#define __HIGH__
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <errno.h>
#define ERR_MSG(msg) do{perror(msg);printf("%d\n",__LINE__);return -1;}while(0)
#endif
int main(int argc, const char *argv[])
{
FILE *fp=fopen("./homefile1","r+");
FILE *fp2=fopen("./homefile2","r+");
if(NULL==fp)
{
ERR_MSG("fopen error");
return -1;
}
printf("fopen success..\n");
char buf1[128]="";
while(1)
{
memset(buf1,0,sizeof(buf1));
if(NULL==fgets(buf1,sizeof(buf1),fp))
{
break;
}
if(EOF==fputs(buf1,fp2))
{
printf("fputs error \n");
}
}
return 0;
}
2.输入一个文件名,默认这个文件存在的,计算文件有几行,多大字节?
#ifndef __HIGH__
#define __HIGH__
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <errno.h>
#define ERR_MSG(msg) do{perror(msg);printf("%d\n",__LINE__);return -1;}while(0)
#endif
int main(int argc, const char *argv[])
{
FILE *fp=fopen("./homefile1","r+");
if(NULL==fp)
{
ERR_MSG("fopen error");
return -1;
}
printf("fopen success..\n");
//读取字符串
char buf[128]="";
int line=0;
while(1)
{
memset(buf,0,sizeof(buf));
if(NULL==fgets(buf,sizeof(buf),fp))
{
break;
}
line++;
}
printf("line=%d\n",line);
fseek(fp,0,SEEK_END);
long size=ftell(fp);
printf("size=%ld\n",size);
return 0;
}
3.思维导图
4.牛客网理论