1、open函数创建和打开文件
Linux系统中使用原型
使用案例
//头文件
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
// O_RDONLY 只读打开 O_WRONLY 只写打开 O_RDWR 可读可写打开
// O_CREAT 若文件不存在则创建他。使用时需要说明第三个参数mode,用其说明该新文件的存取许可权限
// O_EXCL 如果同时指定O_CREAT,而文件存在 则出错
// O_APPEND 每次写时都加到文件的尾端
// O_TRUNC 属性去掉文件时,如果这个文件中本来就有内容的,而且只写或只读成功打开, 则长度截短为0
//1、可读-R 4
//2、可写-W 2
//3、执行-X 1
int main()
{
int fd;
// int open(const char *pathname, int flags);
fd = open("./file1",O_RDWR);//打开文件file1
if(fd ==-1){//判断是否存在文件
printf("open file1 faile\n");
// int open(const char *pathname, int flags, mode_t mode);//创建文件
fd = open("./file1",O_RDWR|O_CREAT,0600);//0600=可读—R 4 + 可写-W 2
if(fd>0){
printf("fd = %d\n",fd);
printf("creat success \n");
}
}
return 0;
}
运行结果
cuichao@ubuntu:~/c语言$ ./a.out
open file1 faile
fd = 3
creat success
2、write
Linux系统中使用原型
使用案例
//头文件
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
// O_RDONLY 只读打开 O_WRONLY 只写打开 O_RDWR 可读可写打开
// O_CREAT 若文件不存在则创建他。使用时需要说明第三个参数mode,用其说明该新文件的存取许可权限
// O_EXCL 如果同时指定O_CREAT,而文件存在 则出错
// O_APPEND 每次写时都加到文件的尾端
// O_TRUNC 属性去掉文件时,如果这个文件中本来就有内容的,而且只写或只读成功打开, 则长度截短为0
//1、可读-R 4
//2、可写-W 2
//3、执行-X 1
int main()
{
int fd;
char *buf = "hello world";
// int open(const char *pathname, int flags);
fd = open("./file1",O_RDWR);//打开文件file1
if(fd ==-1){//判断是否存在文件
printf("open file1 faile\n");
// int open(const char *pathname, int flags, mode_t mode);//创建文件
fd = open("./file1",O_RDWR|O_CREAT,0600);//0600=可读—R 4 + 可写-W 2
if(fd>0){
printf("creat success \n");
}
}
printf("open success :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
Linux系统中使用原型
//头文件
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
// O_RDONLY 只读打开 O_WRONLY 只写打开 O_RDWR 可读可写打开
// O_CREAT 若文件不存在则创建他。使用时需要说明第三个参数mode,用其说明该新文件的存取许可权限
// O_EXCL 如果同时指定O_CREAT,而文件存在 则出错
// O_APPEND 每次写时都加到文件的尾端
// O_TRUNC 属性去掉文件时,如果这个文件中本来就有内容的,而且只写或只读成功打开, 则长度截短为0
//1、可读-R 4
//2、可写-W 2
//3、执行-X 1
int main()
{
int fd;
char *buf = "hello world";
// int open(const char *pathname, int flags);
fd = open("./file1",O_RDWR);//打开文件file1
if(fd ==-1){//判断是否存在文件
printf("open file1 faile\n");
// int open(const char *pathname, int flags, mode_t mode);//创建文件
fd = open("./file1",O_RDWR|O_CREAT,0600);//0600=可读—R 4 + 可写-W 2
if(fd>0){
printf("creat success \n");
}
}
printf("open success :fd = %d\n",fd);
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);
close(fd);
return 0;
}
4、sleek
函数原型
#include <sys/types.h>
#include <unistd.h>
off_t lseek(int fd, off_t offset, int whence);
1、移动光标使用案例
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
/* SEEK_SET
The file offset is set to offset bytes.光标移动到文件头
SEEK_CUR
The file offset is set to its current location plus offset bytes.
SEEK_END
The file offset is set to the size of the file plus offset bytes.光标移动到文件尾
*/
int main()
{
int fd;
char *buf = "hello world";
fd = open("./file1",O_RDWR);
if(fd ==-1){//判断文件是否存在
printf("open file1 faile\n");
fd = open("./file1",O_RDWR|O_CREAT,0600);//创建文件
if(fd>0){
printf("creat success \n");
}
}
printf("open success :fd = %d\n",fd);
int n_write = write(fd,buf,strlen(buf));//字符串写入文件
if(n_write !=-1){//判断是否写入成功
printf("write %d byte to file\n",n_write);
}
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);
return 0;
}
2、利用lseek计算字符长度
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
int main()
{
int fd;
fd = open("./file1",O_RDWR);//打开文件可读可写
int filesize = lseek(fd,0,SEEK_END);//光标从头偏移到尾,计算便宜长度
printf("file's size is %d\n",filesize);
close(fd);
return 0;
}