fgets获取文件的行号
#include <myhead.h>
int main(int argc, const char *argv[])
{
FILE *fp;
char a[50];
int buff = 0;
fp = fopen("1.txt", "r");
if (fp == NULL)
{
perror("无法打开文件");
return 1;
}
while (fgets(a, sizeof(a), fp) != NULL)
{
buff++;
}
printf("行数 %d", buff);
fclose(fp);
return 0;
}
fgets和fputs拷贝文件
#include <stdio.h>
#include <string.h>
#include <myhead.h>
int main(int argc, const char *argv[])
{
FILE *fp1, *fp2;
char line[100];
fp1 = fopen("1.txt", "r");
if (fp1 == NULL)
{
perror("无法打开源文件");
return 1;
}
fp2 = fopen("2.txt", "w");
if (fp2 == NULL)
{
perror("无法打开目标文件");
fclose(fp1);
return 1;
}
while (fgets(line, sizeof(line), fp1) != NULL) {
fputs(line,fp2);
}
fclose(fp1);
fclose(fp2);
printf("文件拷贝完成\n");
return 0;
}