Thread: passing a file pointer to a function

  1. #1
    Registered User
    Join Date
    Aug 2012
    Posts
    2

    Question passing a file pointer to a function

    I did a proyect which writes and reads to a binary file, but in each function I opened and closed the file, so I tryed to pass the file pointer to the functions so i would only have to open it once, however the program compiles but doesnt run, it says "the program stopped working", here's a part of the code:

    Im new to programming, it would be pretty cool if someone showed me what the problem is...

    PD: the double pointer is an random idea to try to fix the problem, it helped to make it compile but doesnt work...

    thanks

    insert
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    
    #define ARCHIVO "data.bin"
    #define SIZE sizeof(person)
    
    
    typedef struct
     {int i_id;
      char name[20];
      int active;} person;
      
    void v_menu(FILE *f);  
    void v_list(FILE **f, person someone);
      
    int main()
    {
      FILE *f;
      fopen(ARCHIVO,"wb+");
      v_menu(f);
      return 0;
    }
    
    
    void v_menu(FILE *f)
    {
      int i_option;
      person someone;
      do{
          system("cls");
          printf("\n\n\t\t1. Add.\n\t\t2. Modify.\n\t\t3. delete.\n\t\t4. Consult.\n\t\t5. List.\n\t\t6. Fin.\n\n\t\t\t\t");
          scanf("%d",&i_option);
          system("cls");
          switch(i_option)
            {
    //         case 1:{v_agregarRegistro(&f, registro);break;}
    //         case 2:{v_modificarRegistro(&f, registro);break;}
    //         case 3:{v_eliminarRegistro(&f, registro);break;}
    //         case 4:{v_consultarRegistro(&f, registro);break;}
             case 5:{v_list(&f, someone);break;}
            }
        }
      while(i_option!=6);
    }
    
    
    void v_list(FILE **f, person someone)
    {
      fseek(*f,0,SEEK_SET);
      while(fread(&someone,SIZE,1,*f))
        {
          if(someone.active==1)
          printf("\n\n\t\tID: %d\n\t\tName: %s",someone.i_id,someone.name);
        }
      getch();
    }

  2. #2
    Registered User
    Join Date
    May 2012
    Location
    Arizona, USA
    Posts
    945
    Where do you save the return value from fopen to f?

    The double pointer isn't needed in this program, but it might be if you wanted to change the value of f within a function and make the change visible in its caller.

  3. #3
    Registered User
    Join Date
    Aug 2012
    Posts
    2
    Quote Originally Posted by christop View Post
    Where do you save the return value from fopen to f?

    The double pointer isn't needed in this program, but it might be if you wanted to change the value of f within a function and make the change visible in its caller.

    I must of errased it while i was shortening the code, so.. it should work if I use just normal pointers, this time it does, but in the full version it doesnt, im guessing its because in the full version i open the file in rb+ mode so i can read and write, is there a difference?

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by estefany
    in the full version it doesnt, im guessing its because in the full version i open the file in rb+ mode so i can read and write, is there a difference?
    No, the open mode does not make a difference when deciding between a pointer and a pointer to a pointer. What makes a difference is whether you are going to change f and want the result to be reflected in the caller. If you do, then you would use a pointer to a pointer and hence change *f instead.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    Yes you can
    Here is an example

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    
    void print_file(FILE *);
    
    
    int main(void)
    {
        /* Lots of interesting code here... */
    
    
        FILE *fp = fopen ( "C:\\ABCDEFGHI.txt", "r" );
        if (!fp)
        {
            perror ( "Unable to open file" );
            exit ( EXIT_FAILURE );
        }
    
    
        print_file (fp);
    
    
        return 0;
    }
    
    
    void print_file(FILE *in)
    {
        char ch=0;
    
    
        while(!feof(in))
        {
            ch = (char) getc(in);
            if (ferror(in))
            {
                fputs ("File error - Reading Operation",stderr);
                exit (EXIT_FAILURE);
            }
    
    
            if (!feof(in))
            {
                putchar(ch);
            }
        }
        return;
    }
    Edit - I think that your problem was that you were entering &f into your function - f was already a pointer.
    Last edited by Click_here; 08-02-2012 at 08:14 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. passing a function pointer
    By Akkernight in forum C++ Programming
    Replies: 17
    Last Post: 03-22-2009, 04:41 AM
  2. passing method pointer as a function pointer
    By sevcsik in forum C++ Programming
    Replies: 5
    Last Post: 12-30-2007, 06:19 AM
  3. Replies: 4
    Last Post: 11-05-2006, 02:57 PM
  4. passing pointer to a function
    By Apropos in forum C++ Programming
    Replies: 14
    Last Post: 02-26-2005, 05:36 PM
  5. Passing Pointer to a function
    By Nishant Ghai in forum C Programming
    Replies: 4
    Last Post: 02-27-2003, 06:40 AM

Tags for this Thread