Thread: calling a fstream in a function

  1. #1
    Registered User
    Join Date
    Apr 2006
    Posts
    27

    calling a fstream in a function

    so .. in c if i had a file pointer

    e.g:

    Code:
    void function(FILE *inputfile){
    int c;
    
    fscanf(inputfile,"%d",c);
    
    }
    
    int main(){
    
    FILE *inputfile=fopen("myfile.txt","r");
    
    function(inputfile);
    
    fclose(inputfile);
    
    return 0;
    }
    How woud i do the same in c++??
    I couldn't find it anywhere in google or in my books.

    My guess is ...

    Code:
    void function ( ???? ){
    int c;
    
    ??? >> c ;
    }
    
    int main(){
    
    ifstream inputfile("myfile.txt");
    
    function(???);
    
    inputfile.close();
    
    return 0;
    }

  2. #2
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,820
    You are on the right track.
    Code:
    void function(ifstream &file)
    {
        int c;
        file>>c;
    }
    
    int main()
    {
        ifstream inputFile("myfile.txt");
        function(inputFile);
        inputFile.close();
      
        return 0;
    }
    Woop?

  3. #3
    Registered User
    Join Date
    Apr 2006
    Posts
    27
    thanks m8

  4. #4
    the Great ElastoManiac's Avatar
    Join Date
    Nov 2005
    Location
    Republika Srpska - Balkan
    Posts
    377
    Don't forget to include <fstream> and use "using namespace std;"
    lu lu lu I've got some apples lu lu lu You've got some too lu lu lu Let's make some applesauce Take off our clothes and lu lu lu

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 03:06 PM
  2. Replies: 28
    Last Post: 07-16-2006, 11:35 PM
  3. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  4. Question on function syntax and calling function
    By cbrman in forum C Programming
    Replies: 10
    Last Post: 10-05-2003, 05:32 PM
  5. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM