this output
Andrew Tanenbaum, David Wetherall
Computer Networks
Michaell Donahoo, Kenneth Calvert
TCP/IP Sockets in C
William,Stallings
Yale Patt, Sanjay Patel
is the result of this code.
#include <stdio.h> /* for printf() and fprintf() */
#include <sys/socket.h> /* for socket() and bind() */
#include <arpa/inet.h> /* for sockaddr_in and inet_ntoa() */
#include <stdlib.h> /* for atoi() and exit() */
#include <string.h> /* for memset() */
#include <unistd.h> /* for close() */
#include "./book.h"
#ifndef fileget_C
#define fileget_C
#endif // fileget_C
void readlib(Book* Library){
/*char stock[4][125];*/
FILE *bookfile=fopen("/home/ninja/Sockets/bookstock.txt","r+");
size_t len=0;
int num;
ssize_t read;
char *stringin;
char *isbn;
int *numin;
int n;
for(n=0; n<4; n=n+1){
getline(&stringin, &len, bookfile);
strncpy(Library[n].isbn,stringin,strlen(stringin));
//printf("%s",Library[n].isbn);
stringin=NULL;
getline(&stringin, &len, bookfile);
strncpy(Library[n].Author,stringin,strlen(stringin));
//printf("%s",Library[n].Author);
stringin=NULL;
getline(&stringin, &len, bookfile);
strncpy(Library[n].title,stringin,strlen(stringin));
//printf("%s",Library[n].title);
stringin=NULL;
getline(&stringin, &len, bookfile);
num=atoi(stringin);
Library[n].edition=num;
//printf("%d\n",Library[n].edition);
stringin=NULL;
getline(&stringin, &len, bookfile);
Library[n].year=atoi(stringin);
stringin=NULL;
//printf("%d\n",Library[n].year);
getline(&stringin, &len, bookfile);
strncpy(Library[n].publisher,stringin,strlen(stringin));
stringin=NULL;
getline(&stringin, &len, bookfile);
Library[n].inventory=atoi(stringin);
stringin=NULL;
getline(&stringin, &len, bookfile);
Library[n].available=atoi(stringin);
//printf("%d\n",Library[n].available);
}
// printf("%s",Library[0].title);
//printf("%s",Library[1].title);
//printf("%s",Library[2].title);
//printf("%s\n",Library[3].title);
printf("%s",Library[0].Author);
printf("%s",Library[1].Author);
printf("%s",Library[2].Author);
printf("%s",Library[3].Author);
}
For some reason, I'm getting extra lines stored or I am not storing to the pointer in an appropriate way. The commented out print lines in the for loop display the right information which includes printing the appropriate author field of the struct.