Reputation: 319
I have an assignment for C and trying to solve it for hours and have not succeeded yet.
So I need to read a text file and append its content to struct type array,
text file =>
5 5 7
H 1 1 MILK White liquid produced by the mammals
H 2 1 IN Used to indicate inclusion within space, a place, or limits
H 3 3 BUS A road vehicle designed to carry many passengers
this is the code =>
#define LINESIZE 100
typedef struct
{
char *word; //word and corresponding hint
char *clue;
int x; //Starting x and y positions
int y;
char direction; //H for horizontal, V for vertical
int f; //solved or not
} Word_t;
Word_t* loadTextFile(FILE* file, int numofWords) {
//allocate memory for dynamic array
Word_t *arr = (Word_t*) malloc(sizeof (Word_t) * numOfWords);
char buffer[LINESIZE];
int i = -1; //to skip reading first line, I read it somewhere else
int val;
if (file != NULL) {
while(fgets(buffer, sizeof(buffer), file) != NULL) {
if (i > -1) {
printf("buffer: %s", buffer);
val = sscanf(buffer, "%s %d %d %s %[^\n]", &arr[i].direction, &arr[i].x, &arr[i].y, arr[i].word, arr[i].clue);
printf("print = %s %d %d %s %s\n", &arr[i].direction, arr[i].x, arr[i].y, arr[i].word, arr[i].clue);
}
if (val != 5 && i > -1)
printf("something wrong");
i++;
}
}
fclose(file);
return arr;
}
After I run the code, I get an output like this
buffer: H 1 1 MILK White liquid produced by the mammals
print = H 1 1 MILK White liquid produced by the mammals
buffer: H 2 1 IN Used to indicate inclusion within space, a place, or limits
Process finished with exit code -1073741819 (0xC0000005)
This error code means I have issues with pointers and memory, I think I have a problem with dynamic type array but I can't solve it. Help me please!
Upvotes: 0
Views: 111
Reputation: 121881
Q: How to read text file to struct type array?
A: You're doing everything right: allocate space for the array, open your file and read a line at a time, parse the data from the line and read it into the next array element. Good :)
Q: How do I prevent 0xC0000005
(segmentation violation)?
A: You need to allocate space for the STRINGS in your structs!
EXAMPLE:
#define LINESIZE 100
#define MAX_STRING 80
typedef struct
{
char word[MAX_STRING]; //word and corresponding hint
char clue[MAX_STRING];
int x; //Starting x and y positions
int y;
char direction; //H for horizontal, V for vertical
int f; //solved or not
} Word_t;
ALSO:
&arr[i].direction
is correct, but...%c
, not %s
EXAMPLE:
val = sscanf(buffer, "%c %d %d %s %[^\n]", &arr[i].direction, &arr[i].x, &arr[i].y, arr[i].word, arr[i].clue);
Upvotes: 2
Reputation: 75062
word
and clue
before reading strings there.%s
is for reading strings (null-terminated sequence of characters), so it cannot be used for read one character to char
variable direction
. You should use %c
for that. You can add a whitespace before that to have sscanf()
skip whitspace characters.%s
to print one character is also wrong. %s
in printf()
is for printing strings.malloc()
family is considered as a bad practice./* allocate memory */
arr[i].word = malloc(10240);
arr[i].clue = malloc(10240);
/* use correct format specifiers */
val = sscanf(buffer, " %c %d %d %10239s %10239[^\n]", &arr[i].direction, &arr[i].x, &arr[i].y, arr[i].word, arr[i].clue);
printf("print = %c %d %d %s %s\n", arr[i].direction, arr[i].x, arr[i].y, arr[i].word, arr[i].clue);
Upvotes: 2