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.
The way you initialize valuesPtr
is not exact. valuesPtr = &values[x];
is failed because the maximum index that array values
can take is x-1
(from 0 to x-1
). It is a pointer, so allocate it (do not forget initialize x
first):
struct values *valuesPtr = malloc(sizeof(struct values) * x);
if (!valuesPtr)
return -1;
Or using 1D array:
/*init x here*/
struct values valuesPtr[x]
And if you want to get value by using fscanf
, do not use &
for char
array. Use as below:
int num = fscanf(fptr, "%s %s %d", valuesPtr[i].start_vertex, valuesPtr[i].destination_vertex, &valuesPtr[i].num);
// verify the the number (num) of input items successfully matched and assigned if you want.
UPDATE test:
struct values{
int num;
char start_vertex[250];
char destination_vertex[250];
};
int main()
{
printf("Hello world!\n");
int x = 3; // The number of lines that you want to get from file.
FILE *fptr;
struct values *valuesPtr = malloc(sizeof(struct values) * x);
if (!valuesPtr)
return -1;
if((fptr = fopen("text.txt", "r")) == NULL)
{
printf ("Error opening file ");
return 0;
}
for(int i = 0; i < x; i++ )
{
fscanf(fptr, "%s %s %d", valuesPtr[i].start_vertex, valuesPtr[i].destination_vertex, &valuesPtr[i].num);
printf("\nStart vertex: %s \nDestination vertex: %s \nWeight: %d\n\n", valuesPtr[i].start_vertex, valuesPtr[i].destination_vertex, valuesPtr[i].num);
}
free(valuesPtr);
fclose(fptr);
return 0;
}
text.txt
ab cd 9
ef gh 10
ii cc 11
The output:
Hello world!
Start vertex: ab
Destination vertex: cd
Weight: 9
Start vertex: ef
Destination vertex: gh
Weight: 10
Start vertex: ii
Destination vertex: cc
Weight: 11
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.