将文本文件逐行读入字符数组C

3
我知道这个问题已经被问了几次,但从来没有以一种帮助我解决问题的方式。本质上,我正在读取四个文本文件,所有单词都用新行分隔,并希望将它们存储在一个char数组中。我首先计算文件中的行数,然后创建一个新的char数组,但是我无论如何都无法弄清楚如何正确地读取它。最后两行只是为了测试它是否正确读取整个文件,它们总是返回NULL和问号符号。
我希望每一行都在char数组的下一个索引处。
任何帮助都将非常棒!提前感谢您。
#include <omp.h>
#include <stdio.h>
#include <stdlib.h>

void countAnagrams(char* fileName);

void main ()
{
    char *fileNames[] = {"AnagramA.txt","AnagramB.txt","AnagramC.txt","AnagramD.txt"};

    countAnagrams(fileNames[0]);
    countAnagrams(fileNames[1]);
    countAnagrams(fileNames[2]);
    countAnagrams(fileNames[3]);
}

void countAnagrams(char* fileName)
{
    int anagramCount = 0;
    int ch, lines = 0;  

    //Count number of lines in file
    FILE *myfile = fopen(fileName, "r");
    do
    {
        ch = fgetc(myfile);
        if(ch == '\n')
            lines++;
    }while(ch != EOF);

    char contents[lines];
    int i = 0;
    for(i=1;i<lines;i++)
    {
        fscanf(myfile,"%s",contents[i]);
    }
    fclose(myfile);

    printf("%.12s\n",fileName);
    printf("number of lines: %d\n", lines);

    printf("first thing: %s\n", contents[0]);
    printf("last thing: %s\n", contents[lines-1]);
}

在托管环境中,void main() 是无效的。 - too honest for this site
四个文本文件,每个文件里都是由一个新行分隔的单词。这意味着lines ++;会发生三次? - chux - Reinstate Monica
2
启用警告,你的编译器想要与你进行严肃的对话。而 char contents[lines]; 是一个 char 数组。你的代码中有太多错误了。 - too honest for this site
2
在计算完文件的行数后,您的文件指针会停留在文件末尾。您需要关闭并重新打开文件,或者使用 rewind 函数将文件指针重置到文件开头。 - kaylum
在使用 rewind() 后,你可能需要使用 for(i=0;i<lines;i++) 或者 for(i=0;i<=lines;i++) - chux - Reinstate Monica
显示剩余3条评论
2个回答

3
以下是可以帮助你的代码轻微修改。
主要要点:
  • You can use getline() instead of fscanf(). fscanf() can be used to read line-by-line, but it needs an explicit check for the end of line condition. getline() does this automatically.
  • As kaylum pointed out, it's necessary to rewind() the file pointer back to the beginning of the file after counting the number of lines.

    #include <omp.h>                                                                                       
    #include <stdio.h>                                                                                     
    #include <stdlib.h>                                                                                    
    
    void countAnagrams(char* fileName);                                                                    
    
    void main ()                                                                                           
    {                                                                                                      
        char *fileNames[] = {"AnagramA.txt","AnagramB.txt","AnagramC.txt","AnagramD.txt"};                 
    
        countAnagrams(fileNames[0]);                                                                       
        countAnagrams(fileNames[1]);                                                                       
        countAnagrams(fileNames[2]);                                                                       
        countAnagrams(fileNames[3]);                                                                       
    }                                                                                                      
    
    void countAnagrams(char* fileName)                                                                     
    {                                                                                                      
        int anagramCount = 0;                                                                              
        int ch, lines = 0;                                                                                 
    
        //Count number of lines in file                                                                    
        FILE *myfile = fopen(fileName, "r");                                                               
        do                                                                                                 
        {                                                                                                  
            ch = fgetc(myfile);                                                                            
            if (ch == '\n')                                                                                
                lines++;                                                                                   
        } while (ch != EOF);                                                                               
    
        rewind(myfile);                                                                                    
    
        char *contents[lines];                                                                             
        int i = 0;                                                                                         
        size_t len = 0;                                                                                    
        for(i = 0; i < lines; i++)                                                                         
        {
            contents[i] = NULL;
            len = 0;                                                                                
            getline(&contents[i], &len, myfile);                                                           
        }                                                                                                  
        fclose(myfile);                                                                                    
    
        printf("%.12s\n",fileName);                                                                        
        printf("number of lines: %d\n", lines);                                                            
    
        printf("first thing: %s\n", contents[0]);                                                          
        printf("last thing: %s\n", contents[lines-1]);                                                     
    }                                                                                                      
    

4
在调用getline()之前,您需要将contents[i]设置为0(NULL),并且我也会在每次迭代中设置len = 0;。这样,getline()就知道它需要每次分配。由于content条目中存在不确定的值和len的增加长度,您将面临严重的不愉快。 - Jonathan Leffler
如果文本文件没有以'\n'结尾,那么contents[lines]会比实际行数小1。 - chux - Reinstate Monica
谢谢。已添加初始化。 - rphv

0

我认为问题出在char contents[lines]fscanf(myfile,"%s",contents[i])以及后面的printf语句。 contents[i]char类型,而你想要将一个字符数组读入到一个字符中。需要将contents声明为char* contents[lines]才能够将字符数组读入到contents[i]中。


1
这是其中一个问题,绝不是代码遇到的第一个问题。而且建议声明一个 char * 数组的解决方案是不完整的,因为需要为每个条目分配内存。 - kaylum

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接