C语言,按行处理代码
按行读取文件,写工具时经常要处理文件, 而且是按行处理。以下程序就很有用linux 下使用gcc测试通过。#include <stdio.h>#include <unistd.h>
#include <fcntl.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
void processline(char *p)
{
if (pR!=NULL)
printf("read=%s\n", p);
}
int main(int argc, char *argv[])
{
FILE *f;
char sLine, fname;
sprintf(fname, "./very.log");
f = fopen(fname,"r");
if (f==NULL)
{
printf("file %s open error\n", fname);
return 0;
}
while(!feof(f))
{
memset(sLine,0,1024);
fgets(sLine,1024,f);
if(feof(f)) break;
processline(sLine);
}
fclose(f);
return 0;
}
页:
[1]