Linux C ftruncate function Empty file considerations (to reset offsets using Lseek)

Source: Internet
Author: User

Before there was a need to empty open files and then re-write the requirements, but using ftruncate (FD, 0), and did not achieve the effect, but the file head has ' "", the length is larger than expected. The reason is that I did not use Lseek to reset the file offset, I was too naïve to think that the empty file will be written from the beginning.

-------------------------------------I was explaining the split line--------------------------------------

First man ftruncate look at the Help handbook

NAME
Truncate, ftruncate- truncate a file to a specified length

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

DESCRIPTION
The truncate () and ftruncate () functions cause the regular file named by path or referenced by FD to is truncated to a siz E of precisely length bytes.
If The file previously was larger than this size, the extra data is lost. If the file previously was shorter, it's extended, and the extended part reads as null bytes ('% ').
The file offset is not changed.
If the size changed, then the St_ctime and St_mtime fields (respectively, time of last status change and time of last modification; See Stat (2)) for the file is updated, and the Set-user-id and
Set-group-id permission bits may be cleared.
With Ftruncate (), the file must is open for writing; With truncate (), the file must is writable.

Before it was because I did not see the red line of words, causing me to create a file at the beginning of the error, said the file offset is not changed!

The experiment is as follows :

#include <stdio.h> #include <string.h> #include <unistd.h> #include <sys/types.h> #include < Sys/stat.h> #include <fcntl.h>int main (void) {int fd;const char *s1 = "0123456789"; const char *S2 = "ABCDE"; fd = op En ("test.txt", O_creat | o_wronly | O_trunc, 0666);/* If Error */write (FD, S1, strlen (S1)), Ftruncate (FD, 0);//Lseek (FD, 0, Seek_set); write (fd, S2, strlen (S2) ); Close (FD); return 0;}
Run effect

Remove Lseek (FD, 0, Seek_set); Comments, the effect is as follows:


Conclusion :

From the above two graphs, it can be seen that the file size of no Lseek is 15, with XXD view 16 binary format to see the file header has 10 ' \ "Fill.

After resetting the file offset, the file size is 5 and the content is correct.

Therefore, when using the Ftruncate function, write again to be sure to reset the file offset (either before or after ftruncate, with Lseek or rewind).

Linux C ftruncate function Empty file considerations (to reset offsets using Lseek)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.