calling a fstream in a function

This is a discussion on calling a fstream in a function within the C++ Programming forums, part of the General Programming Boards category; so .. in c if i had a file pointer e.g: Code: void function(FILE *inputfile){ int c; fscanf(inputfile,"%d",c); } int ...

  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,812
    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, 04:06 PM
  2. Replies: 28
    Last Post: 07-17-2006, 12:35 AM
  3. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 07:39 AM
  4. Question on function syntax and calling function
    By cbrman in forum C Programming
    Replies: 10
    Last Post: 10-05-2003, 06: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, 02:27 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21