I'm trying to make a program for my 101 class that reads from a file using functions for each struct type and prints the file out to the screen. Unfortunately, I don't know how to make it continuously read from the file when I'm using fscanf in different functions. Sorry I know, not a very good explanation of my problem, but hopefully you'll understand from my code.
Code:
#include <stdio.h>
typedef struct {
char make[15];
char model[15];
int odometer;
} autoT;
typedef struct {
int mMonth;
int mDay;
int mYear;
int pMonth;
int pDay;
int pYear;
} dateT;
typedef struct {
double capacity;
double fuelLevel;
} tankT;
dateT scanDate()
{
FILE *input;
input = fopen( "infile.txt", "r" );
dateT date;
fscanf( input, " %d", &date.mMonth );
fscanf( input, " %d", &date.mDay );
fscanf( input, " %d", &date.mYear );
fscanf( input, " %d", &date.pMonth );
fscanf( input, " %d", &date.pDay );
fscanf( input, " %d", &date.pYear );
fclose( input );
/*date = {date.mMonth,date.mDay,date.mYear,date.pMonth,date.pDay,date.pYear};
*/
return date;
} /*end scanDate()*/
tankT scanTank()
{
FILE *input;
input = fopen( "infile.txt", "r" );
tankT tank;
fscanf( input, " %lf", &tank.capacity );
fscanf( input, " %lf", &tank.fuelLevel );
fclose( input );
return tank;
} /*end scanTank()*/
autoT scanAuto()
{
FILE *input;
input = fopen( "infile.txt", "r" );
autoT car;
fscanf( input, " %s", car.make );
fscanf( input, " %s", car.model );
fscanf( input, " %d", &car.odometer );
fclose( input );
return car;
} /*end scanAuto()*/
void printDate( dateT d )
{
printf( "This automobile was manufactured on %d-%d-%d and purchased on %d-"
"%d-%d.\n", d.mMonth,d.mDay,d.mYear,d.pMonth,d.pDay,d.pYear );
} /*end printDate()*/
void printTank( tankT t )
{
printf( "The gas tank has a capacity of %.2f gallons and currently holds "
"%.2f gallons.\n", t.capacity,t.fuelLevel );
} /*end printTank()*/
void printAuto( autoT a )
{
printf( "The automobile in question is a %s %s and it has %d miles on the "
"odometer.\n", a.make,a.model,a.odometer );
} /*end printAuto()*/
int main( void )
{
FILE *input;
input = fopen( "infile.txt", "r" );
if( input == NULL ) {
printf( "File could not be opened. Check file name." );
return 1;
}
else {
while( 1 ) {
if( feof( input ) ) {
break;
}
printAuto( scanAuto() );
printDate( scanDate() );
printTank( scanTank() );
}
}
fclose( input );
return 0;
}
I'm also getting ISO C90 warnings saying
Code:
lab10.c: In function âscanDateâ:
lab10.c:36: warning: ISO C90 forbids mixed declarations and code
lab10.c: In function âscanTankâ:
lab10.c:58: warning: ISO C90 forbids mixed declarations and code
lab10.c: In function âscanAutoâ:
lab10.c:73: warning: ISO C90 forbids mixed declarations and code
any help is greatly appreciated, thanks in advance.