Thread: need help on this fstream object coding

  1. #1
    Registered User
    Join Date
    Oct 2008
    Posts
    61

    need help on this fstream object coding

    hi i want open() accept a pointer or a reference to an ofstream object. That way, it can modify the ofstream object and pass the modified object back to the caller.
    but having trouble with my coding
    my compiler crashes..and i can't find the error becoz of that
    can anyone check whats wrong
    basically i want to open a new fstream file with any string name and pass it back to the main so it can be used..
    Code:
    #include<string>
    #include<iostream>
    #include<fstream>
    void &open(ofstream&);
    string filename;
    
    void &open(ofstream &a)
    {
    
    cout<<"name of file to save"<<endl;
    getline(cin,filename);
    
    a.open(filename.c_str(),ios::in| ios::out);
    a<<"writing this to a file\n";
    a.close();
    
    }
    
    int main()
    {
    ofstream b;
    cout<<"testing how open files from another function"<<endl;
    open(b);
    return 0;
    }

  2. #2
    Registered User
    Join Date
    Mar 2007
    Posts
    416
    using namespace std;

  3. #3
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    Ummmm....I don't think you can return a reference to void? Perhaps you should start with something that actually compiles, rather than tossing crap code together and looking for someone on the internet to fix it for you.

  4. #4
    Registered User
    Join Date
    Oct 2008
    Posts
    61
    ok2 i get this working.
    sorry for that.
    i searched every place for ofstream tutorial which shows usage of reference that can be passed back to main..but i still cannot find it..any useful links?

  5. #5
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    You need to understand passing by reference. It's got NOTHING to do with ofstream...it has to do with UNDERSTANDING THE FUNDAMENTALS. Stop trying to slap code together you find and PUT IN THE EFFORT TO LEARN AND UNDERSTAND!!!! You've been at this for at least a year, and you don't seem to have a learned anything.

    EDIT: Reference
    Last edited by rags_to_riches; 12-26-2009 at 09:51 AM.

  6. #6
    Registered User
    Join Date
    Sep 2008
    Posts
    48
    Happy holydays!
    Code:
    void open (fstream &StreamObject)
    {
    	char FileName[255];
    	cout<<"Name of file: ";
    	cin.getline( FileName, 255, '\n' );
    	StreamObject.open (FileName, ios::out);
    	if ( StreamObject.is_open() )
    	{
    		//Write something to it
    		StreamObject<<"Something";
    	}
    	else
    	{
    		//Error message
    		cout<<"Failed to open file.\n";
    	} 
    
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Inheriting Linked List
    By robolee in forum C++ Programming
    Replies: 20
    Last Post: 10-17-2009, 07:50 PM
  2. Texture Management in OpenGL
    By Brafil in forum Game Programming
    Replies: 13
    Last Post: 07-16-2009, 04:32 PM
  3. Question on l-values.
    By Hulag in forum C++ Programming
    Replies: 6
    Last Post: 10-13-2005, 04:33 PM
  4. Set Classes
    By Nicknameguy in forum C++ Programming
    Replies: 13
    Last Post: 10-31-2002, 02:56 PM
  5. Set Classes
    By Nicknameguy in forum C++ Programming
    Replies: 3
    Last Post: 10-21-2002, 07:40 PM