Outputing to a file

This is a discussion on Outputing to a file within the C++ Programming forums, part of the General Programming Boards category; I know how to write basic stuff to a file but I'm having a problem. In my main function a ...

  1. #1
    Registered User
    Join Date
    Mar 2007
    Posts
    92

    Outputing to a file

    I know how to write basic stuff to a file but I'm having a problem.

    In my main function a .txt file is opened up to write data to. The main function creates some objects that are output by using a print function.
    Code:
    object.print();
    The problem that I'm having is that I can't figure out a way to write the data to the same file. The way the program is going to work is the file is always going to be the same name and I can't pass that name as a variable. So I tried just opening up that file in the object member function print's code and appending data to it but that did not work. What's a good way to go about doing this?

  2. #2
    Registered User
    Join Date
    Dec 2004
    Posts
    465
    Could you please clarify? I don't see what exactly your goal is, why you can't achieve it, or what object.print() has to do with it.
    My computer is awesome.

  3. #3
    Registered User
    Join Date
    Mar 2007
    Posts
    92
    I have a main function:

    Code:
    ofstream csis;
    
    int main(){
    
        csis.open("csis.dat");
    
        Do Stuff...
    
        cout << stuff; //outputs stuff to the terminal window
        
        csis << stuff; //writes the same stuff to the file
    
        Object fun;
    
        fun.dosomething(x,y);
    
        fun.print(); //this is where the problem comes in
    
        csis.close()
    }
    The Object member function print looks like this:
    Code:
    void Object::print(){
    
        cout << something; //outputs something to the terminal window;
    
        csis << something; //I want to somehow write this to the same file/append it to the file
    
    }
    Last edited by warfang; 04-24-2007 at 01:58 AM.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    20,968
    Instead of (or in addition to) a print() member function, overload operator<< for output streams. This may have to be a friend function.
    Code:
    std::ostream& operator<<(std::ostream& out, const Object& rhs) {
        return out << rhs.something; // Or rhs.something(), then no need to be a friend function.
    }
    With that done, you can easily write:
    Code:
    cout << fun;
    csis << fun;
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Mar 2007
    Posts
    92
    I'm already overloading the << operator for a different purpose so I don't think I can do it that way. Print and << are outputting different things.

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    20,968
    I'm already overloading the << operator for a different purpose so I don't think I can do it that way. Print and << are outputting different things.
    Then just do it this way:
    Code:
    void Object::print(std::ostream& out) {
        out << something;
    }
    Out of curiosity, what are you doing with your current overloaded operator<< ?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User
    Join Date
    Mar 2007
    Posts
    92
    overloaded operator << only outputs the string in my object.

    print() outputs all variables in my object.

    I still don't think that is going to fix my problem though . I'll try it but if it doesn't work I'll post up my code. I'm working on other parts of it. This outputting is important but I have more code to write before I can come back to this. I'll post up my code after I'm done coding the rest.

  8. #8
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Quote Originally Posted by warfang View Post
    overloaded operator << only outputs the string in my object.

    print() outputs all variables in my object.
    FWIW, I'm not really a C++ guy, but I find that counterintuitive. If I used operator<<, I would expect it to output the whole object since it is "acting" on the whole object.

    A special printing function that only does a subset of the class to me it would seem to be the job of a special printing function such as printstring.

    [edit]And would you want this function to receive a stream reference such that you could specify an fstream or iostream?
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  9. #9
    Registered User
    Join Date
    Mar 2007
    Posts
    92
    Go ahead and tell my teacher that one. I wish we had that much freedom in his labs... It would make them a lot less confusing.

  10. #10
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    If the problem is in the guts of the implementation of this function, it would be wise to post the guts of the implementation. A minimal compilable snippet of code that demonstrates the issue(s) is generally the fastest way to a solution.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  11. #11
    Registered User
    Join Date
    Mar 2007
    Posts
    92
    EDIT: Look at my last post with the code...
    Last edited by warfang; 04-23-2007 at 09:32 PM.

  12. #12
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Em. Didja know you can attach files to posts? Or even zip a project and post the zip?

    Anyway, without "strdrv.h" -- unless I messed it, which happens all too frequently -- I can't try to build what you have.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  13. #13
    Registered User
    Join Date
    Mar 2007
    Posts
    92
    Wow, no I didn't know you could add a files to posts. Well I'll try it now .

    Well it didn't let me add a zip so in the file name I added .txt. So it's String.zip.txt. All you'll have to do is take out the .txt from the filename and you should be able to unzip it. thanks.

    EDIT: The derived files are "case.cpp" and "reverse.cpp". The base file is "string.cpp" and the driver file "strdrv.cpp".

    Note: You'll have to comment out test21 because I'm having trouble with a function.
    Last edited by warfang; 04-24-2007 at 12:31 AM.

  14. #14
    Registered User
    Join Date
    Mar 2007
    Posts
    92
    Rest of the files.
    Last edited by warfang; 04-24-2007 at 12:31 AM.

  15. #15
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Quote Originally Posted by warfang View Post
    Wow, no I didn't know you could add a files to posts. Well I'll try it now .

    Well it didn't let me add a zip so in the file name I added .txt. So it's String.zip.txt. All you'll have to do is take out the .txt from the filename and you should be able to unzip it.
    My bad. At one time I thought .zip's we're okay.

    My time is limited, but I think you've posted enough for a thorough run-through by those that follow. Or else you'll hear otherwise.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  2. System
    By drdroid in forum C++ Programming
    Replies: 3
    Last Post: 06-28-2002, 10:12 PM
  3. Hmm....help me take a look at this: File Encryptor
    By heljy in forum C Programming
    Replies: 3
    Last Post: 03-23-2002, 09:57 AM
  4. Need a suggestion on a school project..
    By Screwz Luse in forum C Programming
    Replies: 5
    Last Post: 11-27-2001, 01:58 AM

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