Hi, guys. Got some problem coding fread and fseek.
I have a structure as below:
struct bookData {
char isbn_no[14];
char BookName[100];
char Author[100];
char Publisher[100];
} item;
FILE *bookPtr; //Pointer to the FILE
I have managed to create a file (bookdata.txt) to enter data into. The format, if you open the text file is as below.
ISBN No: 1-11111-111-1
Book Name: Shawshank Redemption
Author: Stephen King
Publisher: Random House Inc.
My problem is that the search function below couldn't execute properly.
void searchISBN(void)
{
char isbn[14];
printf("\n\nSearching by ISBN No."
if ((bookPtr = fopen("bookdata.txt", "r+t") !=NULL)
{
printf("\n----------------------------"
printf("\nEnter ISBN No: "
fflush(stdin);
gets(isbn);
if(fseek(bookPtr, 0, SEEK_SET) == 0) //File is found
{
fread(&item, 1, sizeof(item), bookPtr);
while(!feof(bookPtr))
{
if(strcmp(item.isbn, isbn) == 0)
{
fprintf(stdout, "\nISBN No: %s", item.isbn); fprintf(stdout, "\nBook Title: %s", item.bookTitle);
fprintf(stdout, "\nAuthor: %s", item.author);
fprintf(stdout, "\nPublisher: %s", item.publisher);
}
fread(&item, 1, sizeof(item), bookPtr);
}
}
}
}
No, error was returned. This function is called from a switch statement. It just goes back to asking me to enter the ISBN No again. I suspect that my fread or fseek statement somehow has error/s but I couldn't find out what.
Thanks in advance,
Darryl
I have a structure as below:
struct bookData {
char isbn_no[14];
char BookName[100];
char Author[100];
char Publisher[100];
} item;
FILE *bookPtr; //Pointer to the FILE
I have managed to create a file (bookdata.txt) to enter data into. The format, if you open the text file is as below.
ISBN No: 1-11111-111-1
Book Name: Shawshank Redemption
Author: Stephen King
Publisher: Random House Inc.
My problem is that the search function below couldn't execute properly.
void searchISBN(void)
{
char isbn[14];
printf("\n\nSearching by ISBN No."
if ((bookPtr = fopen("bookdata.txt", "r+t") !=NULL)
{
printf("\n----------------------------"
printf("\nEnter ISBN No: "
fflush(stdin);
gets(isbn);
if(fseek(bookPtr, 0, SEEK_SET) == 0) //File is found
{
fread(&item, 1, sizeof(item), bookPtr);
while(!feof(bookPtr))
{
if(strcmp(item.isbn, isbn) == 0)
{
fprintf(stdout, "\nISBN No: %s", item.isbn); fprintf(stdout, "\nBook Title: %s", item.bookTitle);
fprintf(stdout, "\nAuthor: %s", item.author);
fprintf(stdout, "\nPublisher: %s", item.publisher);
}
fread(&item, 1, sizeof(item), bookPtr);
}
}
}
}
No, error was returned. This function is called from a switch statement. It just goes back to asking me to enter the ISBN No again. I suspect that my fread or fseek statement somehow has error/s but I couldn't find out what.
Thanks in advance,
Darryl