文件读写和文件指针的移动
生活随笔
收集整理的這篇文章主要介紹了
文件读写和文件指针的移动
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
read 函數(shù)
-#include <unistd.h>
-ssize_t read(int fd, void *buf, size_t count);
從fd 所指的文件中讀取count 個(gè)字節(jié)到buf 中。返回實(shí)際讀取到的字節(jié)數(shù),有錯(cuò)誤發(fā)生則返回-1。讀取文件時(shí),文件讀寫(xiě)指針會(huì)會(huì)隨著讀取到的字節(jié)數(shù)移動(dòng)。
write 函數(shù)
- #include <unistd.h>
- ssize_t write(int fd, const void *buf, size_t count);
把buf中的count個(gè)字節(jié)寫(xiě)入fd 所指的文件中, 返回實(shí)際寫(xiě)入的字節(jié)數(shù),有錯(cuò)誤發(fā)生則返回-1。寫(xiě)入文件時(shí),文件讀寫(xiě)指針會(huì)隨著寫(xiě)入的字節(jié)數(shù)移動(dòng)。
lseek 函數(shù):控制文件指針的位置
-#include <sys/types.h>
-#include <unistd.h>
-off_t lseek(int fd, off_t offset, int whence);
offset 根據(jù)whence 來(lái)移動(dòng)文件指針的位移數(shù)
whence 取值 :
| 取值 | 含義 |
|---|---|
| SEEK_SET | 從文件開(kāi)始處向后移動(dòng) offset |
| SEEK_CUR | 從文件指針當(dāng)前位置處向后移動(dòng) offset,負(fù)數(shù)時(shí)向前移動(dòng)offset |
| SEEK_END | 從文件結(jié)尾處向后移動(dòng) offset,負(fù)數(shù)時(shí)向前移動(dòng)offset |
成功返回當(dāng)前的讀寫(xiě)位置,也就是距離文件開(kāi)始處多少個(gè)字節(jié),錯(cuò)誤返回-1
常用操作:
| 用法 | 含義 |
|---|---|
| lseek( int fd , 0,SEEK_SET) | 移動(dòng)到文件開(kāi)頭 |
| lseek(int fd, 0, SEEK_END) | 移動(dòng)到文件結(jié)尾i |
| lseek(int fd, 0, SEEK_CUR) | 獲取當(dāng)前位置(相對(duì)于文件開(kāi)頭的偏移量) |
實(shí)例:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<fcntl.h>
#include<unistd.h>
#include<errno.h>
void my_err(const char *err_string ,int line )
{
fprintf(stderr,"line:%d " ,line) ; //fprintf()函數(shù)根據(jù)指定的格式(format)向輸出流(stream)寫(xiě)入數(shù)據(jù),把后面的寫(xiě)到前面
perror(err_string) ;//先輸出err_string ,再輸出錯(cuò)誤原因
exit(1) ;
}
int my_read(int fd) //讀數(shù)據(jù)函數(shù)
{
int len ;
int ret ;
int i ;
char read_buf[64] ;
if((lseek( fd , 0 ,SEEK_END))== -1) //移動(dòng)文件指針到結(jié)尾
my_err("lseek",__LINE__) ; //__LINE__ 預(yù)編譯器內(nèi)置宏,表示行數(shù)
if((len=lseek(fd,0,SEEK_CUR)) == -1 ) //求相對(duì)于文件開(kāi)頭的偏移量,用于表明文件開(kāi)始處到文件當(dāng)前位置的字節(jié)數(shù) len
my_err("lseek",__LINE__) ;
if((lseek(fd,0,SEEK_SET)) == -1 ) //移動(dòng)文件指針到開(kāi)頭
my_err("lseek",__LINE__) ;
printf(" 字節(jié)數(shù)是 : %d
",len) ;
if((ret = read(fd,read_buf,len)) < 0) //成功時(shí)返回實(shí)際讀到的字節(jié)數(shù),失敗返回 -1
my_err("read",__LINE__ ) ;
for(i= 0 ;i< len ;i++)
printf("%c",read_buf[i]) ;
printf("
") ;
return ret ;
}
int main(void)
{
int fd ;
char write_buf[32]="Hello Word !!" ;
if((fd =open("example_63.c",O_RDWR | O_CREAT |O_TRUNC ,S_IRWXU))== -1) //O_RDWR 可讀寫(xiě) O_CREAT 創(chuàng)建 O_TRUNC 文件清空
my_err("open",__LINE__) ;
else{
printf("Creat file success !!
") ;
}
if(write(fd,write_buf,strlen(write_buf)) != strlen(write_buf) ) //寫(xiě)入文件,write 的返回值是實(shí)際寫(xiě)入的字節(jié)數(shù)
my_err("write",__LINE__) ;
my_read(fd) ; //讀出數(shù)據(jù)
printf("/*--------------------------------------------*/
") ;
if(lseek(fd,10,SEEK_END)== -1) //從文件結(jié)尾處向后移動(dòng)10位
my_err("lseek",__LINE__) ; //_LINE_ 預(yù)編譯器內(nèi)置宏,表示行數(shù)
if(write(fd,write_buf,strlen(write_buf)) != strlen(write_buf) ) //寫(xiě)入文件,write 的返回值是實(shí)際寫(xiě)入的字節(jié)數(shù)
my_err("write",__LINE__) ;
my_read(fd) ;
close(fd) ; //關(guān)閉文件
return 0;
}
lseek 允許文件指針移到EOF之后,會(huì)以 填充,但不會(huì)改變文件大小。
預(yù)編譯器內(nèi)置宏有:
__LINE__ 行數(shù)
__TIME__ 時(shí)間
__FUNCTION__ 函數(shù)
__FINE__ 文件名
下一篇介紹關(guān)于文件鎖的那些事.
總結(jié)
以上是生活随笔為你收集整理的文件读写和文件指针的移动的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: C#磁盘遍历——递归
- 下一篇: 射击的乐趣:WIN32诠释打飞机游戏