Get the App
SLTechnology News&Howtos  ›  Servers  › 

The method of changing file size by Linux

Shulou Source: shulou.com Published: 2022-06-02 04:08:20 10月15日 Update

Function prototype:

# include int ftruncate (int fd, off_t length); / / change the file size to the specified size of length; return value 0 for successful execution and-1 for failure.

The function ftruncate changes the file size specified by parameter fd to the size specified by parameter length. The parameter fd is an open file descriptor and must be a file opened in write mode. If the original file size is larger than the parameter length, the excess will be deleted.

Test.c

# include#include#include#include#include#includeint main (int argc,char** argv) {struct stat st; bzero (& st,sizeof (st)); stat (argv [1], & st); printf ("% s% ld\ n", argv [1], st.st_size); int fd=open (argv [1], O_RDWR); ftruncate (fd,20); bzero (& st,sizeof (st)); stat (argv [1], & st) Printf ("% s% ld\ n", argv [1], st.st_size); close (fd); return 0;}

Summary

The above is the method of changing the file size of Linux introduced by the editor. I hope it will be helpful to you. If you have any questions, please leave a message for me, and the editor will reply you in time. Thank you very much for your support to the website!

Tags: Size files parameters functions methods success prototypes patterns questions websites sections help support Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Technology MySQL Docker Shulou Tech Info MariaDB