Solving Confusion with fgets & 2D Arrays

  • Thread starter VirObitus
  • Start date
  • Tags
    Confused
In summary, the conversation was about reading a .txt file and storing each line as a separate instance of an array. The code provided caused each instance to become the last line in the file, leading to the question of whether a 2D array was needed. The expert suggests some modifications to the code, such as changing the type of the buffer array and using the strdup function, to address the issue. They also mention the inclusion of the new-line character in the retrieved lines and suggest removing it if necessary.
  • #1
VirObitus
19
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
  • #2
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.
 
  • #3
thanks a ton, that was exactly what i needed!
 

1. How do I use fgets to read user input into a 2D array?

The fgets function in C is used to read a line of input from the user, and it can be used to read into a 2D array by using a loop to read each line of input and storing it in a specific row of the array. You can use the fgets function in conjunction with the sscanf function to read and store specific data types into the array.

2. Why am I getting confused with using fgets and 2D arrays?

Fgets and 2D arrays can be confusing because they involve multiple levels of indexing and sometimes require nested loops. It's important to carefully plan and visualize the data structure you are trying to create before writing your code to avoid confusion.

3. How can I avoid going out of bounds when using fgets and 2D arrays?

To avoid going out of bounds when using fgets and 2D arrays, it's important to carefully manage your loops and ensure that you are only accessing elements within the bounds of your array. You can use conditional statements to check the size of your array and prevent any out of bounds errors.

4. Can I use fgets to read multiple lines of input into a 2D array?

Yes, you can use fgets to read multiple lines of input into a 2D array by using a loop to read each line of input and storing it in a specific row of the array. You can also use the sscanf function to read and store specific data types into the array.

5. Is fgets the only way to read user input into a 2D array?

No, there are other methods for reading user input into a 2D array such as using the scanf function or using pointers to dynamically allocate memory for the array. However, fgets is a commonly used function for reading user input into a 2D array in C.

Similar threads

  • Programming and Computer Science
Replies
4
Views
735
  • Programming and Computer Science
Replies
7
Views
1K
  • Programming and Computer Science
Replies
6
Views
2K
  • Programming and Computer Science
Replies
32
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
754
  • Programming and Computer Science
Replies
1
Views
944
  • Programming and Computer Science
Replies
4
Views
1K
  • Programming and Computer Science
2
Replies
47
Views
4K
  • Programming and Computer Science
Replies
1
Views
1K
  • Programming and Computer Science
Replies
4
Views
2K
Back
Top