function problem - freezing

This is a discussion on function problem - freezing within the C Programming forums, part of the General Programming Boards category; Hello people, I'm getting some problems with the following function: Code: extern struct product products[1000]; int loadDB (struct product products[], ...

  1. #1
    Registered User Vber's Avatar
    Join Date
    Nov 2002
    Posts
    807

    function problem - freezing

    Hello people, I'm getting some problems with the following function:
    Code:
    extern struct product products[1000];
    
    int loadDB (struct product products[], char *filepath)
    {
    	FILE *fpData; /* pointer to the file that we'll get the data */
    	int i = 0; /* the index for filling the structure array */
    
    	/* try to open the file and check for errors */
    	if ((fpData = fopen(filepath,"r")) == NULL)
    		return (EXIT_FAILURE);
    #ifdef DEB
    	printf("hhehe");
    #endif
    
    		while (fscanf(fpData,"%d%d%d%f%s%s%s%s",&products[i].pr_code,&products[i].pr_inv,&products[i].pr_min_inv, &products[i].pr_price, products[i].pr_name,products[i].pr_supplier,products[i].pr_sup_tel,products[i].pr_sup_fax) == 8)
    			i++;
    	
    
    	/* check if we at least got a single value, if not, return fail (0) */
    	if (products[0].pr_code == 0)
    		return 0;
    
    	return 1;
    }
    The first extern is to get a global array of structures declared in main.c, this file is market.c.

    My program freezes when I call this function, I can see the HEEE msg, but afters this, all goe's down.
    Last edited by Vber; 03-21-2003 at 04:50 PM.

  2. #2
    Registered User Vber's Avatar
    Join Date
    Nov 2002
    Posts
    807
    Now, he don't freezes but he returns EXIT_FAILURE, and says file could not be opened, but the file exists, for sure.

  3. #3
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    Post the call of that function.
    Are you sure the file lies in the proper folder, since that code seems alright.

    Now, he don't freezes
    It's a male function?
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  4. #4
    Registered User Vber's Avatar
    Join Date
    Nov 2002
    Posts
    807
    I call the function like this:
    Code:
    if (loadDB(products,"c:\\db.txt") == EXIT_FAILURE)
    {
       //
    }
    the file db.txt exists, sure.

    >>It's a male function?

  5. #5
    Registered User Vber's Avatar
    Join Date
    Nov 2002
    Posts
    807
    Ok, I fixed the problem, thanks everyone, I just changed from return EXIT_FAILURE to return 0

    and changed the calling function to:
    Code:
    if (!loadDB(products,"c:\\db.txt"))

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 03:06 PM
  2. doubt in c parser coding
    By akshara.sinha in forum C Programming
    Replies: 4
    Last Post: 12-23-2007, 12:49 PM
  3. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 10:03 AM
  4. Problem with function pointers
    By vNvNation in forum C++ Programming
    Replies: 4
    Last Post: 06-13-2004, 06:49 AM
  5. structure vs class
    By sana in forum C++ Programming
    Replies: 13
    Last Post: 12-02-2002, 06:18 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21