hi, wonder if anyone can help me.
I have a text file with the following format:
town_name followed by 12 real numbers i.e
London 7.24 8.15 6.45 3.24 3.66 2.45 4.71 6.78 6.45 8.61 7.45 6.55
Manchester 12.23 10.67 7.56 4.34 5.55 6.29 8.77 14.77 9.77 7.49 8.34 7.66
Liverpool 10.12 6.76 7.87 6.66 4.56 7.82 12.63 9.62 6.02 7.94 8.34 9.18
Bristol 6.55 8.61 4.67 4.86 5.55 2.39 6.66 4.91 2.93 4.66 4.81 7.39
ZZZZZ
the ZZZZZ is a sentinel value which tells my program to stop reading from the file. I need to read the data file and display it in the console window. However I can only get it to work when I have only one number next to the town name, as soon as the others go in, i get strange results. the entire data set needs to be held in an array as I need to do calculations to the data, below is the code I have developed so far...
#include <stdio.h>
#include <string.h>
const int maxrows=12;
const int maxcols=12;
const int maxchar=10;
static char place[maxrows][maxchar];
static char dummy[maxchar];
float rain[maxrows];
float average, annual, wet, dry;
FILE *fp;
main ()
{
int i=0;
if ((fp = fopen("data1.txt", "r"))==NULL)
printf("Error opening file\n");
else
{
do
{
printf("\n");
fscanf(fp,"%s",&dummy);
if (strcmp(dummy, "ZZZZZ") !=0)
{
strcpy(place[i],dummy);
fscanf(fp,"%f",&rain[i]);
printf("%10s\t%2.2f", place[i],rain[i]);
i++;
}
} while ((strcmp(dummy,"ZZZZZ") !=0) and (i<maxrows));
}
fclose(fp);
getchar();
}
thanks in advance