Thread: passing a file to a function

  1. #1
    Registered User
    Join Date
    Feb 2005
    Posts
    5

    passing a file to a function

    Hey All, I've just starting learning C++ from C (Not that i was that good as C to begin with) and I'm trying to convert some programs dealing with file i/o form C to C++. I'm stuck at a spot where I have a file handle passed to a funciton, for example-

    Code:
    aFile=fopen("pic.bmp","rb");
    filegetsize(aFile);
    Then Later-
    Code:
    long filegetsize(somefile)
    {
          do stuff with somefile
    }
    But from what i understand of C++ you simply use
    " TheFile.open ("pic.bmp",ios::binary | ios::in) " to open the file. However this doesn't give me a "file handle" that i can pass to another function as an argument. Am I making sense? How would one do this is C++?

  2. #2
    #define WORLD "sad place" LinuxCoder's Avatar
    Join Date
    Mar 2006
    Location
    Portugal
    Posts
    89
    The c++ way of working with files is through file stream objects. There are 3 types of file streams, according to the operation you are to perform on it. There is std::ifstream for input operations, std::ofstream for output operations and std::fstream which allows both acess types. If you start using these objects then you can pass the object as a reference to any function where you intend to use it. For instance:
    Code:
    #include <fstream>
    
    void somefn(const std::fstream& myfile)        // notice the function takes a reference (notice the &)
    {
            // perform some I/O on the file
    }
    
    int main()
    {
            std::fstream myfile("somefile.txt", std::ios::out | std::ios::trunc);      // set flags defining open mode, in this case output + truncate mode
    
            // check if file was open
            if (!myfile.is_open) {
                    std::cout << "unable to open file. quitting." << std::endl;
                    exit(1);
            }
            
            somefn(myfile);
    }
    Using file streams is quite nice as it simplifies a lot some file i/o operations. writting and reading operations, since it is a stream it allows you to use the some of the methods you use with regular streams.

    cheers
    Last edited by LinuxCoder; 04-08-2006 at 04:55 AM.

  3. #3
    Registered User
    Join Date
    Feb 2005
    Posts
    5
    O, OK thanks.
    So this '&' is no longer 'address of' then or is it both (or is it the same thing?)
    I guess I'll do some more reading around....
    The tourtial i was reading just used

    " TheFile.open ("somefile.txt", std::ios::out | std::ios::trunc); "

    instead of

    " std::fstream myfile("somefile.txt", std::ios::out | std::ios::trunc); "

    So i never really got the whole fstream part... that then would be the "data type" per se?

  4. #4
    Registered User
    Join Date
    Dec 2005
    Posts
    54
    Quote Originally Posted by evilertoaster
    O, OK thanks.
    So this '&' is no longer 'address of' then or is it both (or is it the same thing?)
    I guess I'll do some more reading around....
    The tourtial i was reading just used

    " TheFile.open ("somefile.txt", std::ios:ut | std::ios::trunc); "

    instead of

    " std::fstream myfile("somefile.txt", std::ios:ut | std::ios::trunc); "

    So i never really got the whole fstream part... that then would be the "data type" per se?
    the & also is used for sending references of objects. I personaly like to make a function that takes a file path and then does the opening inside that function.

    fstream has to types, ostream and istream. you can use fstream i believe which works both ways. don't quote me on that though, i havnt tried it myself. I used to hate file i/o, but the more I have been using it I am really starting to enjoy it.

  5. #5
    Registered User
    Join Date
    Feb 2005
    Posts
    5
    humkay, thanks a bunch

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    In the tutorial, TheFile probably was an std::fstream variable. If you pass the filename to the constructor it is opened immediately. If you don't, you have to call open() when you are ready to open the file.

    >> that then would be the "data type" per se?
    Yes.

    >> fstream has to types, ostream and istream
    ofstream (output only), ifstream (input only), and fstream (both). If you are only doing input, use ifstream. If you are only doing output with that variable, use ofstream. Only if you might do either or both do you need fstream.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Undefined Reference Compiling Error
    By AlakaAlaki in forum C++ Programming
    Replies: 1
    Last Post: 06-27-2008, 11:45 AM
  2. To find the memory leaks without using any tools
    By asadullah in forum C Programming
    Replies: 2
    Last Post: 05-12-2008, 07:54 AM
  3. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  4. Scope And Parameter Passing
    By djwicks in forum C Programming
    Replies: 6
    Last Post: 03-28-2005, 08:26 PM
  5. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM