linux檔案操作 學習筆跡1

2021-10-08 12:22:43 字數 4646 閱讀 3313

1、open函式需要包含的標頭檔案,以及函式的定義:

#include

#include

#include

intopen

(const

char

*pathname,

int flags)

;int

open

(const

char

*pathname,

int flags, mode_t mode)

;

pathname: 檔案路徑字串。

開啟方式(flags):

o_rdwr(可讀可寫)

o_rdonly(唯讀)

o_wronly(只寫)

前面三個使用是互斥的。

可以用『|』與o_creat使用,表示開啟檔案,如果沒有則建立。

許可權(mood)只有在建立檔案時生效

返回值:-1表示失敗。

例1:

#include

#include

#include

#include

"stdio.h"

intmain()

}return0;

}

2、write函式標頭檔案及函式定義:

#include

ssize_t write

(int fd,

const

void

*buf, size_t count)

;

引數fd為開啟的檔案流。

buf:針型別,指定的記憶體。

count:寫入的位元組數。

返回值:正常則返回位元組數,返回1則表示有許可權被禁止。

3、read函式標頭檔案及函式定義:

#include

ssize_t read

(int fd,

void

*buf, size_t count)

;

從fd中讀取到buf,讀取count個位元組。

返回值:正常返回讀到的位元組數,返回-1表示錯誤。

4、lseek數標頭檔案及函式定義:

#include

#include

off_t lseek

(int fd, off_t offset,

int whence)

;

offset:位移

whence:位置

引數型別有:seek_set檔案開頭。

seek_cur當前位置。

seek_end檔案尾部。

返回值:成功返回距離開頭多少位元組,失敗返回-1.

例2:

#include

#include

#include

#include

"stdio.h"

#include

#include

"string.h"

#include

"stdlib.h"

intmain()

}printf

("open success fd=%d"

,fd)

;int n_write =

write

(fd,buf,

strlen

(buf));

if(n_write!=1)

// close(fd);

// fd=open("./file1",o_rdwr);

char

*readbuf;

readbuf =

(char*)

malloc

(sizeof

(char

)*n_write+1)

;lseek

(fd,0,

seek_set);

int n_read=

read

(fd,readbuf,n_write)

;printf

("read %d,context:%s\n"

,n_read,readbuf)

;close

(fd)

;return0;

}

5、實現cp指令

#include

#include

#include

#include

"stdio.h"

#include

#include

"string.h"

#include

"stdlib.h"

intmain

(int argc,

char

**ar**)

fdsrc=

open

(ar**[1]

,o_rdwr)

;int size=

lseek

(fdsrc,0,

seek_end);

lseek

(fdsrc,0,

seek_set);

readbuf=

(char*)

malloc

(sizeof

(char

)*size+8)

;int n_read=

read

(fdsrc,readbuf,size)

; fdes=

open

(ar**[2]

,o_rdwr|o_trunc|o_creat,

0600);

//清除原本內容

int n_write =

write

(fdes,readbuf,

strlen

(readbuf));

close

(fdsrc)

;close

(fdes)

;return0;

}

1、修改檔案

#include

#include

#include

#include

"stdio.h"

#include

#include

"string.h"

#include

"stdlib.h"

intmain

(int argc,

char

**ar**)

fdsrc=

open

(ar**[1]

,o_rdwr)

;int size=

lseek

(fdsrc,0,

seek_end);

lseek

(fdsrc,0,

seek_set);

readbuf=

(char*)

malloc

(sizeof

(char

)*size+8)

;int n_read=

read

(fdsrc,readbuf,size)

;char

*p=strstr

(readbuf,

"leng=");

//返回找到串開頭的位置

if(p==

null

) p=p+

strlen

("leng=");

*p='5'

;lseek

(fdsrc,0,

seek_set);

int n_write =

write

(fdsrc,readbuf,

strlen

(readbuf));

close

(fdsrc)

;return0;

}

2、寫入乙個結構體

#include

#include

#include

#include

"stdio.h"

#include

#include

"string.h"

#include

"stdlib.h"

struct test

;int

main()

;struct test data2;

fd=open

("./file1"

,o_rdwr)

;int n_write =

write

(fd,

&data1,

sizeof

(struct test));

lseek

(fd,0,

seek_set);

int n_read=

read

(fd,

&data2,

sizeof

(struct test));

printf

("read %d,%c"

,data2.a,data2.c)

;close

(fd)

;return0;

}

linux庫檔案學習1

1.概述 庫檔案,本質上是可執行 的二進位制形式,可被作業系統載入記憶體執行 我們可以理解為是一種儲存了一定函式的資料夾可以供我們呼叫 如我們編寫c語言程式時 include的stdio即是庫檔案介面 即告訴我們庫檔案的用法 它包含了輸入輸出函式 庫檔案有系統自帶的,也可以我們自行製作 特別注意li...

Linux學習 管理檔案(1)

重要目錄 位置用途 usr 安裝的軟體 共享庫,包括檔案和唯讀程式資料。重要的子目錄有 usr bin 使用者命令 usr sbin 系統管理命令 usr local 本地自定義軟體 etc 特定於此系統的配置檔案 var 特定於此系統的可變資料,在系統啟動之間保持永久性。動態變化的檔案 如資料庫 ...

linux下的檔案操作(1)

1.開啟檔案 open 返回的是檔案描述符,不帶緩衝 fopen 返回的是file 帶緩衝 根據引數不同,可以選擇不同的開啟方式,這是檔案操作的第1步 2.讀檔案 read fread 這2個函式僅僅是按位元組讀取檔案 如果要按一定的規則對檔案讀取,則需要另外的函式 fscanf 例子1 如果輸入資...