Thread: No truncation?

  1. #1
    Rabite SirCrono6's Avatar
    Join Date
    Nov 2003
    Location
    California, US
    Posts
    269

    No truncation?

    I'm using std:: ofstream, and I'm trying to open a file without truncation. I know of std::ios::app, but I'm trying to write to the beginning of the file and std::ios::app makes every output go to the end of the file (even with std:: ofstream::seekp( 0, std::ios::beg )). I've been Googling/board searching for a while, and I've found several "solutions" that don't work. Some say std::ios::ate doesn't truncate, but it does, and I've found plenty of similar things that do the same. So how do I go about opening a file for writing, with truncating it, and so I still have the ability to write to the beginning of the file?
    From C to shining C++!

    Great graphics, sounds, algorithms, AI, pathfinding, visual effects, cutscenes, etc., etc. do NOT make a good game.
    - Bubba

    IDE and Compiler - Code::Blocks with MinGW
    Operating System - Windows XP Professional x64 Edition

  2. #2
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    use ios::ate instead of ios::app

  3. #3
    Rabite SirCrono6's Avatar
    Join Date
    Nov 2003
    Location
    California, US
    Posts
    269
    » use ios::ate instead of ios::app
    As I said in my post, it still truncates for me. For example:
    Code:
    #include <iostream>
    #include <fstream>
    
    int main( void )
    {
      std::ofstream Out( "File.txt" );
      if( !Out.is_open() )
      {
        return 0;
      }
      Out << "Triangle";
      Out.close();
      std::cin.get();
      Out.open( "File.txt", std::ios::ate );
      if( !Out.is_open() )
      {
        return 0;
      }
      Out << "Square";
      Out.close();
      return 0;
    }
    I run it, check the file, and it says "Triangle." Then I hit enter in the program, it closes, and I open the file again. Now it simply says "Square."
    From C to shining C++!

    Great graphics, sounds, algorithms, AI, pathfinding, visual effects, cutscenes, etc., etc. do NOT make a good game.
    - Bubba

    IDE and Compiler - Code::Blocks with MinGW
    Operating System - Windows XP Professional x64 Edition

  4. #4
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    You can try:
    Code:
    #include <iostream>
    #include <fstream>
    
    int main( void )
    {
      std::ofstream Out( "File.txt" );
      if( !Out.is_open() )
      {
        return 0;
      }
      Out << "Triangle";
      Out.close();
      std::cin.get();
      Out.open( "File.txt", std::ios::ate | std::ios::out | std::ios::in );
      if( !Out.is_open() )
      {
        return 0;
      }
      Out << "Square";
      Out.close();
      return 0;
    }
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  5. #5
    Rabite SirCrono6's Avatar
    Join Date
    Nov 2003
    Location
    California, US
    Posts
    269
    Well, the example wasn't entirely accurate:
    Code:
    #include <iostream>
    #include <fstream>
    
    int main( void )
    {
      std::ofstream Out( "File.txt" );
      if( !Out.is_open() )
      {
        return 0;
      }
      Out << "Triangle";
      Out.close();
      std::cin.get();
      Out.open( "File.txt", std::ios::ate | std::ios::out | std::ios::in );
      if( !Out.is_open() )
      {
        return 0;
      }
      Out.seekp( 0, std::ios::beg );
      Out << "Square";
      Out.close();
      return 0;
    }
    The resulting file is:
    Code:
    Squarele
    So now I need to figure out how to not overwrite Triangle...
    From C to shining C++!

    Great graphics, sounds, algorithms, AI, pathfinding, visual effects, cutscenes, etc., etc. do NOT make a good game.
    - Bubba

    IDE and Compiler - Code::Blocks with MinGW
    Operating System - Windows XP Professional x64 Edition

  6. #6
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    1) Assign your beginning of file text to a string variable.

    2) Read the file into another string variable.

    3) Add 1 and 2.

    4) Write the result to the file.

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > So now I need to figure out how to not overwrite Triangle...
    There is no file insert functionality.
    If you want to end up with
    Square
    Triangle
    Then you need to open a whole new file for output, open the existing file for input, and copy data from the old file to the new file, intermixed with whatever new data you want to write.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Pointer truncation
    By Magos in forum C++ Programming
    Replies: 6
    Last Post: 02-27-2006, 03:26 PM
  2. Converting a float to an int without truncation
    By SETmajor in forum C++ Programming
    Replies: 3
    Last Post: 06-16-2004, 12:39 AM
  3. weird truncation
    By kristy in forum C Programming
    Replies: 6
    Last Post: 08-06-2003, 04:03 PM
  4. Try out my new game :) !
    By Stan100 in forum Game Programming
    Replies: 10
    Last Post: 06-05-2003, 08:10 AM
  5. Warning message truncation 'double' to 'float'
    By Tojam in forum C Programming
    Replies: 4
    Last Post: 05-01-2002, 02:21 AM