![]() |
| | #1 |
| Registered User Join Date: Dec 2008
Posts: 8
| fscanf in different functions for the same file 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;
}
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 |
| bchan90 is offline | |
| | #2 |
| and the Hat of Guessing Join Date: Nov 2007
Posts: 8,845
| Open the file in main, and pass the FILE pointer around to each function. And the warning means just what it says: the official 1990 standard for the C language forbids variable declarations mixed in with code. |
| tabstop is offline | |
| | #3 |
| Kernel hacker Join Date: Jul 2007 Location: Farncombe, Surrey, England
Posts: 15,686
| If production date and purchase date are part of the car itself, perhaps you want to put those into the autoT type. I would also use a struct with year, month, day in it, and not have the two dates in one structure - that way, you could have ONE function that reads a date, and call it twice, instead of repeating the same bit of code twice in the same function. Also read: http://faq.cprogramming.com/cgi-bin/...&id=1043284351 whilst: Code: while(1) {
if( feof( input ) ) {
break;
}
Code: while(feof(input)) {
...
Mats
__________________ Compilers can produce warnings - make the compiler programmers happy: Use them! Please don't PM me for help - and no, I don't do help over instant messengers. |
| matsp is offline | |
| | #4 |
| Registered User Join Date: Dec 2008
Posts: 8
| awesome! i didn't know (didn't even try for some reason) that you could pass the FILE pointer. Thanks a bunch! Program works like a charm now, except for some minor cleaning up to do. |
| bchan90 is offline | |
| | #5 |
| Registered User Join Date: Jan 2008 Location: Seattle
Posts: 575
| If you are familiar with how the "infile.txt" is structured there is no need to then create a FILE handle within each of your function calls. Just pass a reference to the input FILE handle variable to each of your function calls. You will need to redefine your prototypes for the functions though. This way you are not needing to start reading from the file from the beginning each time. |
| slingerland3g is offline | |
| | #6 |
| Registered User Join Date: Dec 2008
Posts: 8
| i actually had to change that part of my code cause it wasn't looping correctly...i had to run the scan functions before checking for EOF so my while loop ends up looking like this Code: while( 1 ) {
car = scanAuto( input );
date = scanDate( input );
tank = scanTank( input );
if( feof( input ) ) {
break;
}
printAuto( car );
printDate( date );
printTank( tank );
printf( "\n" );
}
|
| bchan90 is offline | |
![]() |
| Thread Tools | |
| Display Modes | |
|
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| A development process | Noir | C Programming | 32 | 12-19-2009 10:15 AM |
| Data Structure Eror | prominababy | C Programming | 3 | 01-06-2009 09:35 AM |
| Problems with file pointer in functions | willie | C Programming | 6 | 11-23-2008 01:54 PM |
| Inventory records | jsbeckton | C Programming | 23 | 06-28-2007 04:14 AM |
| C++ std routines | siavoshkc | C++ Programming | 33 | 07-28-2006 12:13 AM |