利用标准IO实现读取文件每一行的内容

#include 
#include 
#include 
int main(int argc, char **argv)
{
    char *linebuf;
    size_t linesize;
	FILE *fp;
	int count;
	if(argc < 2)
	{
		fprintf(stderr,"Usage.....\n");
		exit(1);
	}
	//从终端命令行参数得去文件
	fp = fopen(argv[1], "r");
	if(fp == NULL)
	{
		perror("fopen_argv[1]");
		exit(1);
	}
    linebuf = NULL;
    linesize = 0;
	while(1)
    {	
        //利用getlien读取文件每一行数据
        if(getline(&linebuf, &linesize, fp) <  0)
            break;
		printf("%s\n",linebuf);
        printf("%d\n",strlen(linebuf));
        //printf("%d\n",linesize);   
    }
	fclose(fp);
	exit(0);
}

你可能感兴趣的:(c语言,linux,服务器,算法)