
1.读取文件,效果类似cat的功能
#include <25051head.h>
int main(int argc, const char *argv[])
{
FILE* fp=fopen("./my.txt","r");
if(fp==NULL)
{
ERRLOG("fopen error");
}
while(1)
{
char buf[128]={0};
if(fgets(buf,128,fp)==NULL)
break;
printf("%s",buf);
}
fclose(fp);
return 0;
}
2.通过argc和argv输入文件名和路径,请实现文件的拷贝
#include <25051head.h>
int main(int argc, const char *argv[])
{
FILE* fp=fopen("./my.txt","r+");
if(fp==NULL)
{
ERRLOG("fopen error");
}
FILE* fp1=fopen("./My.txt","r+");
if(fp1==NULL)
{
ERRLOG("fopen error");
}
while(1)
{
int res_get=fgetc(fp);
if(res_get==EOF)
{
break;
}
fseek(fp1,0,SEEK_END);
int res_put=fputc(res_get,fp1);
}
fseek(fp1,0,SEEK_SET);
while(1)
{
int res=fgetc(fp1);
if(res==EOF)
{
break;
}
printf("%c",res);
}
fclose(fp);
return 0;
}
3.通过argc和argv输入文件名和路径,请计算文件的行数
#include <25051head.h>
int main(int argc, const char *argv[])
{
FILE* fp=fopen("./my.txt","r");
if(fp==NULL)
{
ERRLOG("fopen error");
}
int line=0;
while(1)
{
char buf[128]={0};
if(fgets(buf,128,fp)==NULL)
{
break;
}
line++;
}
printf("%d\n",line);
fclose(fp);
return 0;
}
374

被折叠的 条评论
为什么被折叠?



