Home PC Games Linux Windows Database Network Programming Server Mobile  
           
  Home \ Programming \ The Linux C truncate function clears the file notes     - Python uses multi-process pool (Programming)

- RAID disk array Description (Linux)

- JavaScript subarray Deduplication (Programming)

- CentOS 7 hard disk expansion toss record (Linux)

- Linux desktop system using the remote server in clear text ssh password (Server)

- Linux file compression and file system packaged with instructions (Linux)

- [Android] Eclipse does not update the Android SDK Manager solution [using GoAgent] (Programming)

- Linux System Administrator common interview questions and answers 30 (Linux)

- How to configure a server in a MySQL Cluster (Database)

- Log4cplus logging facility configuration, installation, testing (Linux)

- Ubuntu download install and configure Plank Dock (Linux)

- OpenGL Superb Learning Notes - Fragment Shader (Programming)

- Spring Data study notes -Helloworld (Programming)

- ActiveMQ-based shared file system HA solutions (Server)

- Java recognize simple codes (Programming)

- C # Future: Method Contract (Programming)

- To install PostgreSQL 9.4 (Database)

- The direct insertion sort algorithm (Programming)

- Installation CD audio file extraction tool Flacon (Linux)

- To install Oracle Database Details and FAQ Summary under CentOS (Database)

 
         
  The Linux C truncate function clears the file notes
     
  Add Date : 2018-11-21      
         
         
         
  Before the opening of a document to be emptied, and then re-written demand, but the use of ftruncate (fd, 0), and did not achieve the effect, but the document has a head '\ 0', the length of the larger than expected. The reason is not using lseek reset file offset, I was too naive, and that empty file will be written from scratch.

First man ftruncate Look at the help manual

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 be truncated to a size of precisely length bytes.
       If the file previously was smaller than this size, the extra data is lost. If the file previously was shorter, it is extended, and the extended part reads as null bytes ( '\ 0').
       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 are updated, and the set-user-ID and
       Set-group-ID permission bits may be cleared.
       With ftruncate (), the file must be open for writing; with truncate (), the file must be writable.

Before that is because I did not see the red line that led to the beginning of the paper I have the wrong paper said that the document will not change the offset!

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 = open ( "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;
}}

in conclusion:

From the above two pictures, you can see, do not lseek the file size is 15, with hexadecimal format to see xxd see the file header has 10 '\ 0' fill.

And reset the file offset, the file size is 5, the content is correct.

Therefore, when using ftruncate function, re-write must re-set the file offset (ftruncate before or after the line, with lseek or rewind can be).
     
         
         
         
  More:      
 
- Common Linux system performance monitoring command (Linux)
- Java reflection summary (Programming)
- Linux automatic installation and implementation (Linux)
- MySQL master-slave database configuration and error handling Raiders (Database)
- Linux platform Oracle MySQL connection (Database)
- 10 Regulation of painless SQL Schema (Database)
- Linux system performance and usage activity monitoring tools -Sysstat (Linux)
- Cooling solutions Ubuntu system (Linux)
- GoldenGate for Oracle data consistency initializing (Database)
- Some safety precautions of Linux servers (Linux)
- RedHat Linux 7 build yum source server (Server)
- Simple RPM package production (Linux)
- Lua regex (string function) (Programming)
- The method of CentOS-7.0. The installation and configuration of Tomcat-7 (Server)
- Create the best introductory tutorial REST API (Linux)
- Easily create RPM packages using the FPM (Linux)
- When Linux Detailed time zone and common function of time (Linux)
- How to create SWAP files in Ubuntu 14.04 (Linux)
- Use OpenSSL carried BASE64 encoding and decoding (Linux)
- Learning Linux coding style (Programming)
     
           
     
  CopyRight 2002-2022 newfreesoft.com, All Rights Reserved.