Thread: comment header (file i/o)

  1. #1
    Registered User
    Join Date
    Aug 2003
    Posts
    44

    comment header (file i/o)

    I feel pretty stupid for not being able to figure this out, but:
    Code:
    #include <iostream>
    #include <fstream>
    
    using namespace std;
    
    int main()
    {
    	int hw_num;
    	int month;
    	int day;
    	ofstream file_out ("test.txt", ios::ate);
    
    	cout << "HW#: ";
    	cin >> hw_num;
    	cout << "Month: ";
    	cin >> month;
    	cout << "Day: ";
    	cin >> day;
    
    	file_out << "/* xxxxxxxxxxxxxxxxxx            xxxxxxxxxxxx */\n";
    	file_out << "/* xxxxxxxxxxxxx               xxxxxxxxxxxxxxxx */\n";
    	file_out << "/* HW#: " << hw_num << "                        Date: ";
    	file_out << month << "-" << day << "-03     */\n";
    
    	return 0;
    }
    I have only used file i/o to store settings in the past. I need this program to add information to the beginning of my sources for my homework, but it keeps writing over the whole file.

    How do I get it to quit writing over the file?
    And how do I make sure it adds the commenting to the beginning?

    As always, thanks in advance.
    Check out all my dimensions:
    Height, width, and for a limited time only... Depth!
    -sb

  2. #2
    root
    Join Date
    Sep 2003
    Posts
    232
    Write the header to your file before adding anything else, then use ios::app to append after the header.
    The information given in this message is known to work on FreeBSD 4.8 STABLE.
    *The above statement is false if I was too lazy to test it.*
    Please take note that I am not a technical writer, nor do I care to become one.
    If someone finds a mistake, gleaming error or typo, do me a favor...bite me.
    Don't assume that I'm ever entirely serious or entirely joking.

  3. #3
    Registered User
    Join Date
    Sep 2003
    Posts
    71
    Please close the file stream.

    file_out.close();

    Very important.
    Name: Eric Lesnar
    Learning: C/C++, SDL, WinAPI, OpenGL, and Python
    Compiler: Dev-C++ 4.9.0
    Current Game Project: Acoznict

  4. #4
    root
    Join Date
    Sep 2003
    Posts
    232
    >Very important.
    No, not really. When the end of the block is reached, the destructor for ofstream will be called and the object destroyed. Only really anal people explicitly close a file stream if it would be destroyed anyway.
    The information given in this message is known to work on FreeBSD 4.8 STABLE.
    *The above statement is false if I was too lazy to test it.*
    Please take note that I am not a technical writer, nor do I care to become one.
    If someone finds a mistake, gleaming error or typo, do me a favor...bite me.
    Don't assume that I'm ever entirely serious or entirely joking.

  5. #5
    mustang benny bennyandthejets's Avatar
    Join Date
    Jul 2002
    Posts
    1,401
    Only really anal people explicitly close a file stream if it would be destroyed anyway.
    I think it's considered good programming to explicitly close everything you open. It might not make a difference, but it ensures that you are aware of its current state, etc. But I do agree that it is NOT "very important" to close it.
    [email protected]
    Microsoft Visual Studio .NET 2003 Enterprise Architect
    Windows XP Pro

    Code Tags
    Programming FAQ
    Tutorials

  6. #6
    root
    Join Date
    Sep 2003
    Posts
    232
    >I think it's considered good programming to explicitly close everything you open.
    I think it's redundant to do something explicitly that would be done implicitly anyway, especially if the language standard defines that kind of behavior. But, everyone has a different style, and I'm tolerant of that.
    The information given in this message is known to work on FreeBSD 4.8 STABLE.
    *The above statement is false if I was too lazy to test it.*
    Please take note that I am not a technical writer, nor do I care to become one.
    If someone finds a mistake, gleaming error or typo, do me a favor...bite me.
    Don't assume that I'm ever entirely serious or entirely joking.

  7. #7
    unleashed alphaoide's Avatar
    Join Date
    Sep 2003
    Posts
    696
    You might save some memory space by closing it.

  8. #8
    root
    Join Date
    Sep 2003
    Posts
    232
    >You might save some memory space by closing it.
    This only matters if you plan on using more variables and your stack space is incredibly small. But in the example given, the program terminates after using the output stream, so why bother freeing the stack space for further variables if you're just going to end the program with the next line? If your program looks more like this:
    Code:
    // Lots of code
    
    ofstream fd;
    // Use fd
    
    // Lots of code that doesn't need fd
    Then yea, it makes sense to close the stream when you're done. But if your code looks like this:
    Code:
    // Lots of code...or not
    
    ofstream fd;
    // Use fd
    
    // Terminate the program
    Why bother doing something that will be done anyway? The only good reason is style.
    The information given in this message is known to work on FreeBSD 4.8 STABLE.
    *The above statement is false if I was too lazy to test it.*
    Please take note that I am not a technical writer, nor do I care to become one.
    If someone finds a mistake, gleaming error or typo, do me a favor...bite me.
    Don't assume that I'm ever entirely serious or entirely joking.

  9. #9
    unleashed alphaoide's Avatar
    Join Date
    Sep 2003
    Posts
    696
    I got your point. But who plans to always write simple programs like that? If you don't make it habit to close in simple programs, you might forget to do it when it's necessary.

  10. #10
    root
    Join Date
    Sep 2003
    Posts
    232
    >you might forget to do it when it's necessary.
    That's speculation at best. If you know when to use something and when you don't have to, you're probably experienced enough to remember such things. People tend to forget things when they fall into a routine. By always analyzing your code and paying attention when you write code, this doesn't happen. For example, when I create a stream object I always look at where I'm going to be using it and losing it and I act accordingly, much like with dynamic memory management.
    The information given in this message is known to work on FreeBSD 4.8 STABLE.
    *The above statement is false if I was too lazy to test it.*
    Please take note that I am not a technical writer, nor do I care to become one.
    If someone finds a mistake, gleaming error or typo, do me a favor...bite me.
    Don't assume that I'm ever entirely serious or entirely joking.

  11. #11
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    Originally posted by twm
    Only really anal people explicitly close a file stream if it would be destroyed anyway.
    i guess i must be really anal... i always destroy/close things that i don't need as soon as i don't need them anymore...
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  12. #12
    root
    Join Date
    Sep 2003
    Posts
    232
    >i always destroy/close things that i don't need as soon as i don't need them anymore...
    So do I, just not always explicitly.
    The information given in this message is known to work on FreeBSD 4.8 STABLE.
    *The above statement is false if I was too lazy to test it.*
    Please take note that I am not a technical writer, nor do I care to become one.
    If someone finds a mistake, gleaming error or typo, do me a favor...bite me.
    Don't assume that I'm ever entirely serious or entirely joking.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Subtle(?) File I/O Problem
    By cecomp64 in forum C Programming
    Replies: 9
    Last Post: 07-16-2008, 11:39 AM
  2. Need Help Fixing My C Program. Deals with File I/O
    By Matus in forum C Programming
    Replies: 7
    Last Post: 04-29-2008, 07:51 PM
  3. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  4. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 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