Thread: does ios::ate truncates the file it the file already exist

  1. #1
    Deleted Account
    Join Date
    Oct 2013
    Posts
    39

    does ios::ate truncates the file it the file already exist

    does ios::ate truncates the file it the file already exist in c++.......
    i tried and found that ios::ate truncates the file...

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    If you want to append, use ios::app instead.
    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

  3. #3
    Deleted Account
    Join Date
    Oct 2013
    Posts
    39
    yeah......i know that i can append using ios::app ..........

    but my question is that will ios::ate truncates the file ...?

    what is the use of using ios::ate if it truncates the file.....there is no use of appending if no data is present in file (ie.it gets truncates)

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Well, it should not be truncating the file since that is supposed to be indicated by ios::trunc, not ios::ate.
    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

  5. #5
    Deleted Account
    Join Date
    Oct 2013
    Posts
    39
    yes but i don't know why it is getting truncates.........any idea

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Post your code and post your input file before and after running your program.
    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

  7. #7
    Deleted Account
    Join Date
    Oct 2013
    Posts
    39
    hey i notice that in Turbo C++ the ios::ate is working as it should....ie it doesn't truncates the file.......

    but at the same time in code::blocks Mingw compiler it is truncating the file

  8. #8
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by Rohit Kumar View Post
    hey i notice that in Turbo C++ the ios::ate is working as it should....ie it doesn't truncates the file.......

    but at the same time in code::blocks Mingw compiler it is truncating the file
    That goes down as the most uninformative response of the month to a request for code and relevant input files.

    Odds are, you are using some magic value (e.g. or'ing with ios::ate) when opening the file. With MingW, that value causes truncation. With Turbo C++, you just got lucky that it didn't.

    If you are going to persist in not providing useful information, and hoping that people will guess what your problem is, I suspect you will be disappointed.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Rohit Kumar
    hey i notice that in Turbo C++ the ios::ate is working as it should....ie it doesn't truncates the file.......

    but at the same time in code::blocks Mingw compiler it is truncating the file
    I don't understand your refusal to post your code. My guess is that you used an ofstream, which then truncated the file when opening. If you used an fstream, ifstream, or added ios::in, perhaps this would not have happened. But I'm just guessing because of your obstinate refusal to post code that I can examine and perhaps test for myself.

    To show you that I practice what I preach, here's a program in which ios::ate is used and for which the file is not truncated, even when the program is compiled with gcc (which is what MinGW is a port of):
    Code:
    #include <fstream>
    
    int main()
    {
        std::fstream file("hello.txt", std::ios::ate);
    }
    The content of hello.txt before and after running this program was the string "hello".
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 03-25-2013, 01:33 AM
  2. Replies: 1
    Last Post: 05-13-2012, 10:00 AM
  3. Replies: 2
    Last Post: 04-13-2012, 07:39 PM
  4. Replies: 7
    Last Post: 06-16-2008, 01:18 PM
  5. hwo to check if a file exist in certain dir?
    By Jasonymk in forum C++ Programming
    Replies: 4
    Last Post: 03-02-2003, 08:20 PM