[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now


How to truncate the file using C++ no MFC

Posted on 2006-04-03
Medium Priority
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
  • 2
LVL 23

Accepted Solution

brettmjohnson earned 200 total points
ID: 16366710
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

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

Expert Comment

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


Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In real business world data are crucial and sometimes data are shared among different information systems. Hence, an agreeable file transfer protocol need to be established.
When you discover the power of the R programming language, you are going to wonder how you ever lived without it! Learn why the language merits a place in your programming arsenal.
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 seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

873 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