I did the following code to read values from a text file. Text file looks like this.(in code it's named as annotation.txt)
299.jpg
480
640
3
dog
124
234
380
290
hand
789
456
234
123
wire
900
567
456
234
Here's the code.
#include <stdio.h>
#include <stdlib.h>
typedef struct object
{
char obj_name[20];
int x_min;
int y_min;
int x_max;
int y_max;
} Object;
typedef struct annotation
{
char img_name[20];
int width;
int height;
int num_obj;
Object objects[];
} Annotation;
void readAnnotation(char *fileName, Annotation *annot);
int main()
{
Annotation annot;
readAnnotation("annotation.txt", &annot);
printf("%s\n", annot.img_name);
printf("%d\n", annot.width);
printf("%d\n", annot.height);
printf("%d\n", annot.num_obj);
printf("0th object name for the 1st time %s\n", annot.objects[0].obj_name); // here it can be seen as 'dog'
for (int i = 0; i < 3; i++)
{
printf("%s\n", annot.objects[i].obj_name); // oth one disappear
printf("%d\n", annot.objects[i].x_min);
printf("%d\n", annot.objects[i].y_min);
printf("%d\n", annot.objects[i].x_max);
printf("%d\n", annot.objects[i].y_max);
};
printf("0th object name for the 2nd time %s\n", annot.objects[0].obj_name); //here it disappear
};
void readAnnotation(char *fileName, Annotation *annot)
{
FILE *fp;
fp = fopen(fileName, "r");
if (fp == NULL)
{
printf("\nError: Cannot open file");
exit(1);
};
fscanf(fp, "%s", annot->img_name);
fscanf(fp, "%d", &(annot->width));
fscanf(fp, "%d", &(annot->height));
fscanf(fp, "%d", &(annot->num_obj));
for (int i = 0; i < annot->num_obj; i++)
{
fscanf(fp, "%s", annot->objects[i].obj_name);
fscanf(fp, "%d", &(annot->objects[i].x_min));
fscanf(fp, "%d", &(annot->objects[i].y_min));
fscanf(fp, "%d", &(annot->objects[i].x_max));
fscanf(fp, "%d", &(annot->objects[i].y_max));
};
fclose(fp);
};
When I run the code, inside the for loop written in main() function makes the annot.objects[0].obj_name to disappear. When I check it before the for loop the value can be seen as 'dog'. But within the for loop and after the for loop, that value disappears. Can anyone tell me the issue with my code and give me a proper answer to solve this.