目录
在windows中如何编写一个文件呢?
步骤:打开/新建文档---->编辑文档---->保存文档---->关闭文档。
那在Linux中如何编写一个文件呢?
步骤:打开(open)---->读写(read/write)---->关闭(close)
在Linux系统中的文件编程如下
1、打开/创建文件:open()函数
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
int open(const char *pathname, int flags);
int open(const char *pathname, int flags, mode_t mode);
const char *pathname 指打开文件的路径;
int flags 指打开文件的模式;
打开文件的模式有三种,如下:
O_RDONLY(可读)
O_WRONLY(可写)
O_RDWR(可读可写)
open的返回值是一个文件描述符,是一个非负整数
什么是文件描述符:对于内核而言,所有打开文件都由文件描述符引用。文件描述符是一个非负整数。当打开一个现存文件或者创建一个新文件时,内核向进程返回一个文件描述符
open函数打开文件,打开成功返回一个文件描述符,打开失败,则返回-1
下面用代码举例
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
int main(){
int fd;//定义一个文件描述符
fd = open("./file1",O_RDWR);//打开一个文件,然后返回一个文件描述符
printf("fd = %d\n",fd);//打印文件描述符,要是本地中有file1文件,则返回3,否则返回-1
return 0;
}
若本地文件中有file1文件时,文件描述符的返回值
若本地文件中没有file1文件时,文件描述符的返回值
在文件有三种打开模式后,还能在后面添加几种模式
O_CREAT:若文件不存在则创建它
O_EXCL:如果同时指定了OCREAT,而文件已经存在,则返回-1
O_APPEND:每次写时都加到文件的尾端
O_TRUNC:去打开文件时,如果原文件中有内容,则把原文件中的内容清零,再写入新文件
举例:
O_CREAT:若文件不存在则创建它
#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");
}
}
return 0;
}
#O_CREAT 指若文件不存在,则创建它
#0600 指新创建文件的权限,可读可写
O_EXCL :如果同时指定了OCREAT , 而文件已经存在,则返回-1
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
int main(){
int fd;
fd = open("./file1",O_RDWR|O_CREAT|O_EXCL,0600);
if(fd == -1){
printf("file Cunzai\n");
}
return 0;
}
O_APPEND:每次写时都加到文件的尾端
#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 = "this is a boy";
fd = open("./file1",O_RDWR|O_APPEND);//每次写时,都加到文件的尾端
int n_write = write(fd,buf,strlen(buf));
if(n_write != -1){
printf("write %d byte to file\n",n_write);
}
close(fd);
return 0;
}
结果如下: 注 12345678是之前在file1文件中写好的。
O_TRUNC:去打开文件时,如果原文件中有内容,则把原文件中的内容清零,再写入新文件
#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 = "this is a boy";
fd = open("./file1",O_RDWR|O_TRUNC);// 如果原文件中有内容,则把原文件中的内容清零,再写入新文件
int n_write = write(fd,buf,strlen(buf));
if(n_write != -1){
printf("write %d byte to file\n",n_write);
}
close(fd);
return 0;
}
结果如下:注 原来的文件中还有"12345678"字符串,执行后都给清零了,然后重新写入的新文件
2、创建文件:creat()函数
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
int creat(const char *pathname, mode_t mode);
const char *pathname 指要创建的文件路径(包括文件路径和文件名)
mode_t mode 指要创建文件的权限
S_IRWXU 可读可写可执行
S_IRUSR 可读
S_IWUSR 可写
S_IXUSR 可执行
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
int main(){
int fd;
fd = creat("/home/CLC/Linux1/file",S_IRWXU); //可读可写可执行
return 0;
}
执行结果如下:
3、文件写入操作:write()函数
#include <unistd.h>
ssize_t write(int fd,const void *buf,size_t count );
int fd //文件描述符
const void *buf //是一个文件缓冲区,把这个文件写入fd中
size_t count 要写入文件的大小
可以简单理解为:将缓冲区中的数据,写count个字节,写到fd中
写入成功的话,则返回写入字节的大小,没有内容写入则返回0,错误写入则返回-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 = "this is a boy";
fd = open("./file",O_RDWR);
if(fd == 1 ){
printf("open file failed\n");
fd = open("./file1",O_RDWR|O_CREAT,0600);
if(fd >0){
printf("create file1 success\n");
}
}
write(fd,buf,strlen(buf));
close(fd);
return 0;
}
#strlen()函数 指计算字符串buf的长度的函数
#close()函数 用于关闭一个已经打开的文件
写到file1文件中的结果
4、文件读取操作:read()函数
#include <unistd.h>
ssize_t read(int fd,void *buf,size_t count);
int fd; //要读取的文件
void *buf //读上来的数据保存在缓冲区buf中,同时文件的当前读写位置向后移
size_t count // 请求读取的字节数
返回值,正确读取则返回读取到内容的字节大小,没有内容则返回0,错误读取则返回-1
文件读取代码示例如下:
#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 = "this is a boy";
fd = open("./file1",O_RDWR);
if(fd == -1){
printf("open file1 fauled\n");
fd = open("./file1",O_RDWR|O_CREAT,0600);
if(fd > 0){
printf("create file1 success!\n");
}
}
int n_write = write(fd,buf,strlen(buf));
if(n_write != -1){
printf("write %d byte to file1\n",n_write);
}
close(fd); //关闭文件
open("./file1",O_RDWR);//重新打开文件
char *readBuf; //创建一个char类型的指针
readBuf = (char *)malloc(sizeof(char)* n_write);//动态分配内存空间
int n_read = read(fd,readBuf,n_write); //返回读取内容的字节个数
printf("read %d , context: %s \n",n_read ,readBuf);
close(fd);
return 0;
}
#(char *)malloc(sizeof(char)* n_write)
#sizeof(char) * n_write 指分配一个char的大小 乘以 n_write个char
#(char *) 强制转换分配的内存为char指针类型
文件读取返回结果:
5、关闭文件操作:close()函数
#include <unistd.h>
int close(int filedes);
#int filedes 指需要关闭的文件
关闭文件代码示例如下:
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
int main(){
int fd;
fd = open("./file1",O_RDWR);
printf("fd = %d\n",fd);
close(fd);//关闭文件
return 0;
}
6、sizeof与strlen有以下区别:
(1)sizeof是一个操作符,而strlen是库函数。
(2)sizeof的参数可以是数据的类型,也可以是变量,而strlen只能以结尾为'\0'的字符串作参数。
(3)编译器在编译时就计算出了sizeof的结果,而strlen必须在运行时才能计算出来。
(4)sizeof计算数据类型占内存的大小(计算指针的长度),strlen计算字符串实际长度。
使用小技巧:遇见要分配内存的用sizeof,遇见要输出字符串长度的用strlen。
举例:
分配内存:readBuf = (char *)malloc(sizeof(char) * size);
计算字符串长度:int n_write = write(fdDes,readBuf,strlen(readBuf));
以上文章作为学习参考,为了复习知识而写
原文链接:https://blog.csdn.net/l_z_y_000/article/details/128271569