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

plicqrtu  于 2023-10-16  发布在  其他
关注(0)|答案(5)|浏览(107)

我不确定如何读取文件的所有行,atm它只读取文本文件中代码的第一行。有人能教我怎么让它读所有的台词吗?

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

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
dzjeubhm

dzjeubhm1#

#include <stdio.h>
#include <assert.h>

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);
}
thtygnil

thtygnil2#

如果你想逐行读取'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 );

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

1yjd4xko

1yjd4xko3#

使用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));

上面的代码可以在系统资源允许的情况下接受尽可能多的行和更大的行。在我强大的笔记本电脑(16GB RAM)上,我可能能够读取超过一百万行的文件,每行近一千个字符(或者一个单行数百万个字符的文件)。
在程序结束时,你应该更好地释放内存:

for (size_t ix=0; ix<nblines; ix++) {
   free(sizelist[ix]), sizelist[ix] = NULL;
}
free(sizelist), sizelist = NULL;
free(curline), curline = NULL; cursize = 0;
x6h2sr28

x6h2sr284#

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

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

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;
}
cclgggtu

cclgggtu5#

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

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

相关问题