Returning file descriptors

This is a discussion on Returning file descriptors within the C Programming forums, part of the General Programming Boards category; I am having trouble returning a file descriptor to main and calling a 2nd function there passing the returned file ...

  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
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,473
    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*)
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

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, 01: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, 09:57 AM
  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