2015/11/23

C, Read text file and process line by line.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
#define _BUFFER_SIZE_   2048
 
FILE    *fd;
int     line;
char    buf[_BUFFER_SIZE_];
 
fd = fopen("filename", "r");
     
if (fd) {
    line = 0;
    while(fgets(buf, _BUFFER_SIZE_, fd) != NULL) {
        /* process line by line */
        line++;
    }
    fclose(fd);
} else {
    /* open file error handle */
}