#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(int argc,const char *argv[])
{
FILE *fp;
size_t len = 0;
char *buff = NULL;
ssize_t read;
if(argc != 2)
{
fprintf(stderr,"usage: %s <src>\n",argv[0]);
exit(1);
}
if((fp=fopen(argv[1],"r")) == NULL)
{
perror("fopen()");
exit(1);
}
while((read = getline(&buff,&len,fp)) != -1)
{
printf("\n buff=0x%x \n", buff);
//buff[read-1]='\0'; 去掉行末换行符
fprintf(stdout," line: %s",buff);
}
fclose(fp);
exit(0);
}
输出:
$ ./c_getline_test.exx c_getline_test.c
buff=0xc51d70
line: #include <stdio.h>
buff=0xc51d70
line: #include <stdlib.h>
buff