it is badQuote:
fgets(&s, sizeof(contact_rec), stdin);
s is 1 byte
you give pointer to it and tell fgets that you have place for sizeof(contact_rec) bytes... Not good... Not good at all... Neveer lie about memory size you own. ;)
Printable View
it is badQuote:
fgets(&s, sizeof(contact_rec), stdin);
s is 1 byte
you give pointer to it and tell fgets that you have place for sizeof(contact_rec) bytes... Not good... Not good at all... Neveer lie about memory size you own. ;)
Yeah, so? You're not even remotely paying attention to what your code does.Quote:
Originally Posted by enjoyincubus
Of course it does. It'd be ........ing miracle if it actually worked. I don't even want to attempt to calculate the odds on that working right. I'll comment your code, since you apparently have no idea what you're doing.Quote:
Originally Posted by enjoyincubus
Like I said, you never actually read. Well that's actually really really apparent. You never actually read what I write, and you never actually read anything from your file here, just like I keep saying!Code:void search (void) /* first, you're not passing any arguments... */
{
FILE *fp;
contact_rec X[4]; /* declare a local array, which holds nothing right now... */
char s;
int i = 0;
if ((fp = fopen ("phone.dat", "rb")) == NULL) /* open the file for reading... */
{
printf ("Error : file is does not exist");
exit(1);
}
printf ("Enter a name to be search: ");
fgets(&s, sizeof(contact_rec), stdin); /* get something to search for... */
for ( i = 0; i < 5; i++)
{
/* look through the array, which contains nothing useful... */
if ((strcmp (&s, X[i].name)) == 0)
{
printf("%s", X[i].tel);
printf("%s", X[i].cel);
}
else
{
/* somehow be mystified as to why it doesn't work... */
printf("Error : Record not found");
fclose(fp);
}
}
/* return without closing the file, since you only close it on error... */
}
Quzah.
umm ok, so how do i do that?
A good lesson ought to help. http://c-faq.com/~scs/cclass/int/sx3.html
You're looking for information concerning reading binary files, but I would read the whole lesson at some point. Now's good.
What do you mean "how do I do that"? Did you write any of this code, or did you just steal it from someone else who doesn't have a clue?Quote:
Originally Posted by enjoyincubus
You do it the same way you did it in your view function. You really should be thinking this through better. Make a function to read the info into memory. Make your other functions take that information as an argument to the function, so you can edit it, view it, search it, etc.
Quzah.
I made this code
ill try to do what you just said