change this program to open a file

This is a discussion on change this program to open a file within the C Programming forums, part of the General Programming Boards category; how would i change this function of my program to open a file named "numbers"? i know i would get ...

  1. #1
    Registered User
    Join Date
    Apr 2003
    Posts
    2

    change this program to open a file

    how would i change this function of my program to open a file named "numbers"? i know i would get rid of the srand and the part dealing w/ rand. also, do i declare FILE *fptemp in this function or in main?
    Code:
    /*	=============== fillArray ===================
    	This function fills an array of a given length with
    	random numbers in a given range.
    	   Pre   ary is an ary of integers
    	         size is the number of elements
    	         lowLimit is the minimum value to use
    	         highLimit is the maximum value to use
    	   Post  array has been filled
    */
    void fillArray (int ary[],    int size,
                    int lowLimit, int highLimit)
    {
    /*	Local Definitions */
    	int i;
    	int num;
    
    /*	Statements */
    	srand (time (NULL));
    
    	for (i = 0; i < size; i++)
    	   {
    	    num = rand() % ((highLimit + 1) 
    	         lowLimit) + lowLimit;
    	    if (num % 7 == 0  ||  num % 3 == 0)
    	       ary[i] = -num;
    	    else
    	       ary[i] = num;
    	   } /* for */
    
    	return;
    }	/* fillArray */
    thanks
    Last edited by A F Army; 04-05-2003 at 04:14 PM.

  2. #2
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,671
    Check out the FAQ.

    And use code tags.

    gg

  3. #3
    Registered User
    Join Date
    Apr 2003
    Posts
    2
    hows that?

    the only thing i'm not sure of is how to pull the numbers from the file called numbers and put them into the array

  4. #4
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,671
    Click on the "FAQ" in my last post - it will show you have to open a file. You can then use fscanf() using the open file handle.

    gg

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. sequential file program
    By needhelpbad in forum C Programming
    Replies: 80
    Last Post: 06-08-2008, 01:04 PM
  3. Inventory records
    By jsbeckton in forum C Programming
    Replies: 23
    Last Post: 06-28-2007, 04:14 AM
  4. Basic text file encoder
    By Abda92 in forum C Programming
    Replies: 15
    Last Post: 05-22-2007, 01:19 PM
  5. Need a suggestion on a school project..
    By Screwz Luse in forum C Programming
    Replies: 5
    Last Post: 11-27-2001, 01:58 AM

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