C Board  

Go Back   C Board > General Programming Boards > C Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 12-03-2008, 08:25 PM   #1
Registered User
 
Join Date: Dec 2008
Posts: 8
fscanf in different functions for the same file

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.
bchan90 is offline   Reply With Quote
Old 12-03-2008, 08:28 PM   #2
and the Hat of Guessing
 
tabstop's Avatar
 
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   Reply With Quote
Old 12-03-2008, 08:37 PM   #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;
      }
may not look the same, it is functionally identical to:
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   Reply With Quote
Old 12-03-2008, 08:47 PM   #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   Reply With Quote
Old 12-03-2008, 08:51 PM   #5
Registered User
 
slingerland3g's Avatar
 
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   Reply With Quote
Old 12-03-2008, 09:31 PM   #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   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

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


All times are GMT -6. The time now is 11:03 PM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22