Click here to Skip to main content
15,868,141 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a text file which has lines in the following format.
BASIC
The
Quick 
Brown
Fox
Jumps
Over 
The 
Lazy
Dog


I try to read the contents of the file with the following C code snippet but its printing empty strings. Why is this code snippet not working.

What I have tried:

int main()
   {
   //declare the file pointer
   FILE* dictionary = fopen("data.txt", "r");
   //check if the file is null
   if(dictionary == NULL){
       printf("%s", "The fie could not be opened");
       return EXIT_FAILURE;
    }
   //count the number of lines in the file
   int lineCount =0;
   char c;
       while ((c = fgetc(dictionary)) != EOF) {
           if (c == '\n') {
               lineCount++;
           }
       }
       //declare a buffer to store each word read
       char word[50];
       while (fgets(word, sizeof(word), dictionary)!= NULL) {
           printf("%s", word);
       }
   }
Posted
Updated 2-Jun-23 22:51pm
v2

1 solution

Look at your code, you read all the data but you do not do anything with it:
C++
   int lineCount =0;
   char c;
       while ((c = fgetc(dictionary)) != EOF) {
           if (c == '\n') {
               lineCount++;
           }
       }
//
// At this point you have read every character and reached the end of the file.
// So the first call to fgets below will fail as there is nothing to read.
//
       //declare a buffer to store each word read
       char word[50];
       while (fgets(word, sizeof(word), dictionary)!= NULL) {
           printf("%s", word);
       }
   }


So remove that first loop where you read it character by character, and just use the second loop to read and print the contents.

[edit]
OK, now we know what you are trying to do. So you first need to count the lines to know how much data to save. Once you have that then you need to allocate space to save the data. You do this by creating a dynamic array of char* types, and each one will then need to point to an array of characters. You then read each line into a new array and save its address in the array. Something like the following:
C++
//  1. first we need to count the lines in the file
int linecount = 0;
char line[50]; // space for a line of text up to 49 characters long
while (fgets(line, sizeof(line), dictionary)!= NULL)
{
    linecount++;
}
printf("%d lines read from file\n", linecount);

// 2. Now allocate an array to hold the pointers to the separate lines:
char** lines = (char**)malloc(linecount * sizeof(char*));
fseek(dictionary, 0, SEEK_SET); // rewind the file
linecount = 0;
do
{
    char* next = (char*)malloc(50); // space for the next line
    if (fgets(next, 50, dictionary) == NULL)
    {
        break; // reached end of file
    }
    lines[linecount] = next; // add this line pointer to the array
    linecount++; // update the index for next time
} while (1);

// 3. now print the lines in the array
int index;
for (index = 0; index < linecount; index++)
{
    printf("%s", lines[index]);
}

There is no error checking in this code and there are a few things that need improving, but it should give you something to start with.

[/edit]
 
Share this answer
 
v2
Comments
Tim the Gamer 3-Jun-23 5:05am    
I need the first loop for knowing the number of lines in the file but I can drop it because I can use dynamic memory allocation for the words array.
Tim the Gamer 3-Jun-23 5:07am    
removing the first loop fixes the problem but I how to count the number of lines in the text file without it?
Tim the Gamer 3-Jun-23 5:21am    
I need a way to reset the file pointer for the second iteration to not return null
Richard MacCutchan 3-Jun-23 5:29am    
No all you need is the second loop. Each time you call fgets it returns the next line of the file. So you can count the lines in that loop; although, since you do not do anything with the line count it is somewhat redundant.
Tim the Gamer 3-Jun-23 5:41am    
I have reset the pointer to the beginning of the file with this code `fseek(dictionary, 0, SEEK_SET);`, Now I have a problem with the difference between an array of strings and array of characters in C, they seem to be the same thing to me in terms of declaration.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900