免费视频淫片aa毛片_日韩高清在线亚洲专区vr_日韩大片免费观看视频播放_亚洲欧美国产精品完整版

打開APP
userphoto
未登錄

開通VIP,暢享免費電子書等14項超值服

開通VIP
read,write和lseek函數(shù)
函數(shù)作用:
read函數(shù)是用于從指定文件描述符中讀出數(shù)據(jù)。當(dāng)從終端設(shè)備文件中讀出數(shù)據(jù)時,通常一次最多讀一行。
write函數(shù)是用于向打開的文件寫數(shù)據(jù),寫操作從當(dāng)前位移量開始。若磁盤寫滿或者超出該文件長度,則write返回失敗。
lseek函數(shù)是用于在指定的文件描述符中將指針定位到相應(yīng)的位置。

函數(shù)格式:
1.read函數(shù)
所需頭文件:#include<unistd.h> /*是linux/unix的系統(tǒng)調(diào)用,包含了許多UNIX系統(tǒng)服務(wù)的 函數(shù)原型。*/

函數(shù)原型: ssize_t read(int fd, void *buf, size_t count)

fd: 文件描述符
buf指定存儲器讀出數(shù)據(jù)的緩存區(qū)
count指定讀出的字節(jié)數(shù)。

返回值:成功:讀到的字節(jié)數(shù)。 
             0:已到文件尾。
           -1:失敗。
注意:在讀普通文件時,若在要讀相應(yīng)的字節(jié)數(shù)之前就已經(jīng)達到了文件尾,則返回的字節(jié)數(shù)小于count值。

2.write函數(shù)
所需頭文件:#include<unistd.h>

函數(shù)原型:ssize_t write(int fd, void *buf, size_t count)

fd:文件描述符
buf:指定存儲器寫入數(shù)據(jù)的緩沖區(qū)
count:需要寫入字節(jié)數(shù)。

返回值:成功:已寫字節(jié)數(shù)
          失?。?1

3.lseek函數(shù)
所需頭文件:#include<unistd.h>
                    #include<sys/types.h> /*定義了off_t,pid_t等類型*/

函數(shù)原型: off_t lseek(int fd,,off_t offset,int whence)

fd:文件描述符
offset:偏移量,該值可正可負,正值為向后移,負值為向前移

whence:有三個參數(shù)。
    (1).SEEK_SET: 當(dāng)前位置為文件的開頭,新位置為偏移量大小
    (2).SEEK_CUR: 當(dāng)前位置為文件指針位置,新位置為當(dāng)前位置加上偏移量大小
    (3).SEEK_END: 當(dāng)前位置為文件結(jié)尾,新位置為偏移量大小

返回值成功:文件當(dāng)前位移量
        失敗:-1


舉例:
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#define MAXSIZE
int main(void)
{
    int i,fd,size,len;
    char *buf="Hello! I'm writing to this file!";
    char buf_r[10];
    len = strlen(buf);
    buf_r[10] = '\0';
    if((fd = open("/tmp/hello.c", O_CREAT | O_TRUNC | O_RDWR,0666 ))<0)/*打開文件*/
    {
        perror("open:");
        exit(1);
    }
    else
        printf("open file:hello.c %d\n",fd);

    if((size = write( fd, buf, len)) < 0)/*寫入數(shù)據(jù)*/
    {
        perror("write:");
        exit(1);
    }
    else
        printf("Write:%s\n",buf);

    lseek( fd, 0, SEEK_SET ); /*將指針定為文件開頭,偏移量為0*/
    if((size = read( fd, buf_r, 10))<0)/*讀入剛才所寫數(shù)據(jù)*/
   {
        perror("read:");
        exit(1);
    }
    else
        printf("read form file:%s\n",buf_r);
    if( close(fd) < 0 )/*關(guān)閉文件*/
    {
        perror("close:");
        exit(1);
    }
    else
        printf("Close hello.c\n");
    exit(0);
}
本站僅提供存儲服務(wù),所有內(nèi)容均由用戶發(fā)布,如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點擊舉報。
打開APP,閱讀全文并永久保存 查看更多類似文章
猜你喜歡
類似文章
linux 下文件I/O編程 (open、read、write、lseek、close)
Linux文件描述符
Linux系統(tǒng)調(diào)用(一)—文件讀寫操作
UNIX環(huán)境編程學(xué)習(xí)筆記(4)
文件io函數(shù)
文件IO-文件描述符與lseek
更多類似文章 >>
生活服務(wù)
分享 收藏 導(dǎo)長圖 關(guān)注 下載文章
綁定賬號成功
后續(xù)可登錄賬號暢享VIP特權(quán)!
如果VIP功能使用有故障,
可點擊這里聯(lián)系客服!

聯(lián)系客服