1. 程式人生 > >Linux伺服器程式設計之:truncate()函式+案例說明

Linux伺服器程式設計之:truncate()函式+案例說明

1.依賴標頭檔案

#include<unistd.h>

#include<sys/types.h>

2.函式定義:

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

int ftruncate(int fd,off_t length);

函式說明:

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.

       翻譯:truncate()和ftruncate()函式導致一個名稱為path或者被檔案描述符fd引用的常規檔案被截斷成一個大小精為length位元組的檔案。

If  the  file  previously  was larger 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').

       翻譯:如果先前的檔案大於這個大小,額外的資料丟失。如果先前的檔案小於當前定義的大小,那麼,這個檔案將會被擴充套件,擴充套件的部分將補以null,也就是‘\0’

If  the  size  changed,  then the st_ctime and st_mtime fields (respec‐
       tively, 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.

       翻譯:如果大小發生變化,那麼這個st_ctime(訪問時間)和st_mtime()修改時間將會被更新。

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

      翻譯:使用ftruncate(),這個檔案必須被開啟用以寫操作。使用truncate函式的檔案必須能夠被寫

3.案例說明:

 



相關推薦

Linux伺服器程式設計truncate函式+案例說明

1.依賴標頭檔案 #include<unistd.h> #include<sys/types.h> 2.函式定義: int truncate(const char *path,off_t length); int ftruncate(int fd,o

Linux伺服器程式設計link()函式,ln命令,symlink,readlink,案例說明

1 link()依賴標頭檔案 #include<unistd.h> 2函式定義 int link(const char *oldpath,const char *newpath); 函式說明:  link()  creates  a  new link (als

linux系統程式設計基礎必備檔案描述符file descriptor與inode的相關知識

       每個程序在Linux核心中都有一個task_struct結構體來維護程序相關的 資訊,稱為程序描述符(Process Descriptor),而在作業系統理論中稱為程序控制塊 (PCB,Process Control Block)。task_struct中有一

Linux 程序通訊管道 Pipe

一、簡介 管道(pipe) 是一種最基本的 IPC(Inter Process Communication) 機制,優點是簡單。 二、特點: 管道只適用於 存在血緣關係 的兩個程序之間通訊,因為只有存在血緣關係的兩個程序之間才能共享檔案描述符 管道分為兩端,一端

Linux 核心程式設計檔案系統

1.為了方便查詢,VFS引入了 目錄 項,每個dentry代表路徑中的一個特定部分。目錄項也可包括安裝點。 2.目錄項物件由dentry結構體表示 ,定義在檔案linux/dcache.h 標頭檔案中。   89struct dentry {  90        atomic_t d_count;     

linux網路程式設計-----多播組播程式設計

linux網路程式設計之—–多播(組播)程式設計 轉載:https://blog.csdn.net/jmq_0000/article/details/7095727 什麼是多播 ? 單播用於兩個主機之間的端對端通訊,廣播用於一個主

8Linux伺服器程式設計chdir()函式和cd命令,getcwd()函式和pwd

 1chdir依賴的標頭檔案 #include<unistd.h> 2函式定義 int chdir(const char *path); int fchdir(int fd)

Linux下程序相關fork,wait,exec()

1採用命令列操作時,所建立程序的pid編號、程序執行、撤銷過程; 為實現此部分要求,我們編寫一小段程式。它的設計想法是,接收使用者的輸入,直到得到我們需要的輸入,才退出。當我們完成程式程式碼編寫,併成功編譯,執行這段可執行程式時,就建立了一個程序。程序建立後,可以通過ps命

Pythonrange函式

range() 函式 range(stop) 用來生成0~stop區間內的整數,直到stop為 止(不包含stop) range(start, stop[,step]) 用來生成start~stop區 間內的整數,直到stop為止(不包含s

C語言學習筆記printf函式詳解

C語言中有關printf()函式的詳細使用方法: 修飾符: - digit(s) :欄位寬度的最小值。如果該欄位不能容納要列印的數或者字串,系統就會使更寬的欄位。 如%4d。 - .digit(s):精度,將結果保留到小數點後的多少位。 - h: 和整數轉

C++STL庫unique函式

作用:去除相鄰元素中重複的數(實際是移動到後面) 原理:對一個有序陣列或容器,不停的從末尾將一個元素送到開頭,(重複的元素只送一個),送出的元素覆蓋原來在此位置的元素。 #include <iostream> #include <algorithm> using na

Linux 程序間通訊方式 pipe函式

Linux 程序間通訊方式有以下幾種: 1-》管道(pipe)和有名管道(fifo). 2-》訊息佇列 3-》共享記憶體 4-》訊號量 5-》訊號(signal) 6-》套接字(sicket) 在這裡我們看一下第一種====管道(pipe)。有名管道(fifo)見其它文章。

Python3學習筆記input函式的返回值

Python3中內建input()函式,鍵入數字時,將返回int,還是str? 例如: >>> number = input() 6 >>> type(number) <class 'str'> >>&g

Linux網路程式設計高階併發伺服器

1. 介紹 在上一節,我們介紹了Linux簡單的併發伺服器,通過在伺服器端建立多個子程序,來接收客戶端的請求,實現併發處理,但這種方式明顯有缺陷,伺服器並不知道客戶端請求的數量,所以事先建立的程序數不好確定。所以,這裡介紹三種高階併發伺服器模式。第一種是伺服器端統一

linux系統程式設計程序守護程序詳解及建立,daemon()使用

一,守護程序概述 Linux Daemon(守護程序)是執行在後臺的一種特殊程序。它獨立於控制終端並且週期性地執行某種任務或等待處理某些發生的事件。它不需要使用者輸入就能執行而且提供某種服務,不是對整個系統就是對某個使用者程式提供服務。Linux系統的大多數伺服器就是通過守護程序實現的。常見的守護程序包括系

linux系統程式設計程序程序退出exit,_exit區別即atexit函式

一,程序終止有5種方式: 正常退出: 從main函式返回 呼叫exit 呼叫_exit 異常退出: 呼叫abort 由訊號終止 二,exit和_exit區別: 關於_exit():        #include <unistd.h>     

linux網路程式設計TCP/IP基礎利用ARP和ICMP協議解釋ping命令

一、MTU 乙太網和IEEE 802.3對資料幀的長度都有限制,其最大值分別是1500和1492位元組,將這個限制稱作最大傳輸單元(MTU,Maximum Transmission Unit)。如果I

linux系統程式設計訊號訊號的阻塞與未決

/*************************************************************************     > File Name: process_.c     > Author: Simba     > Mail: [email 

linux網路程式設計socket十六通過UNIX域套接字傳遞描述符和 sendmsg/recvmsg 函式

void send_fd(int sock_fd, int send_fd) {     int ret;     struct msghdr msg;     struct cmsghdr *p_cmsg;     struct iovec vec;     char cmsgbuf[CMSG_SPACE(

linux網路程式設計posix 執行緒posix 條件變數與互斥鎖 示例生產者--消費者問題

#include <unistd.h>#include <sys/types.h>#include <pthread.h>#include <semaphore.h>#include <stdlib.h>#include <stdio.h>