Linux c语言操作入门之文件编程
一、前言
使用Linux进行研发的基础流程如下:
这里记录一下Linux入门使用C语言进行文件编程的一些API使用。包括:
- 打开文件
- 创建文件
- 写入文件
- 读取文件
- 光标设置
- 关闭文件
二、查看API帮助文档
命令行中输入: man 2 name of API 可以查看相关API的具体信息
1、open 打开文件
open 函数语法要点:
所需头文件:
#include<sys/types.h>// 提供类型pid_t 的定义
#include<sys/stat.h>
#include<fcntl.h>
函数原型:int open(const char *pathname,flags,int perms)
函数传入值:
path :被打开文件名(可包括路径名)
flag :文件打开的方式,参数可以通过“|” 组合构成,但前3 个参数不能互相重合。
O_REONLY :只读方式打开文件
O_WRONLY :可写方式打开文件
O_RDWR :读写方式打开文件
O_CREAT :如果文件不存在时就创建一个新文件,并用第三个参数为其设置权限。
O_EXCL :如果使用O_CREAT 时文件存在,则可返回错误信息。这一参数可测试文件是否存在。
O_NOCTTY :使用本参数时,如文件为终端,那么终端不可以作为调用open ()系统调用的那个进程的控制终端。
O_TRUNC :如文件已经存在,并且以只读或只写成功打开,那么会先全部删除文件中原因数据。
O+APPEND :以添加方式打开文件,在打开文件的同时,文件指针指向文件末尾。
perms :被打开文件的存取权限,为8 进制表示法。
函数返回值:成功:返回文件描述符 失败:-1
使用示例
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
//打开某个文件,若打开失败,则重新创建该文件。权限为可读可写
int main()
{
int fd;
fd =open("./file1",O_RDWR);
if(fd == -1)
{
printf("open file1 failed \n");
fd =open("./file1",O_RDWR|O_CREAT,0600);
if(fd>0)
{
printf("create file1 success!!! \n");
}
}
}
2、creat 创建文件
所需头文件: #include<fcntl.h>
函数原型: int creat(const char *pathname, mode_t mode);
函数传入值:
pathname :待创建文件名(可包括路径名)
mode:与open的mode相同
函数功能:创建一个文件与下句功能相同
open(pathname,O_WRONLY | O_CREAT | O_TRUNC, mode)
2、Write 写入文件
Write 函数语法要点:
所需头文件:#include<unistd.h>
函数原型: ssize_t write(int fd,void *buf,size_t count)
函数传入值:
fd: 文件描述符
Buf :指定存储器写入数据的缓冲区
Count :指定读出的字节数
函数返回值:成功:已写的字节数 -1 :出错
使用示例
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
int main()
{
int fd;
char *buf = "ryj";
//读取文件,若读取成功则返回文件描述符 大于0的正整数
fd =open("./file1",O_RDWR);
if(fd == -1)
{
printf("open file1 failed \n");
//读取文件,若没有该文件则创建该文件!
fd =open("./file1",O_RDWR|O_CREAT,0600);
if(fd>0)
{
printf("create file1 success!!! \n");
}
}
printf("create file1 is susceess : fd=%d\n",fd);
// 写入文件
// ssize_t write(int fd, const void *buf, size_t count);
write(fd,buf,strlen(buf));
// 关闭文件
close(fd);
return 0;
}
3、read 读取文件
Read 函数语法要点:
所需头文件:#include<unistd.h>
函数原型:ssize_t read(int fd,void *buf,size_t count)
功能:从fd读count个到buf
函数传入值:
fd: 文件描述符
Buf :指定存储器读出数据的缓冲区
Count :指定读出的字节数
函数返回值:成功:读出的字节数 0 :已到达文件尾 -1 :出错
在读普通文件时,若读到要求的字节数之前已达到文件的尾部,则返回字节数会小于希望读出的字节数。
使用示例
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
int main()
{
int fd;
char *buf = "ryj";
//读取文件,若读取成功则返回文件描述符 大于0的正整数
fd =open("./file1",O_RDWR);
if(fd == -1)
{
printf("open file1 failed \n");
//读取文件,若没有该文件则创建该文件!
fd =open("./file1",O_RDWR|O_CREAT,0600);
if(fd>0)
{
printf("create file1 success!!! \n");
}
}
printf("create file1 is susceess : fd=%d\n",fd);
// 写入文件
// ssize_t write(int fd, const void *buf, size_t count);
int n_write=write(fd,buf,strlen(buf));
if(n_write !=-1)
{
printf("write %d byte to file \n",n_write);
}
// 关闭文件
close(fd);
// 重新打开文件,会使得光标重新到达文件开头
fd =open("./file1",O_RDWR);
// 开辟文件读取的空间
char *readBuf;
readBuf=(char *)malloc(sizeof(char)*n_write+1);
// ssize_t read(int fd, void *buf, size_t count);
int n_read=read(fd,readBuf,n_write);
printf("read %d,context:%s\n",n_read,readBuf);
return 0;
}
4、Lseek 光标设置
Lseek 函数语法要点:
所需头文件:
#include<unistd.h>
#include<sys/types.h>
函数原型:off_t lseek(int fd,off_t offset,int whence)
函数传入值:
fd: 文件描述符
Offset :偏移量,每一读写操作所需要移动的距离,单位是字节的数量,可正可负(向前移,向后移)
Whence :当前位置的基点:
SEEK_SET :当前位置为文件开头,新位置为偏移量的大小
SEEK_CUR :当前位置为文件指针位置,新位置为当前位置加上偏移量
SEEK_END :当前位置为文件的结尾,新位置为文件的大小加上偏移量大小
下列是特别的使用方式:
- 欲将读写位置移到文件开头时:lseek(int fildes, 0, SEEK_SET);
- 欲将读写位置移到文件尾时:lseek(int fildes, 0, SEEK_END);
- 想要取得目前文件位置时:lseek(int fildes, 0, SEEK_CUR);
返回值:当调用成功时则返回目前的读写位置, 也就是距离文件开头多少个字节. 若有错误则返回-1, errno 会存放错误代码.
使用示例
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
int main()
{
int fd;
char *buf = "ryj";
//读取文件,若读取成功则返回文件描述符 大于0的正整数
fd =open("./file1",O_RDWR);
if(fd == -1)
{
printf("open file1 failed \n");
//读取文件,若没有该文件则创建该文件!
fd =open("./file1",O_RDWR|O_CREAT,0600);
if(fd>0)
{
printf("create file1 success!!! \n");
}
}
printf("create file1 is susceess : fd=%d\n",fd);
// 写入文件
// ssize_t write(int fd, const void *buf, size_t count);
int n_write=write(fd,buf,strlen(buf));
if(n_write !=-1)
{
printf("write %d byte to file \n",n_write);
}
//移动光标
//off_t lseek(int fd, off_t offset, int whence);
lseek(fd,0,SEEK_SET);
// 开辟文件读取的空间
char *readBuf;
readBuf=(char *)malloc(sizeof(char)*n_write+1);
// ssize_t read(int fd, void *buf, size_t count);
int n_read=read(fd,readBuf,n_write);
printf("read %d,context:%s\n",n_read,readBuf);
close(fd);
return 0;
}
可以利用lseek进行文件大小的计算:
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
int main()
{
int fd;
char *buf = "ryj";
fd =open("./file1",O_RDWR);
//移动光标
//off_t lseek(int fd, off_t offset, int whence);
int off_t = lseek(fd,0,SEEK_END);
printf("file1's size is :%d",off_t);
close(fd);
return 0;
}
~
5、close 关闭文件
Close 语法要点:
所需头文件:#include<uniste.h>
函数原型:int close (int fd )
函数输入值:fd :文件描述符
函数返回值:成功:0 出错:-1
三、注意
在文件操作时,需要先打开文件,再进行读写操作,最后关闭文件