前面6行不处理,读到第7行时保存,知道读到没有后续结果为止;
举个例子:
#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
FILE * fp;
char * line = NULL;
size_t len = 0;
ssize_t read;
fp = fopen("/etc/motd", "r");
if (fp == NULL)
exit(EXIT_FAILURE);
while ((read = getline(&line, &len, fp)) != -1)
{
printf("Retrieved line of length %zu :\n", read);
printf("%s", line);
}
if (line)
free(line);
exit(EXIT_SUCCESS);
}
追问写了这么多代码辛苦了,但是你写的程序运行不出来啊。说getline没定义
所以我加了,
#include <string>
using namespace std;
但是后面while((read=getline(&line, &len,fp))!=-1)这句显示