Solving Confusion with fgets & 2D Arrays

  • Thread starter Thread starter VirObitus
  • Start date Start date
  • Tags Tags
    Confused
Click For Summary
SUMMARY

This discussion addresses the issue of reading lines from a text file and storing them in a 2D array using C programming. The original code incorrectly assigns the same pointer to each element of the responses array, leading to all elements containing the last line read. The solution involves changing the declaration of the saythis variable to a character array and using strdup to create separate copies of each line read from the file. Additionally, it is noted that fgets includes the newline character, which may need to be removed if not desired.

PREREQUISITES
  • Understanding of C programming syntax and structures
  • Familiarity with file handling in C using fopen and fgets
  • Knowledge of dynamic memory allocation with strdup
  • Basic understanding of arrays and pointers in C
NEXT STEPS
  • Learn about dynamic memory management in C, specifically using malloc and free
  • Research how to manipulate strings in C, including functions like strtok and strcspn
  • Explore error handling techniques in C for file operations
  • Investigate the use of 2D arrays in C for more complex data storage
USEFUL FOR

C programmers, software developers working with file I/O, and students learning about arrays and memory management in C.

VirObitus
Messages
19
Reaction score
0
Im attempting to read a .txt file, and store each line as a separate instance of an array. The code I've written causes each instance of the responses array to become the last line in the .txt file. Do I need to make a 2D array to get what I want? If so, how would I go about doing that?

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

void main(void)
{
  FILE* spResponses;
  char* saythis[30];
  char* responses[30];
  int i = 0;
  
    if ((spResponses = fopen("c:\\responses2.txt", "r")) == NULL)                           //open file
    {
       printf("Error opening input file!\n");
    }
    else
    {

            while(fgets(saythis, sizeof (saythis), spResponses) != NULL)
            {
         
                responses[i] = saythis;
                i++;
                
            }
            fclose(spResponses);
        
    }
    
  printf("%s\n\n", responses[0]);
  printf("%s\n\n", responses[1]);
  printf("%s\n\n", responses[2]);
  printf("%s\n\n", responses[3]);
  printf("%s\n\n", responses[4]);
          
  system("PAUSE");	
  return 0;
}
 
Technology news on Phys.org
Your program looks OK except for just a few points:
1. If the lines of the files do not exceed 30 characters, the line
char* saythis[30];
should be changed to
char saythis[30];
to get a buffer array of 30 characters including the '\0' terminator.
The line as is assigns memory for 30 pointers to arrays, but no memory for the strings themselves.
2. The line
responses = saythis;
could be changed for
responses =strdup(saythis);
The modified version will assign memory to make a copy of each string.
Otherwise all members of responses will contain the pointer to the array saythis, meaning that they all have values of the last line.
3. Note that fgets retrieves the new-line character (at the end of the line). If this is not what you want, you may want to remove it.
 
thanks a ton, that was exactly what i needed!
 

Similar threads

Replies
7
Views
2K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 6 ·
Replies
6
Views
3K
  • · Replies 4 ·
Replies
4
Views
2K
Replies
3
Views
3K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 32 ·
2
Replies
32
Views
4K
  • · Replies 4 ·
Replies
4
Views
2K
Replies
47
Views
6K
  • · Replies 11 ·
Replies
11
Views
35K