简体   繁体   English

如何多次读取文件C

[英]How to read file more than once C

I am trying to store a list of words from a file into a char* . 我正在尝试将文件中的单词列表存储到char *中 I am not to assume a max number of lines or a max number of characters. 我不假定最大行数或最大字符数。 So to combat this, I decided to do a run through of the .txt file to find the number of lines and the maximum number of characters so I can allocate memory to char * list. 因此,为了解决这个问题,我决定对.txt文件进行遍历,以找到行数和最大字符数,以便可以将内存分配给char * list。

However, when I used GDB to debug my program, it skips over the second runthough of the file to store in the words. 但是,当我使用GDB调试程序时,它会跳过文件的第二个运行时间来存储在单词中。 Why is it doing this and how do I fix it? 为什么要这样做,我该如何解决? Thanks! 谢谢!

void readFile(int argc, char** argv)
{
    FILE *myFile;
    char** list;
    char c;
    int wordLine = 0, counter = 0, i;
    int maxNumberOfChars = 0, numberOfLines = 0, numberOfChars = 0;

    myFile = fopen(argv[1], "r");

    if(!myFile)
    {
        printf("No such file or directory\n");
        exit(EXIT_FAILURE);  
    }

    while((c = fgetc(myFile)) !=EOF)
    {
        numberOfChars++;
        if(c == '\n')
        {
            if(maxNumberOfChars < numberOfChars)
                maxNumberOfChars += numberOfChars + 1;

            numberOfLines++;
        }
    }

    fseek(myFile, 0, SEEK_SET);

    list = malloc(sizeof(char*)*numberOfLines);

    for(i = 0; i < wordLine ; i++)
        list[i] = malloc(sizeof(char)*maxNumberOfChars);


    while((c = fgetc(myFile)) != EOF)
    {
        if(c == '\n' && counter > 0)
        {
            list[wordLine][counter] = '\0';
            wordLine++;
            counter = 0;
        }
        else if(c != '\n')
        {
            list[wordLine][counter] = c;
            counter++;
        }
    } 
    fclose(myFile);
}

You need to use fseek to reset the read-pointer of the file before your second loop. 您需要在第二个循环之前使用fseek重置文件的读取指针。

Add something like this 添加类似的东西

fseek(myFile, 0, SEEK_SET);

or 要么

rewind(myFile);

thanks to @ThomasPadron-McCarthy. 感谢@ ThomasPadron-McCarthy。

1st of all, your technique is bad because it is very slow. 首先,您的技术不好,因为它非常慢。 You can just allocate some memory and then use realloc if you need more. 您可以只分配一些内存,然后在需要时使用realloc。

2nd: you can use stat() on a file to know the size. 第二:您可以在文件上使用stat()来了解大小。 You won't know the number of lines in it but it could be useful. 您将不知道其中的行数,但这可能很有用。

3rd: you can use fseek() to move the cursor back to the beginning of the file, and in general to any position within the file. 第三:您可以使用fseek()将光标移回文件的开头,并且通常移至文件内的任何位置。

再次读取之前,请使用rewind(myFile)。

rewind(myFile)

but you don't need to read the whole file just to find out the number of chars. 但是您无需读取整个文件就可以找出字符数。 You can use this structure 您可以使用此结构

struct stat file_stat;
fstat(file_id, &file_stat);
int size_to_read = file_stat.st_size - 1;

you don need to know the number of lines, because you can can use realloc on line: 您不需要知道行数,因为您可以在行上使用realloc:

list=realloc(list,(sizeof(char*)));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM