Thread: No clue how to do this....

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

    No clue how to do this....

    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?

    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
    
    }
    Note: I've already overloaded << operator for a different reason and it can't be changed. No parameters can be passed to the print function. The data file will be the same file everytime though. I tried to just make the code open up that same file again and append to it but it only appends at the very end of the program and then it just looks like it dumps a buffer it had filled up.

  2. #2
    Registered User
    Join Date
    Apr 2007
    Posts
    45
    If you use the flush manipulator on the stream, it will flush it to the file.

    Code:
    cout << something << flush;   // flush to cout's stream (default stdout)
    csis << something << flush;   // flush to csis's stream (your file)
    Last edited by wiiire; 04-24-2007 at 03:02 AM.
    we are one

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    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?
    In my opinion, there is no good way to go about doing this due to the artificial restrictions that your teacher created. I am loathe to suggest this, but I think you have two choices:
    1. Make csis a member reference variable. Use the constructor to pass csis when you instantiate the object. The bad part is that your object now can be modified by modifying csis from outside (e.g., from the main() function).

    2. Create an ofstream local to your print() member function. The bad part is that you probably have to hardcode the filename, and you have to consider things like having to append the content instead of overwriting.
    Last edited by laserlight; 04-24-2007 at 03:08 AM.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    Registered User
    Join Date
    Mar 2007
    Posts
    92
    Quote Originally Posted by laserlight View Post
    In my opinion, there is no good way to go about doing this due to the artificial restrictions that your teacher created. I am loathe to suggest this, but I think you have two choices:
    1. Make csis a member reference variable. Use the constructor to pass csis when you instantiate the object. The bad part is that your object now can be modified by modifying csis from outside (e.g., from the main() function).

    2. Create an ofstream local to your print() member function. The bad part is that you probably have to hardcode the filename, and you have to consider things like having to append the content instead of overwriting.

    I've already tried your second suggestion and I get the same result. I guess I can try your first one and see what's happening.

  5. #5
    Registered User
    Join Date
    Apr 2007
    Posts
    45
    and I get the same result.
    What is that, exactly? I thought before it was this :
    I tried to just make the code open up that same file again and append to it but it only appends at the very end of the program and then it just looks like it dumps a buffer it had filled up.
    You just need to flush buffers to stop that... with flush...

    Could you be more precise?
    we are one

  6. #6
    The larch
    Join Date
    May 2006
    Posts
    3,573
    If print can't take any parameters, may-be the constructor can? Or may-be you might use a static variable for the filename.
    The data file will be the same file everytime though. I tried to just make the code open up that same file again and append to it but it only appends at the very end of the program and then it just looks like it dumps a buffer it had filled up.
    I don't get this. If the print() function opens a hard-coded file for appending, the file should be closed and flushed automatically when the function ends.

  7. #7
    Registered User
    Join Date
    Mar 2007
    Posts
    92
    I'll try a static variable. I'm not sure if it will go over too well. I have to declare it in the object class and not in main though.

    Wiiire: I tried using flush and it did the same thing as you quoted.

    Here's to be more specific:
    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);
    
        cout << stuff;
    
        csis << stuff;
    
        fun.print(); //this is where the problem comes in
    
        cout << stuff;
    
        csis << stuff;
    
        .
        .
        .
    
        // This patter repeats itself about 20 times.
    
        csis.close()
    }
    The end result would look like this in the file:

    Code:
    1 stuff
    2 stuff
    3 stuff
    4 stuff
    .
    .
    .
    19 stuff
    20 stuff
    
    17 print //output from the print function (first appearance)
    18 print
    19 print
    20 print

  8. #8
    The larch
    Join Date
    May 2006
    Posts
    3,573
    It is absolutely unclear what you are trying to do and your code snippet doesn't tell what is wrong with it.
    Rather than trying out random ideas, try to think what you are exactly supposed to do. Using a global stream like that doesn't seem like a very good idea. Are you supposed to do all the printing to the file inside the class member?

  9. #9
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    You should have an ofstream variable inside the print function. You should open the file in that print function with ios::app so that it appends. When the function ends, the ofstream variable will be destroyed, which will flush the contents to the file and the close it.

    My guess is that your problem is that you are opening the file in more than one place. You should only be ever opening that file in the print function. If you open it also in main (like in that code snippet) then you will be having the same file open twice. Also notice that in that code snippet, you are not opening the file for appending, so hopefully you did that correctly in your print function.

    So forget the rest of the stuff and implement the printing by opening the file in the print function. If that doesn't work, post that code and somebody might be able to help.

  10. #10
    Registered User
    Join Date
    Mar 2007
    Posts
    92
    It is opened in the main function. So should I just close it in the print function and then reopen it?

  11. #11
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    If you need to write to it in the main function, you should open it (in append mode), write to it and close it immediately.

    Your print function should open it and write to it and close it immediately as well.

    When you create a local ofstream variable in the function, then it is closed automatically when the function ends. But the main function doesn't end until the end of the program, so it doesn't close automatically. You have to call close() yourself to ensure that your print function can open and write to it properly.

    Normally, you would pass the ofstream by reference to the print function, it is only because your requirements state that the filename should be hardcoded in the print function do you have to worry about this. Of course, if you weren't also writing to the same file in main it would be easier as well.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. No clue how to make a code to solve problems!
    By ctnzn in forum C Programming
    Replies: 8
    Last Post: 10-16-2008, 02:59 AM
  2. Replies: 8
    Last Post: 04-19-2006, 09:07 AM
  3. No clue what the problem is...
    By FingerPrint in forum C++ Programming
    Replies: 5
    Last Post: 03-29-2006, 03:16 PM
  4. A really weird problem. I understand the error, but not a clue why I get the errors.
    By Finchie_88 in forum Networking/Device Communication
    Replies: 1
    Last Post: 02-20-2005, 09:48 AM
  5. No CLUE what to do...
    By stewade in forum C Programming
    Replies: 7
    Last Post: 05-03-2004, 12:02 AM