Thread: Pointers & File I/O

  1. #1
    For Narnia! Sentral's Avatar
    Join Date
    May 2005
    Location
    Narnia
    Posts
    719

    Pointers & File I/O

    Hello, I was wondering if pointers could be used with in file i/o. What I want to do with this is be able to have the ofstream in one function. Than use the pointer to show the ifstream in another function. So if the program says,"Enter your name", it will then save my name in that function where it asks me. Then if I go to a function, lets call it showlist(), then I should be able to see my name in that function. If you dont understand me, tell me
    If it can't be done tell me what can be done to achieve what i want.
    Videogame Memories!
    A site dedicated to keeping videogame memories alive!

    http://www.videogamememories.com/
    Share your experiences with us now!

    "We will game forever!"

  2. #2
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    If you dont understand me
    Yep, that part.

  3. #3
    For Narnia! Sentral's Avatar
    Join Date
    May 2005
    Location
    Narnia
    Posts
    719
    Ex. With false code:

    Code:
    function makefile()
    {
             what is your name?
             // save the name in a .txt file
    }
    function showname()
    {
        show name, from function makefile
       in other words, open the .txt to show the contents
    }
    How would I be able to open the .txt file in another function, pointers? Because when I tried to do it wo/ pointers it came up with a bunch of undeclare errors.
    Videogame Memories!
    A site dedicated to keeping videogame memories alive!

    http://www.videogamememories.com/
    Share your experiences with us now!

    "We will game forever!"

  4. #4
    I am me, who else?
    Join Date
    Oct 2002
    Posts
    250
    It sounds like you want to pass a file pointer as an argument to a function correct?

    You could do it one of two ways I suppose, if you are using classes, make the fstream var a class member. You could make it static as well, but I think the member approach is better.

    If this is not in a class based program, you could make the file pointer global, or just pass the FILE pointer directly. You must pass the FILE as a pointer because when you call fopen you get back a pointer.

    I am still a bit confused about what you want, but perhaps answers to my questions or comments will help us.

  5. #5
    Registered User
    Join Date
    Jan 2003
    Posts
    311
    You can pass streams around by reference relatively freely, however this may not be quite what you want. If you want to search in a stream/file for a particular string you could do something like this
    Code:
    std::string search_line(const std::string &pat, std::istream &is) {
        std::string tok;
        while(is >> tok) {
            if(tok == pat) {
                std::string tail;
                std::getline(is,tail);
                return tok + tail;
            }
        }
        return "";
    }
    ....
    std::ifstream ifs("file.txt");
    std::string line = search_line("joe",ifs);
    if(!line.empty()) std::cout << "Found " << line << std::endl;
    This will advance through a stream/file until a string exactly matches pat, then return that line from the token to the end of line.

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    You don't need to pass anything anywhere. In the makefile function, write out the file using an ofstream local to that function (so that it closes when the function ends). In the showname function, display the contents of the file using an ifstream local to that function (so that it closes when the function ends). As long as the file names are the same it will work. At worst all you need to pass is the filename as a string to each method if the name changes.

  7. #7
    I am me, who else?
    Join Date
    Oct 2002
    Posts
    250
    What if he wants to keep the file open? There may be instances where closing and reopening the file is a waste of time, especially if he has a loop going where he is writing/reading a file. Opening and closing it each cycle could be a tremendous waste of time, not to mention a bit lazy.

    In this SPECIFIC case, it would be fine to do so, but its a perfectly valid thing to know how to pass input/output pointers to a function.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. gcc link external library
    By spank in forum C Programming
    Replies: 6
    Last Post: 08-08-2007, 03:44 PM
  2. Post...
    By maxorator in forum C++ Programming
    Replies: 12
    Last Post: 10-11-2005, 08:39 AM
  3. Encryption program
    By zeiffelz in forum C Programming
    Replies: 1
    Last Post: 06-15-2005, 03:39 AM
  4. Need a suggestion on a school project..
    By Screwz Luse in forum C Programming
    Replies: 5
    Last Post: 11-27-2001, 02:58 AM