作业:fgets获取文件的行号
#include<myhead.h>
int main(int argc, const char *argv[])
{
FILE *fp = fopen("./1.txt","r");
if(fp==NULL)
{
printf("fopen");
return -1;
}
char buff[100];
int sub = 0;
while(1)
{
if(fgets(buff,sizeof(buff),fp)==NULL)
{
break;
}
sub++;
}
printf("行号有%d行\n",sub);
fclose(fp);
return 0;
}
fgets和fputs拷贝文件
#include<myhead.h>
int main(int argc, const char *argv[])
{
//fgets和fputs
//将1.txt内容拷贝到2.txt中
if(argc != 3)
{
printf("外部参数错误\n");
return -1;
}
FILE *fp1 = fopen("./1.txt","r");
if(fp1==NULL)
{
perror("fp1");
return -1;
}
FILE *fp2 = fopen("./2.txt","w");
if(fp2==NULL)
{
perror("fp2");
return -1;
}
char buff[100];
while(1)
{
if(fgets(buff,sizeof(buff),fp1)==NULL)
{
break;
}
fputs(buff,fp2);
}
//while(fgets(buff,sizeof(buff),fp1)!=NULL)
//{
// fputs(buff,fp2);
//}
fclose(fp1);
fclose(fp2);
printf("拷贝成功\n");
fp2 = fopen("./2.txt","r");
if(fp2==NULL)
{
perror("fp2");
return -1;
}
while(fgets(buff,sizeof(buff),fp2)!=NULL)
{
fputs(buff,stdout);
}
fclose(fp2);
return 0;
}