Image

Imagedelusory wrote in Imagecprogramming dumb

Arrays of Structures

It's been 8 months since I've last touched C and need a crash course, particularly with structures. So where do I turn to for assistance? The ever helpful Imagecprogramming community! You guys have helped me before so I hope you can do it again :)

For my homework assignment, I am to create a library database. The information we have is the book's author, title, and code (consisting of a letter and a 2-digit number, such as H01), as well as the number of copies of the book the library owns and how many are currently on hand & available to be borrowed by the library's patrons. The first task of this assignment is to write a function called readlibrary that will read the data the professor's provided into an array of structures.

Now, for some reason, I cannot figure out how to do this o_O Here's what I have:

#include <stdio.h>
#include <string.h>

#define N 25
#define LFILE "C:\\CIS\22\asn1\library.txt"

FILE *lf;
FILE *tf;

struct library {
     char author[40];
     char title[40];
     char code[4];
     int hold;
     int loan;
} book[N];

void readlibrary( struct library book, int nelts) {
     int i;
     char *eofile;
     fscanf( lf, "%s", &eofile ); /* checking for EOF */
     while( strcmp( eofile, "end" ) != 0) /* I've forgotten how to work with strings, too... I'm horrible :( */
          for( i = 0; i < nelts; i++ )
               fscanf( lf, "%s %s %s %d %d", &book[i].author, &book[i].title, &book[i].code, &book[i].hold, &book[i].loan ); /* I get errors here: subscripted value is neither array nor pointer */
     return;
}

int main( ) {
     lf = fopen( LFILE, "r" );

     readlibrary( *book, N ); /* I don't know if I'm calling the function correctly o_O */

     fclose( lf );

     return;
}

Here is the library.txt file:

HabermannOperating SystemsH01105
GolumbicGraph TheoryG0152
JacobsDatabase LogicJ0131
KanterManagement InformationK0181
KuoNumerical MethodsK0220
HughsStructured ProgrammingH0244
SchroderCS0172
HerzogComputing StructuresH03107
HunterUnderstanding CH0466
HolubCompiler DesignH05118
GalvinOperating SystemsG02152
LaneData CommunicationsL01203
KleinrockQueueing SystemsK03140
KindredData SystemsK0420
ManoComputer ArchitectureM0122
HorowitzProgramming LanguagesH061610
end


Any and all help will be appreciated :D (Also. Please be so kind as to not make fun of my lack of C skills. Heh.) Thank you.


UPDATED:

#include <stdio.h>

#define N 25

FILE *lib;
FILE *tra;

typedef struct {
     char author[40];
     char title[40];
     char code[4];
     int hold;
     int loan;
} LIBRARY;

LIBRARY book[N];

void readlibrary( LIBRARY *b ) {
     int i;

     for( i = 0; i < N; i++ )
          fscanf( lib, "%s %s %s %d %d", &b[i].author, &b[i].title, &b[i].code, &b[i].hold, &b[i].loan );

     return;
}

void printfull( LIBRARY *b ) {
     int i;

     printf( "Author\tTitle\tCode\t# Copies\t# Borrowed\t# Available\n" );
     for( i = 0; i < N; i++ )
          printf( "%s\t%s\t%s\t%d\t%d\t%d\n", b[i].author, b[i].title, b[i].code, b[i].hold, b[i].loan, (b[i].hold - b[i].loan));

     return;
}

int main( ) {
     lib = fopen( "library.txt", "r" );

     readlibrary( book );
     printfull( book );

     fclose( lib );
     return 0;
}


This seems to work. YAY! :D