getdelim函数

利用getdelim函数分割读取字段,将文件制表符替换为空格符

 

 1 #include <stdio.h>
 2 #include <stdlib.h>
 3 
 4 int main(int argc, char *argv[])
 5 {
 6     char *line = NULL;
 7     FILE *fp;
 8     size_t len = 0;
 9     ssize_t read;
10 
11     fp = fopen(argv[1], "r");
12     if (fp == NULL)
13         exit(EXIT_FAILURE);
14 
15     while ((read = getdelim(&line, &len, '\t' ,fp)) != -1) 
16     {
17         line[read-1] = '\0';
18         printf("%s ",line);
19     }
20     
21     if (line)
22         free(line);
23     fclose(fp);
24     exit(EXIT_SUCCESS);
25 
26 }

 

posted @ 2020-10-19 10:08  天使不设防  阅读(1224)  评论(0编辑  收藏  举报