C – 如何读取文件的所有行

我不确定如何读取文件的所有行,atm它只读取文本文件中代码的第一行。 有人可以告诉我如何让它读取所有行吗?

#include  #include  #include  int main(int argc, char **argv) { FILE *fp; fp = fopen("specification.txt", "r"); char ** listofdetails; listofdetails = malloc(sizeof(char*)*6); listofdetails[0] = malloc(sizeof(char)*100); fgets(listofdetails[0], 100, fp); /*strcpy(listofdetails[0], "cars");*/ printf("%s \n", listofdetails[0]); free(listofdetails[0]); free(listofdetails); fclose(fp); return 0; } 

我的文字档案:

 10X16 de4 dw9 ds8 g8,7 m3,4 h6,5 p2,2 10X16 de4 dw9 ds8 g8,7 m3,4 h6,5 p2,2 10X16 de4 dw9 ds8 g8,7 m3,4 h6,5 p2,2 

 #include  #include  int main(int argc, const char * argv[]) { { FILE *file = fopen("specification.txt", "r"); char currentline[100]; assert(file != NULL); while (fgets(currentline, sizeof(currentline), file) != NULL) { fprintf(stderr, "got line: %s\n", currentline); /* Do something with `currentline` */ } (void)fclose(file); } } 

如果你想逐行阅读’specification.txt’文本文件,你可以这样做:

  char row[255]; FILE *fp; fp = fopen( "specification.txt", "r" ); if ( fp == NULL ) { // error handling.. } while ( fgets( row, sizeof( row ), fp ) != NULL ) { puts( row ); } fclose( fp ); 

确保你的“行”缓冲区足够大。

使用getline(3) ,假设您的操作系统和libc符合Posix2008(例如在Linux上):

 FILE *fp = fopen("specification.txt", "r"); if (!fp) { perror("specification.txt"); exit(EXIT_FAILURE); }; size_t sizelist = 6; // initial guess of number of lines size_t nblines = 0; char ** listofdetails = calloc(sizelist, sizeof(char*)); if (!listofdetails) { perror("initial calloc"); exit(EXIT_FAILURE); }; char*curline = NULL; size_t cursize = 0; do { ssize_t curlen = getline(&curline, &cursize, fp); if (curlen < 0) break; if (nblines >= sizelist) { size_t newsizelist = 3*sizelist/2+5; char**newlist = calloc(newsizelist, sizeof(char*)); if (!newlist) { perror("growing calloc"); exit(EXIT_FAILURE); }; memcpy (newlist, sizelist, nbline*sizeof(char*)); sizelist = newsizelist; }; if (!(sizelist[nblines++] = strdup(curline))) { perror("strdup"); exit(EXIT_FAILURE); }; } while (!feof(fp)); 

上面的代码可以接受尽可能多的行,以及系统资源允许的大行。 在我强大的笔记本电脑(16Gbytes RAM)上,我可能能够读取超过一百万行的文件,每行近千个字符(或一行数百万字符的文件)。

在(你的程序)结束时,你应该更好地释放内存:

 for (size_t ix=0; ix 

另一个例子:如果你的目标是POSIX平台,你可以使用getline 。 它分配存储线所需的空间,但你必须自己释放它。 如果出现错误或EOF,则getline返回-1。

 #include  #include  #include  int main(int argc, char **argv) { FILE *fp; fp = fopen("specification.txt", "r"); char** listofdetails; int ROWS = 6; listofdetails = calloc(ROWS,sizeof(char*)); int i; size_t len; ssize_t readed; for (i = 0; i < ROWS; i++) { if ((readed = getline(&listofdetails[i], &len, fp)) == -1) { break; } } for (i = 0; i < ROWS; i++) { if (listofdetails[i] == NULL) { break; } printf("%s\n",listofdetails[i]); free(listofdetails[i]); } fclose(fp); free(listofdetails); return 0; } 

您可以通过以下方式阅读整个文件:

 char *buffer; FILE *fp = fopen("filename.txt", "rb"); if (fp != NULL) { fseek(fp, 0L, SEEK_END); long stell = ftell(fp); rewind(fp); buffer = (char *)malloc(stell); if (buffer != NULL) { fread(buffer, stell, 1, fp); fwrite(buffer, stell, 1, stdout); fclose(fp); fp = NULL; free(buffer); } } 

上面的代码读取filename.txt文件并将其打印到stdout