Thread: Passing array member as a parameter

  1. #1
    High Flyer stumpster123's Avatar
    Join Date
    Mar 2005
    Posts
    26

    Passing array member as a parameter

    In my main function I have an array of File pointers like this:

    Code:
    FILE *fInfile[MAXFILE]
    and my functions header is

    Code:
    int read_info(FILE *fA, FILE *fI);
    {
    .
    .
    .
      if((fI = fopen(szFilename, "w")) == NULL)
        {
          fprintf(stderr, "Could not open the input file %s.\n", szFilename);
          exit(1);
        }
    .
    .
    }
    I call the function with the following

    Code:
    iSize[iTemp] = read_info(fArchive, fInfile[iTemp]);
    where iTemp is the current file i want to write open

    However when i go to use the file pointers they are all null. I have gotten the program to work correctly but not in the way i want too by sending iTemp as a parameter and the entire array instead.

    I tried using double asterisks to send the dereference the pointer but I could not get it to work correctly could someone please help me understand what is wrong with this code.

    Thanks.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Have you actually opened every file with fopen? You know, something like:
    Code:
    char buf[BUFSIZ] = {0};
    
    for ( i = 0; i < MAXFILE; x++ )
    {
        sprintf( buf, "filename%03d.dat", i );
        fInfile[ i ] = fopen( filename, "r" );
        if( fInfile[ i ] == NULL )
        {
            ...fopen failed...
        }
    }
    Quzah.
    Last edited by quzah; 03-29-2005 at 01:08 AM.
    Hope is the first step on the road to disappointment.

  3. #3
    High Flyer stumpster123's Avatar
    Join Date
    Mar 2005
    Posts
    26
    Yeah that is what read info is doing before it reads anything. It opens the file and uses the file pointer that is sent from main function. I am using an array so the function call is in a for loop that will open all the files. But when i use the function the array in the main function remains all NULL instead of pointing to the files that were opened in the read function.

  4. #4
    Registered User
    Join Date
    Mar 2004
    Posts
    536
    Quote Originally Posted by stumpster123
    It opens the file and uses the file pointer that is sent from main function.
    ...
    But when i use the function the array in the main function remains all NULL instead of pointing to the files that were opened in the read function.

    You could try this:


    Change read_info() to this
    Code:
    int read_info(FILE *fA, FILE **fI);
    Call it like this:

    Code:
    iSize[iTemp] = read_info(fArchive, &fInfile[iTemp]);
    And inside read_info, open the file like this:

    Code:
      if((*fI = fopen(szFilename, "w")) == NULL)
    Now, the value gets passed back to the calling program.

    Regards,

    Dave

    ***Note*** My original post had a "typographical" error, which has been corrected here.
    Last edited by Dave Evans; 03-29-2005 at 10:49 AM. Reason: Correction to calling statement

  5. #5
    High Flyer stumpster123's Avatar
    Join Date
    Mar 2005
    Posts
    26
    Thanks dave that worked perfectly.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 07-11-2008, 07:39 AM
  2. Replies: 2
    Last Post: 04-22-2008, 12:07 PM
  3. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 10:03 AM
  4. Class Template Trouble
    By pliang in forum C++ Programming
    Replies: 4
    Last Post: 04-21-2005, 04:15 AM
  5. typecasting data for parameter passing
    By daluu in forum C++ Programming
    Replies: 8
    Last Post: 12-08-2004, 02:58 PM