Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Problem in search function to read data in file using fseek and fread

Status
Not open for further replies.

darr01

Programmer
Oct 24, 2001
28
0
0
MY
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

 
hi daryl,
ur fread for sizeof(item) is going to read 314 bytes straight from the curr. file pointer.. i don't see ur file data as normalized to that effect.. u would do better to read strings, build ur own parser on file description and copy in to ur item struct.

hth,
shail
 
Hi, shail. Thanks for the suggestion. Actually, I am quite new to C. How do I build a parser ur talking about?

Don't know if my above way is a good way of coding. Is there a better way of doing it?

Thanks again,

Darryl
 
Hi,

In your file, you don't want those labels like ISBN No or Book Name. You want fread to read the size of the structure. Those labels make it read less than it needs. If you compare what's in your text file: ISBN No: 1-11111-111-1 with what you type: 1-11111-111-1, you will never get a match.

Also:

if(strcmp(item.isbn, isbn) == 0)

Won't find anything in that structure. You need to use item.isbn_no as you defined it in the structure.

Hope that helps.

-Tyler
 
Hi, gonzilla. Thanks for your reply. Yes, I have realize that my problem lies in the fact that I have added formatting into my file. The file should only contain only my input.

Now it's working fine after I remove ISBN No, Book Name etc.

Thanks for all who reply. You guys have been very helpful.

Regards,

Darryl
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top