I'm trying to get the line:
allTowns[i - 1].startTown = allTowns[i].endTown;
to work, but whenever i run it i get an error code stating
"Error: Assignment to expression with array type"
Does anyone know why this is? My end goal is to run through the file energy.txt and put its contents into the array allTowns[1024], i got this to work, but now i would like to make every second line the same as the previous but with startTown, endTown swapped.
e.g. a file that looks like this:
Hull York 30
Manchester Derby -21
would create a struct i can print that looks like this:
Hull York 30
York Hull 30
Manchester Derby -21
Derby Manchester -21
Thank you very much for any help anyone can provide or suggestions, i'm new to C, the code I've pasted below is my code, but I have missed off the includes (<stdio.lib> and <stdio.h>). as it is in a header file
struct intertownEnergy {
char startTown[256];
char endTown[256];
int energyExpended;
}
allTowns[1024], reverseTowns[1024], readInTowns[8];
int openEnergyFile(void) {
FILE * energyFilePtr = fopen("energy.txt", "r");
int i = 0;
if (energyFilePtr == NULL) {
printf("the file doesn't exist");
return 0;
}
while (!feof(energyFilePtr)) {
fscanf(energyFilePtr, "%s %s %d\n", & allTowns[i].startTown, & allTowns[i].endTown, &
allTowns[i].energyExpended);
printf("Printing the file: %s %s %d\n", & allTowns[i].startTown, & allTowns[i].endTown,
allTowns[i].energyExpended);
i++;
allTowns[i - 1].startTown = allTowns[i].endTown;
}
printf("\n");
//printf("Printing town middle test: %s %s %d\n", &allTowns[19].startTown, &allTowns[12].endTown, allTowns[12].energyExpended); WORKING TEST
return 0;
}