Hi
I am having problems with the following.
I am trying to program a mileage chart that reads the names of places and miles from a text file
Once read in will look similar to below.
Hull Wall Stow Ford
Hull 0 21 3 45
Wall 21 0 5 65
Stow 3 87 0 12
Ford 45 65 12 0
I can do this part and have got the chart displaying on screen by using the following code.
Code:
void showchart ()
{
int x, row, col;
FILE *in_file;
in_file = fopen("places.txt","r");
for (x=0;x<=9; x++)
{
fscanf(in_file, "%4c", names[x]);
}
printf("\n ");
for (x=0; x<=9; x++)
{
printf("%6s",names[x]);
}
printf("\n");
for (row=0; row<=9; row++)
{
printf("%s", names[row]);
for(col=0; col<=9; col++)
{
fscanf(in_file, "%d", &grid[row][col]);
printf("%6d", grid[row][col]);
}
printf("\n");
}
}
Now I need to capture the names as a string and also the mile for each name, so I can sort the place names in alphabetical order and it will still display the correct mile in the grid when sorted.
I have an idea of using the gets command to store the name and the miles in a temp array when it is read from the text file, but I am unsure how to do this.
This is the code I have got so far and it doesnt work
Code:
#include <stdio.h>
#include <string.h>
char names[10][5];
void read_strings(char s[][5],int n)
int main(void)
{
int x, i, s;
FILE *in_file;
in_file = fopen("places.txt","r");
for (x=0;x<=9; x++)
{
fscanf(in_file, "%4c", names[x]);
}
for (x=0; x<=9; x++)
{
printf("%6s",names[x]);
for (i=0; i<n; i++)
{
gets(char s[][i]);
}
}
printf("\n");
}
}
It prints the names on the screen (well it did before I entered the gets(char s[][i]); line) in a list but not the miles as I have not got round to this part yet.
I am first trying just to get it to store the names so I can sort them, then I will try and add the code to store the miles with the names.
I hope this makes sence?
Thanks for any help.
Or can any one suggest an easier way to do this?