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
cprogramming 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:
| Habermann | Operating Systems | H01 | 10 | 5 |
| Golumbic | Graph Theory | G01 | 5 | 2 |
| Jacobs | Database Logic | J01 | 3 | 1 |
| Kanter | Management Information | K01 | 8 | 1 |
| Kuo | Numerical Methods | K02 | 2 | 0 |
| Hughs | Structured Programming | H02 | 4 | 4 |
| Schroder | C | S01 | 7 | 2 |
| Herzog | Computing Structures | H03 | 10 | 7 |
| Hunter | Understanding C | H04 | 6 | 6 |
| Holub | Compiler Design | H05 | 11 | 8 |
| Galvin | Operating Systems | G02 | 15 | 2 |
| Lane | Data Communications | L01 | 20 | 3 |
| Kleinrock | Queueing Systems | K03 | 14 | 0 |
| Kindred | Data Systems | K04 | 2 | 0 |
| Mano | Computer Architecture | M01 | 2 | 2 |
| Horowitz | Programming Languages | H06 | 16 | 10 |
| 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