Thread: Help on simple file functions

  1. #16
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    fgets(&s, sizeof(contact_rec), stdin);
    it is bad
    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.
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  2. #17
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by enjoyincubus
    @quzah

    ive read what youve wrote

    if you mean the data that are being ask by the user , i already have them

    it works w/ view function
    Yeah, so? You're not even remotely paying attention to what your code does.
    Quote Originally Posted by enjoyincubus
    it will result into an "Error: Record not found"

    here is my code again:
    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.
    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... */
    }
    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!


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #18
    Registered User
    Join Date
    Oct 2006
    Posts
    21
    umm ok, so how do i do that?

  4. #19
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    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.

  5. #20
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by enjoyincubus
    umm ok, so how do i do that?
    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?

    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.
    Hope is the first step on the road to disappointment.

  6. #21
    Registered User
    Join Date
    Oct 2006
    Posts
    21
    I made this code


    ill try to do what you just said

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. opening empty file causes access violation
    By trevordunstan in forum C Programming
    Replies: 10
    Last Post: 10-21-2008, 11:19 PM
  2. Can we have vector of vector?
    By ketu1 in forum C++ Programming
    Replies: 24
    Last Post: 01-03-2008, 05:02 AM
  3. System
    By drdroid in forum C++ Programming
    Replies: 3
    Last Post: 06-28-2002, 10:12 PM
  4. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 04:10 PM