drewfiss90
drewfiss90

Reputation: 57

Read txt file line by line into char array C

I know this question has been asked a few times, but never in a way that helps me figure out my problem. Essentially, I am reading four text files, all single words separated by a new line, and wanting to store these in a char array. I first count the number of lines in the file and then create a new char array, but for the life of me, I cannot figure out how to get it to read correctly. The last two lines are just to test if it has read the entire file correctly and they always come back a NULL and the question mark symbol.

I want each line to be at the next index in the char array.

Any help would be awesome! Thank you ahead of time.

#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]);
}

Upvotes: 3

Views: 6928

Answers (2)

rphv
rphv

Reputation: 5537

Here's a slight modification of your code that might help you.

The main points:

  • 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]);                                                     
    }                                                                                                      
    

Upvotes: 3

np_6
np_6

Reputation: 511

I think that the problem is char contents[lines] and then fscanf(myfile,"%s",contents[i]) and the printf-s after. contents[i] is char type, and you want to read an array of chars into one char. contents needs to be declared as char* contents[lines] to be able to read a char array into contents[i].

Upvotes: 0

Related Questions