hi
ive written a program which so far reads in data from a file.
Code:
#include <stdio.h>
#include <string.h>
// the number of ints for the fishers structure
#define MAX 4
struct fisher
{
char name[80];
char address[90];
char postcode[14];
/*
* the int will hold, firstly a competetitor number, then a number for each
* competition weight in ounces
*/
int compNumber [MAX];
};
FILE *fisher_get(FILE *file, struct fisher *fisher)
{
char line[80], *newline;
int i, stones, pounds, ounces;
/*
* Get the name.
*/
if ( !fgets(fisher->name, sizeof fisher->name, file) )
{
return NULL;
}
newline = strchr(fisher->name, '\n');
if ( newline )
{
*newline = '\0';
}
printf("fisher->name = \"%s\"\n", fisher->name);
/*
* Get the address and post code.
*/
if ( fscanf(file, "%89[^.]. %13[^\n]%*c", fisher->address, fisher->postcode) != 2 )
{
return NULL;
}
printf("fisher->address = \"%s\"\n", fisher->address);
printf("fisher->postcode = \"%s\"\n", fisher->postcode);
/*
* Read and discard next line (or do whatever with it).
*/
fgets(line, sizeof line, file);
/*
* Read the stones, pounds, and ounces for each entry.
*/
for (i = 0; i < 3; ++i)
{
printf("i = %d:\n", i);
if ( fscanf(file, "%d %d %d%*c", &stones, £s, &ounces) != 3 )
{
return NULL;
}
printf(" stones = %d\n", stones);
printf(" pounds = %d\n", pounds);
printf(" ounces = %d\n", ounces);
}
return file;
}
int main()
{
FILE *file = fopen("autumn.txt", "r");
if ( file != NULL )
{
int i;
char line[80];
struct fisher fisher;
/*
* Read and display first two lines.
*/
for ( i = 0; i < 2; ++i )
{
fgets(line, sizeof line, file);
fputs(line, stdout);
}
while ( fisher_get(file, &fisher) )
{
}
fclose(file);
}
return 0;
im just stuck on the best way to select which file the user wants to read, as i have more than one, one called autumn.txt and one called spring.txt.
any help very much welcomed