【摘要】 這篇文章介紹C語言的文件編程函數(shù),案例代碼是在Linux環(huán)境下運行測試的分別介紹了C語言標準庫的文件編程接口和Linux下的文件編程函數(shù)。
1. 文件編程函數(shù)介紹
如果在Linux系統(tǒng)下學習C語言,就會了解到兩套文件編程接口函數(shù):
C語言標準的文件編程函數(shù): fopen、fread、fwrite、fclose
Linux下提供的文件編程函數(shù): open、read、write、close
傳參的區(qū)別:
基于文件指針: fopen fclose fread fwrite 比較適合操作普通文件。
基于文件描述符: open close read write 比較適合操作設備文件。
2. C語言標準庫提供的文件編程函數(shù)
下面介紹C語言標準庫提供的文件編程函數(shù),一般對文件常用的操作就是:創(chuàng)建(打開)、讀、寫、關閉。
其他的函數(shù)用法同理,只要把這4個函數(shù)學會了,基本的文件操作已經(jīng)可以完成了。
#include
FILE *fopen(const char *path, const char *mode);
函數(shù)功能: 打開或者創(chuàng)建文件
函數(shù)參數(shù):
const char *path 填文件的路徑
const char *mode 填權(quán)限。比如: wb rb a+b
函數(shù)返回值: 文件打開或者創(chuàng)建成功返回對應的文件指針.
size_t fread(void *ptr, size_t size, size_t nmemb, FILE *stream);
函數(shù)功能: 讀文件
函數(shù)參數(shù):
void *ptr :讀取數(shù)據(jù)存放的緩沖區(qū)。
size_t size :每次讀取的大小
size_t nmemb :每次讀取的次數(shù)
FILE *stream :讀取文件
返回值: 讀取成功的次數(shù)。
比如: fread(buff,1,1000,fp); 從fp文件里讀取1000個字節(jié)的數(shù)據(jù)到buff里。
size_t fwrite(const void *ptr, size_t size, size_t nmemb,FILE *stream);
函數(shù)功能: 寫文件
函數(shù)參數(shù):
void *ptr :將要寫入的數(shù)據(jù)緩沖區(qū)首地址。
size_t size :每次寫入的大小
size_t nmemb :每次寫入的次數(shù)
FILE *stream :寫入的文件
返回值: 寫入成功的次數(shù)。
比如: fwrite(buff,1,1000,fp); 向fp文件里寫1000個字節(jié)的數(shù)據(jù),數(shù)據(jù)從buff里獲取。
int fclose(FILE *fp);
函數(shù)功能: 關閉打開的文件。
3. Linux下的系統(tǒng)函數(shù): 文件編程函數(shù)
Linux下也有一套系統(tǒng)函數(shù),用于文件操作,這些函數(shù)在Linux下常用于讀寫設備文件;當然,讀寫普通文件也是一樣,普通文件也是磁盤上的數(shù)據(jù),也是操作塊設備驅(qū)動。
#include
#include
#include
int open(const char *pathname, int flags);
int open(const char *pathname, int flags, mode_t mode);
函數(shù)功能: 打開或者創(chuàng)建文件
函數(shù)參數(shù):
const char *pathname : 文件的路徑
int flags :打開文件的權(quán)限.
比如: O_RDONLY O_WRONLY O_RDWR O_CREAT
mode_t mode :創(chuàng)建文件時指定文件本身的權(quán)限.
比如:
S_IRWXU 00700 user (file owner) has read, write and execute permission
S_IRUSR 00400 user has read permission
S_IWUSR 00200 user has write permission
S_IXUSR 00100 user has execute permission
S_IRWXG 00070 group has read, write and execute permission
S_IRGRP 00040 group has read permission
S_IWGRP 00020 group has write permission
S_IXGRP 00010 group has execute permission
S_IRWXO 00007 others have read, write and execute permission
S_IROTH 00004 others have read permission
S_IWOTH 00002 others have write permission
S_IXOTH 00001 others have execute permission
返回值: (返回文件描述符--小整數(shù))文件打開成功值>=0 否則<0
示例: open("123.c",O_RDWR|O_CREAT,S_IRWXU|S_IRWXG|S_IRWXO);
創(chuàng)建一個123.c的新文件,并且指定該文件的權(quán)限是最高權(quán)限. chmod 777 123.c一樣
ssize_t read(int fd, void *buf, size_t count);
函數(shù)功能: 讀文件
函數(shù)參數(shù):
int fd :文件描述符. ---表示已經(jīng)打開的文件。
void *buf :讀取數(shù)據(jù)存放的緩沖區(qū).
size_t count :讀取字節(jié)數(shù)量.
返回值: 成功讀取的字節(jié)數(shù)量.
ssize_t write(int fd, const void *buf, size_t count);
函數(shù)功能: 寫文件
函數(shù)參數(shù):
int fd :文件描述符. ---表示已經(jīng)打開的文件。
void *buf :將要寫入到文件的數(shù)緩沖區(qū)首地址.
size_t count :寫入的字節(jié)數(shù)量.
返回值: 成功寫入的字節(jié)數(shù)量.
int close(int fd);
函數(shù)功能: 關閉已經(jīng)打開的文件.
4. 案例: 實現(xiàn)文件拷貝功能(fopen)
例如: cp 123.c 456.c 將123.c的數(shù)據(jù)拷貝到456.c文件里。
注意: 下面這個代碼需要在命令行上運行,需要給main函數(shù)傳參。如果是在windows下IED軟件里運行,可以將傳參代碼屏蔽掉,改成讓用戶輸入,或者直接把參數(shù)固定也可以。
#include
//實現(xiàn): cp 123.c 456.c
int main(int argc,char **argv)
{
if(argc!=3)
{
printf("參數(shù): ./a.out <源文件> <目標文件>\n");
return 0;
}
/*1. 打開源文件*/
FILE *src_fp=fopen(argv[1],"rb");
if(src_fp==NULL)
{
printf("%s 文件打開失敗.\n",argv[1]);
return -1;
}
/*2. 創(chuàng)建新文件*/
FILE *new_fp=fopen(argv[2],"wb");
if(new_fp==NULL)
{
printf("%s 文件創(chuàng)建失敗.\n",argv[2]);
fclose(src_fp);
return -2;
}
/*3. 實現(xiàn)文件的拷貝*/
unsigned char buff[1024];
unsigned int cnt;
while(1)
{
cnt=fread(buff,1,1024,src_fp);
fwrite(buff,1,cnt,new_fp);
if(cnt!=1024)break;
}
/*4. 關閉文件*/
fclose(new_fp);
fclose(src_fp);
return 0;
}
5. 案例: 實現(xiàn)文件拷貝功能(open)
例如: cp 123.c 456.c 將123.c的數(shù)據(jù)拷貝到456.c文件里。
#include
#include
#include
#include
//實現(xiàn): cp 123.c 456.c
int main(int argc,char **argv)
{
if(argc!=3)
{
printf("參數(shù): ./a.out <源文件> <目標文件>\n");
return 0;
}
/*1. 打開源文件*/
int fd_src=open(argv[1],O_RDONLY);
if(fd_src<0)
{
printf("源文件打開失敗.\n");
return -1;
}
/*2. 創(chuàng)建新文件*/
int fd_new=open(argv[2],O_WRONLY|O_CREAT,S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH);
if(fd_new<0)
{
printf("新文件創(chuàng)建失敗.\n");
close(fd_src);
return -2;
}
/*3. 拷貝文件*/
unsigned char buff[1024];
int cnt;
while(1)
{
cnt=read(fd_src,buff,1024);
write(fd_new,buff,cnt);
if(cnt!=1024)break;
}
/*4. 關閉文件*/
close(fd_src);
close(fd_new);
return 0;
}
-
Linux
+關注
關注
88文章
11758瀏覽量
219006 -
C語言
+關注
關注
183文章
7644瀏覽量
145565 -
編程
+關注
關注
90文章
3716瀏覽量
97178
發(fā)布評論請先 登錄
linux下c語言編程pdf
利用C語言和GEL語言的Flash編程新方法
C語言程序例程的文件結(jié)構(gòu)
單片機c語言+編程c語言_C編程語言簡介
Linux開發(fā)_文件目錄操作介紹、創(chuàng)建BMP圖片
C語言-文件編程
評論