Help with File pointers in C

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • whiteCoding
    New Member
    • Nov 2005
    • 1

    #1

    Help with File pointers in C

    Say I have the following code:

    Code:
     #include<stdio.h> 
    #include<stdlib.h>
     
    void fileOpener(FILE*);
     
    int main(void)
    {
    FILE *fpFile;
    char test[14];
     
    fileOpener(fpFile);
     
    fgets(test, sizeof(test), fpFile);
     
    return(0);
    }
     
     
    void fileOpener(FILE *fpntr)
    {
    fpntr = fopen("inst.dat", "r");
    }
    What I'm trying to do is be able to open the stream in fileOpener and then be able to make manipulations back up in main or pass the address of the pointer on to subsequent functions. When I try to compile the code I get a Segmentation Fault. Could someone help me out??? I'd greatly appreciate it.
    Last edited by Niheel; Nov 25 '05, 07:42 AM.
  • Niheel
    Recognized Expert Moderator Top Contributor
    • Jul 2005
    • 2433

    #2
    Anyone with C experience that can help whiteCoding out?
    niheel @ bytes

    Comment

    • yeo
      New Member
      • Mar 2007
      • 1

      #3
      try passing by value instead of address, since the pointer itself already represents a memory location, passing the "address of an address" does not make sense. So it would be:

      Code:
      int fileOpener(FILE*);
       
      int main(void)
      {
      FILE *fpFile;
      char test[14];
       
      fpFile = fileOpener(fpFile);
       
      fgets(test, sizeof(test), fpFile);
       
      return(0);
      }
       
       
      int fileOpener(FILE *fpntr)
      {
      fpntr = fopen("inst.dat", "r");
      
      return fptr;
      }

      Comment

      Working...