Reputation: 7
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.
Upvotes: 0
Views: 76
Reputation: 30813
The problem is with your readlib()
and your Library
.
Note that the return
of the readlib()
is not of type Book*
, while you return Library
which is of type Book[]
, thus the compiler gives you the warning.
Also, it seems like you are trying to return
an array
in C. Be very careful. Technically, you could change the return
of readlib()
like this:
Book* readlib()
But it is highly not recommended. It is better to have array declared outside of the function and the function contains the Book*
argument in it:
void readlib(Book* Library, int noOfBook)
Then you declare your
Book Library[4]; //somewhere else
and call the readlib
like this:
readlib(Library, 4);
You do not need to to:
return Library;
In the readlib
, since it is already declared outside and passed to the readlib
Of course, if you really want to return
Book*
however, you could prepare proper memory space by using malloc
in the function:
Book* readlib(int noOfBook){
Book *Library = malloc (noOfBook * sizeof(Book));
//something else
return Library;
}
But I personally prefer to handle all these outside.
Upvotes: 2