• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 3640
  • Last Modified:

How to truncate the file using C++ no MFC

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
riteshromi
Asked:
riteshromi
  • 2
1 Solution
 
brettmjohnsonCommented:
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
 
riteshromiAuthor Commented:
thx for the solution. Just wanted to clarify, is this for unix or windows
would appreciate your response
0
 
brettmjohnsonCommented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now