Thread: Returning file descriptors

  1. #1
    Registered User
    Join Date
    Mar 2008
    Posts
    22

    Returning file descriptors

    I am having trouble returning a file descriptor to main and calling a 2nd function there passing the returned file descriptor as paramater.
    Code:
    FILE *file_des;
    FILE x; 
    
    FILE input(char* title){
      file_des = fopen(title, "r");
      return file_des;
    }
    
    
    void do_someting (FILE *x){
     int letter;
     while ((letter = fgetc(file_des) != EOF)
         printf ("%c",letter);
    }
      
    
    int main (int ac, char *av[]){
       x = input(av[1]);
      do_someting(x);
    }

  2. #2
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    1. Do not use global vars - declare them in the scope where you use them
    x in main
    file_des in input
    2. make them both FILE*
    3. make input return the correct type (FILE*)
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need Help Fixing My C Program. Deals with File I/O
    By Matus in forum C Programming
    Replies: 7
    Last Post: 04-29-2008, 07:51 PM
  2. gcc link external library
    By spank in forum C Programming
    Replies: 6
    Last Post: 08-08-2007, 03:44 PM
  3. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  4. Hmm....help me take a look at this: File Encryptor
    By heljy in forum C Programming
    Replies: 3
    Last Post: 03-23-2002, 10:57 AM
  5. Need a suggestion on a school project..
    By Screwz Luse in forum C Programming
    Replies: 5
    Last Post: 11-27-2001, 02:58 AM