Ftruncate (change file size)

Source: Internet
Author: User
Tags define function rewind tmp file truncated

ftruncate(change file size) define function int ftruncate(int fd,off_t length); function description ftruncate() changes the file size specified by the 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, then the excess will be deleted. The return value executes successfully, returning 0, failing back to 1, and the cause of the error is stored in errno. Error code EBADF parameter FD file description Word is invalid or the file is closed. EINVAL parameter FD is a socket not a file, or the file is not opened in write mode.

Today with ftruncate truncation file, but how can not achieve the expected effect, truncated after the content of the file is miscellaneous, and the file size is also kept original.

Add Fflush () and rewind () after OK.

Here is the test code:

http://blog.csdn.net/dengzhaoqun/article/details/7962704

[CPP]View Plaincopy
  1. #include <stdio.h>
  2. #include <sys/types.h>
  3. #include <unistd.h>
  4. int main ()
  5. {
  6. FILE *FP;
  7. char *file = "tmp";
  8. int i;
  9. int fd;
  10. fp = fopen (file, "W");
  11. if (fp = = NULL)
  12. {
  13. printf ("fopen failed\n");
  14. return-1;
  15. }
  16. For (i=0; i<1000; i++)
  17. {
  18. fprintf (FP, "%d--ABCEDFG \ n", i);
  19. }
  20. Fflush (FP);
  21. FD = Fileno (FP);
  22. if (ftruncate (FD, 0) <0)
  23. {
  24. Perror ("");
  25. return-1;
  26. }
  27. Rewind (FP);
  28. fprintf (FP, "end\n");
  29. Fclose (FP);
  30. return 0;
  31. }

After the program runs, the contents of the TMP file are end and the size is 4 bytes.

- - - - - - - - - -

It is also OK to use rewind () before calling Ftruncate ().

But with ftruncate () truncated files, in the use of fread, fwrite copy to another file, there will be garbled and some ' \ s ' characters. Switching to fgets and fputs is normal.

Ftruncate (change file size)

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.