I have been stuck on this for a while. I am not sure how to print our all the lines from my file in to C terminal and then save it into a data structure. My code is as following:
#include <stdio.h>
#include <stdlib.h>
#include "structure.h"
int main()
{
printf("Hello world!\n");
int i = 43;// number of iteration to save data in an array
int x;
FILE *fptr;
struct values *valuesPtr, values[x];
valuesPtr = &values[x];
if((fptr = fopen("energy.txt", "r")) == NULL)
{
printf ("Error opening file ");
return 0;
}
while (fptr != NULL)
{
for(x = 0; x < i; x++ )
{
fscanf(fptr, "%s %s %d", &valuesPtr->start_vertex, &valuesPtr->destination_vertex, &valuesPtr->num);
printf("\nStart vertex: %s \nDestination vertex: %s \nWeight: %d\n\n", valuesPtr->start_vertex, valuesPtr->destination_vertex, valuesPtr->num);
}
}
fclose(fptr);
return 0;
}
My structure.h file has the following structure:
struct values{
int num;
char start_vertex[250];
char destination_vertex[250];
};
It currently only shows the first line from my file. I want it to read all the lines from the file and then save that data in a data structure. Could you also tell me what the best data structure I could use to save all of the elements from my file in memory to be used later on.