Если вы хотите сохранить значение в массиве, создайте массив и скопируйте в него значение.
int num[nLines];
char start_vertex[nLines][250];
char destination_vertex[nLines][250];
Скопируйте данные, вы можете использовать strcpy()
для копирования строки в строку в c
for(int i = 0; i < nLines; i++ ) {
if (fscanf ...
...
num[i] = valuesPtr[i].num;
strcpy(start_vertex[i], valuesPtr[i].start_vertex);
strcpy(destination_vertex[i], valuesPtr[i].destination_vertex);
...
}
Тест:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct value {
int num;
char start_vertex[250];
char destination_vertex[250];
} value;
int main()
{
const int nLines = 43; // number of lines in my text file
FILE * fptr;
value * valuesPtr = malloc(sizeof(value) * nLines);
if (!valuesPtr) {
puts("cannot allocate memory");
return -1;
}
if((fptr = fopen("text.txt", "r")) == NULL)
{
perror("Error opening file");
return -1;
}
// The array for storing all num that you get from energy file
int num[nLines];
// The array of string for storing all start_vertex that you get from energy file
char start_vertex[nLines][250];
// The array of string for storing all destination_vertex that you get from energy file
char destination_vertex[nLines][250];
for(int i = 0; i < nLines; i++ )
{
if (fscanf(fptr, "%249s %249s %d",
valuesPtr[i].start_vertex,
valuesPtr[i].destination_vertex,
&valuesPtr[i].num) != 3) {
printf("errored file line %d\n", i);
break;
}
num[i] = valuesPtr[i].num;
strcpy(start_vertex[i], valuesPtr[i].start_vertex);
strcpy(destination_vertex[i], valuesPtr[i].destination_vertex);
printf("\nStart vertex: %s \nDestination vertex: %s \nWeight: %d\n\n",
valuesPtr[i].start_vertex, valuesPtr[i].destination_vertex, valuesPtr[i].num);
printf("\nStart vertex: %s \nDestination vertex: %s \nWeight: %d\n\n",
start_vertex[i], destination_vertex[i], num[i]);
}
free(valuesPtr);
fclose(fptr);
return 0;
}