Linux-Function-truncate;

#include <unistd.h>

#include <sys/types>

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

int ftruncate(int fd,off_t length);

The truncate and ftruncate function cause the regular file named by path or referenced by fd to be truncated to a size of precisely

length bytes.

/*truncate 和 ftruncate函数由参数path命名或者由参数fd指向的有规律的文件截取到参数length指定大小的字节*/

If the file previously was larger then 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_time fields for the file are update ,and the set-usr-ID and set-group-ID permission bits may

be cleared.

/*如果大小改变,文件的st_ctime和st_time成员将更新,set-usr-ID和set-group-ID位可能被清除*/

On success zero is returned , On error -1 is returned and errno is set appropriately.

/*成功返回0。失败返回-1并且设置适当的错误号errno*/

With ftruncate(), the file must be open for writing; with truncate. the file must be writable.

/*要可写,文件的open模式必须包含写标记*/

Simple example:

int main()

{

  struct stat st;

  int fd = open("b.txt",O_CREAT|O_RDWR);

  //或者O_WRONLY,此时新文件大小为0

  ftruncate(fd,10);

  //将文件扩展为十个字节

  /*此处可以添加写入代码*/

  fstat(fd,&st);

  printf("Size of file is %d", st.st_size);

  close(fd);

  truncate("b.txt",5);

  //将文件截取为5个字节大小,多余的5个字节将被舍弃

  stat("b.txt",&st);

  printf("Size of file is %d", st.st_size);

  return 0;

}

时间: 2024-08-05 10:55:32

Linux-Function-truncate;的相关文章

linux function

#!/bin/bash function sayHello() { #这里的{ 和它前面的)之间可以没有空格 echo "Hello $1" } sayHello 'Neeky'

ftrace: tracing linux function calls

First confirm that you have root privilege. Second check the config of kernel that you have the configs on: CONFIG_FTRACE=y CONFIG_FUNCTION_GRAPH_TRACER=y CONFIG_EVENT_TRACING=y CONFIG_FUNCTION_TRACER CONFIG_STACK_TRACER CONFIG_DYNAMIC_FTRACE Last yo

Truncate a string

截断字符串 (用瑞兹来截断对面的退路) 如果字符串的长度比指定的参数num长,则把多余的部分用...来表示. 切记,插入到字符串尾部的三个点号也会计入字符串的长度. 但是,如果指定的参数num小于或等于3,则添加的三个点号不会计入字符串的长度. 当你完成不了挑战的时候,记得开大招'Read-Search-Ask'. 这是一些对你有帮助的资源: String.slice() function truncate(str, num) { // 请把你的代码写在这里 if(num>=str.length

Project Ruler 算法练习之 Truncate Prime

问题描述: The number 3797 has an interesting property. Being prime itself, it is possible to continuously remove digits from left to right, and remain prime at each stage: 3797, 797, 97, and 7. Similarly we can work from right to left: 3797, 379, 37, and

最佳vim技巧

最佳vim技巧----------------------------------------# 信息来源----------------------------------------www.vim.org         : 官方站点comp.editors        : 新闻组http://www.newriders.com/books/opl/ebooks/0735710015.html : Vim书籍http://vimdoc.sourceforge.net/cgi-bin/vim

一个php多态性的小例子

多态性在 OO 中指 "语言具有以不同方式处理不同类型对象的能力",但 PHP 是弱类型语言,在这一点上就比较弱,仅有 instance of 可以用于判断对象的类型 多态性的优点:让代码更接近生活中的真实情况 一下是一个非常简单的多态性例子,描述在电脑上安装不同操作系统,linux, OS X, windows 和 computer 是两种不同类型的对象. interface os{ function name(); function creator(); } class linux

JavaScript数组实战小练习

1.找出元素在数组中的位置. 1 function indexOf(arr, item) { 2 return arr.indexOf(item); 3 } 4 console.log("3在数组[1,2,3,4]中的位置:"+ indexOf([1,2,3,4],3)); 2.计算数组中元素的和. 1 function sum(arr) { 2 var all = 0; 3 for(var i = 0; i < arr.length; i++){ 4 if(typeof arr

FCC的javascript初级算法题解答

FCC上的javascript基础算法题 前一阵子做的基础算法题,感觉做完后收获还蛮大的,现在将自己的做法总结出来,供大家参考讨论.基本上做到尽量简短有效,但有些算法还可以继续简化,比如第七题若采用正则表达式来匹配,则一行代码就可以完成需求.欢迎大家提出不同解法.末尾有FCC的链接,感兴趣的同学可以去做一做. 1.翻转字符串 function reverseString(str) { var arr=str.split(""); str=arr.reverse().join("

DELPHI中的多线程【深入VCL源码】

线程的基础知识 线程的组成.线程有两部分组成. 1.一个是线程的内核对象,操作系统用它来对线程实施管理.内核对象也是系统用来存放线程统计信息的地方. 2.另一个是线程堆栈,它用于维护线程在执行代码时需要的所有函数参数和局部变量. 进程从来不执行任何东西,它只是线程的容器.线程总是在某个进程环境中创建的,而且它的整个寿命期都在该进程中.这意味着线程在它的进程地址空间中执行代码,并且在进程的地址空间中对数据进行操作.因此,如果在单进程环境中,你有两个或多个线程正在运行,那么这两个线程将共享单个地址空

[编程题]删除数组最后一个元素

删除数组 arr 最后一个元素.不要直接修改数组 arr,结果返回新的数组 输入例子: truncate([1, 2, 3, 4]) 输出例子: [1, 2, 3] //利用slice function truncate(arr) { return arr.slice(0,-1); } //利用filter function truncate(arr) { return arr.filter(function(v,i,ar) { return i!==arr.length-1; }); } //