作业:fgets获取文件的行号。
程序代码:
#include<myhead.h>
int main(int argc, const char *argv[])
{
FILE *fp1 = fopen("./01.txt","r");
if(fp1 == NULL)
{
perror("fopen");
return -1;
}
char buff[20] = "";
int count = 0;
int i =0;
while(fgets(buff,sizeof(buff),fp1)!=NULL)
{
if(buff[i]=='\n')
i++;
else
{
count++;
i = 0;
}
printf("%s",buff);
}
printf("有%d行\n",count);
fclose(fp1);
fp1 =NULL;
return 0;
作业:fgets和fputs拷贝文件。
程序代码:
#include<myhead.h>
int main(int argc, const char *argv[])
{
FILE *fp1 = fopen("./01.txt","r");
if(fp1 == NULL)
{
perror("fopen");
return -1;
}
FILE *fp2 = fopen("./02.txt","r");
if(fp2 == NULL)
{
perror("fopen");
return -1;
}
char buff[20] = "";
while(fgets(buff,sizeof(buff),fp1)!=NULL)
{
printf("%s",buff);
fputs(buff,fp2);
}
char buff1[20] = "";
while(fgets(buff1,sizeof(buff1),fp2)!=NULL)
printf("%s",buff1);
fclose(fp1);
fclose(fp2);
fp1 =NULL;
fp2 =NULL;
return 0;