How to truncate the file using C++ no MFC

Posted on 2006-04-03
Last Modified: 2007-11-27
I want to truncate a file using c++. Tried moving the end of file but the data still present. Can someone please tell me how to truncate the file.
Question by:riteshromi
    LVL 23

    Accepted Solution

    Use truncate() or ftrunctate()

    TRUNCATE(2)                 BSD System Calls Manual                TRUNCATE(2)

         truncate, ftruncate -- truncate or extend a file to a specified length

         #include <unistd.h>

         truncate(const char *path, off_t length);

         ftruncate(int fd, off_t length);

         Truncate() causes the file named by path or referenced by fd to be trun-
         cated or extended to length bytes in size.  If the file previously was
         larger than this size, the extra data is lost. If the file was smaller
         than this size, it will be extended as if by writing bytes with the value
         zero.  With ftruncate(), the file must be open for writing.

         A value of 0 is returned if the call succeeds.  If the call fails a -1 is
         returned, and the global variable errno specifies the error.


    Author Comment

    thx for the solution. Just wanted to clarify, is this for unix or windows
    would appreciate your response
    LVL 23

    Expert Comment

    They appear to be Unix interfaces.  For Microsoft OS, use _chsize() or _chsize_s():


    Featured Post

    How to improve team productivity

    Quip adds documents, spreadsheets, and tasklists to your Slack experience
    - Elevate ideas to Quip docs
    - Share Quip docs in Slack
    - Get notified of changes to your docs
    - Available on iOS/Android/Desktop/Web
    - Online/Offline

    Join & Write a Comment

    Suggested Solutions

    Title # Comments Views Activity
    Help with my python script 6 137
    fix34  challenge 9 73
    java constructor error 8 66
    wordappend challenge 8 56
    Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
    In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
    An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
    In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

    745 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    15 Experts available now in Live!

    Get 1:1 Help Now