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

x
?
Solved

How to truncate the file using C++ no MFC

Posted on 2006-04-03
5
Medium Priority
?
3,548 Views
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.
0
Comment
Question by:riteshromi
  • 2
3 Comments
 
LVL 23

Accepted Solution

by:
brettmjohnson earned 200 total points
ID: 16366710
Use truncate() or ftrunctate()

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

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

SYNOPSIS
     #include <unistd.h>

     int
     truncate(const char *path, off_t length);

     int
     ftruncate(int fd, off_t length);

DESCRIPTION
     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.

RETURN VALUES
     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.

0
 

Author Comment

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

Expert Comment

by:brettmjohnson
ID: 16366883
They appear to be Unix interfaces.  For Microsoft OS, use _chsize() or _chsize_s():
http://msdn2.microsoft.com/en-us/library/whx354w1(VS.80).aspx

0

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