fgets()函数每次只获取一行,并且第一次只获取文本第一行,第二次获取时会忽略第一行,获取文本第二行,第n次调用函数获取第n行
#include <string.h>
#include <stdio.h>
int main()
{
FILE *fp = NULL;
char buf[64] = {0};
int i;
fp = fopen("/proc/meminfo", "r");// cat /proc/meminfo
if (fp == NULL)
{
printf("fopen error\n");
return 0;
}
if (fgets(buf, sizeof(buf), fp) == NULL)
{
printf("fgets error\n");
return 0;
}
printf("buf1 = %s", buf);//Print the first line of the file
if (fgets(buf, sizeof(buf), fp) == NULL)
{
printf("fgets error\n");
return 0;
}
printf("buf2 = %s", buf);//Print the second line of the file
return 0;
}