Thread: fscanf in different functions for the same file

  1. #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.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    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.

  3. #3
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    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.

  4. #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.

  5. #5
    Registered User slingerland3g's Avatar
    Join Date
    Jan 2008
    Location
    Seattle
    Posts
    603
    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.

  6. #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" );
        }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A development process
    By Noir in forum C Programming
    Replies: 37
    Last Post: 07-10-2011, 10:39 PM
  2. Data Structure Eror
    By prominababy in forum C Programming
    Replies: 3
    Last Post: 01-06-2009, 09:35 AM
  3. Problems with file pointer in functions
    By willie in forum C Programming
    Replies: 6
    Last Post: 11-23-2008, 01:54 PM
  4. Inventory records
    By jsbeckton in forum C Programming
    Replies: 23
    Last Post: 06-28-2007, 04:14 AM
  5. C++ std routines
    By siavoshkc in forum C++ Programming
    Replies: 33
    Last Post: 07-28-2006, 12:13 AM