Thread: Interesting problem with file I/O...

  1. #1
    Registered User skillet's Avatar
    Join Date
    Feb 2002
    Posts
    20

    Question Interesting problem with file I/O...

    I've used this board to my benefit for a few weeks now, so I thought I'd better register and say "Thanks".... "Thanks!"

    Here is a problem I'm currently kicking around (yes, it is for a C++ class, no it is not due tomorrow), any ideas are appreciated.

    I have a simple program that creates "rectangles" (a class with "width" and "length" data members). I have to create multiple rectangles, change lengths or widths on some, check to see if any of them are squares... no sweat so far. The catch is:

    *we have to send our output to a file--AND the output has to include a notice of each object going out of scope (when the program ends)*

    SO, how do I go about getting two or more destructors from different objects writing to the same output file?

    I am working on getting a static ofstream object in the public data members of my rectangle class-- with some trouble. I'm not sure that can be done yet...

  2. #2
    ¡Amo fútbol!
    Join Date
    Dec 2001
    Posts
    2,138
    How about you do this for the destructor:

    ~rectangle(){cout<<"rectangle being destroyed";};

  3. #3
    Registered User skillet's Avatar
    Join Date
    Feb 2002
    Posts
    20
    I originally did all my output with cout, but I need the output to a file now...

    The actual output looks something like this (I shortened it from five rectangles to two):
    ***
    rectangle number 1 has a length of 1.0
    rectangle number 1 has a width of 1.0
    rectangle number 1 has a perimeter of 4.0
    rectangle number 1 has an area of 1.0
    rectangle number 1 is a square.

    rectangle number 2 has a length of 7.1
    rectangle number 2 has a width of 3.1
    rectangle number 2 has a perimeter of 20.4
    rectangle number 2 has an area of 22.0
    rectangle number 2 is not a square.

    rectangle number 1 has gone out of scope
    rectangle number 2 has gone out of scope
    ***

    all this is sent to one file, from different objects. (ideally).

  4. #4
    Registered User skillet's Avatar
    Join Date
    Feb 2002
    Posts
    20
    Actually, rectangle number 2 will go out of scope first, so I typed the output wrong.

    I can't pass an object (output file) to the destructor when it is called by the program ending, so I can't send it the output file as a parameter. It will have to be set up in the class data. I think the class could contain a pointer or reference to the output file, instead of a ofstream object... can define a reference in the data members to an output file that is not created until "main" is executed? Maybe I need to make and open a "global" file before the class is prototyped? I'm rambling here, haven't tried it yet...

  5. #5
    ¡Amo fútbol!
    Join Date
    Dec 2001
    Posts
    2,138
    my bad, I meant:

    numrects=1;
    ~rectangle(){fout<<"rectangle "<<numrects++<<" destroyed";}

    ofstream fout("outputfile.txt");
    //fout<<all data then destructor will be called

  6. #6
    Registered User skillet's Avatar
    Join Date
    Feb 2002
    Posts
    20
    This is probably important too: I have three files, the rectangle prototype (rectangle.h), the rectangle class definition(rectangle.cpp), and an "assignment2.cpp" file. And FYI, I use MSVC++ 6.0.

    here is my destructor:

    Rectangle::~Rectangle() {
    rectanglefile << "rectangle " << ID << " has gone out of scope." << endl;
    }

    If I create my output file in the assignment.cpp file (above main function), the destructor doesn't know what it is. If I put this in the rectangle.cpp file:

    ofstream rectanglefile("rectangle.txt");

    I get multiple cryptic errors.

    I'm going to attach my code to this post, can I attach multiple files?...

    It's past this old man's bedtime. I'll check in tomorrow.

    Thanks!!

  7. #7
    Registered User skillet's Avatar
    Join Date
    Feb 2002
    Posts
    20
    and the definition...

  8. #8
    Registered User skillet's Avatar
    Join Date
    Feb 2002
    Posts
    20
    and the important part...

  9. #9
    Registered User skillet's Avatar
    Join Date
    Feb 2002
    Posts
    20
    You will notice warnings if you compile this. I was going to get to that later... I don't understand why it is considering a floating point number with only one number after the decimal point a constant double.

    The code I attached works except getting the destructor to write into the file. It seems that the file may close before the destructor for the objects are called?

  10. #10
    Registered User skillet's Avatar
    Join Date
    Feb 2002
    Posts
    20

    Lightbulb

    I made the previous post, went to bed, and had a big "DUH" moment. I am closing the output file in my main function. of course the destructor won't write to it. One would think I would get some kindof error message for writing to a file that isn't open?

    still, take out the last line in assignment2.cpp and everything is hunky-dory.

    I may turn all my floats into doubles to get rid of the warnings, unless I hear a better idea?

    Thanks for letting me bounce my ideas off this board! Hope I can be of help to others in the future!

  11. #11
    Registered User Paro's Avatar
    Join Date
    Feb 2002
    Posts
    160

    Talking

    skill has made 88.9% of his posts on this thread alone... lol
    Paro

  12. #12
    Registered User skillet's Avatar
    Join Date
    Feb 2002
    Posts
    20
    Yeah Yeah... laugh away. here's one more

  13. #13
    Registered User Paro's Avatar
    Join Date
    Feb 2002
    Posts
    160
    you just moved up to 90.0% even... keep this up and we will devote this thread to you alone
    Paro

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. File i/o problem
    By tezcatlipooca in forum C++ Programming
    Replies: 18
    Last Post: 01-01-2007, 09:01 AM
  2. File I/O problem
    By Onions in forum C++ Programming
    Replies: 41
    Last Post: 02-24-2006, 04:32 PM
  3. File I/O problem
    By 81N4RY_DR460N in forum C++ Programming
    Replies: 12
    Last Post: 09-03-2005, 12:14 PM
  4. Possible circular definition with singleton objects
    By techrolla in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2004, 10:46 AM
  5. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 04:10 PM