文章目录
一、什么是文件编程?
在windows中,我们如何编写一个文件呢?
步骤:打开/新建文档---->编辑文档---->保存文档---->关闭文档。
那在Linux中如何编写一个文件呢?
步骤:打开(open)---->读写(read/write)---->关闭(close)
二、常用文件函数
1.文件打开/创建:open()函数
- 查看open函数 man 2 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 可读可写打开
当我们附带了权限后,打开的文件就只能按照这种权限来操作
以上这三个常数中应当只指定一 个。下列常数是可选择的:
O_CREAT :若文件不存在则创建它。使用此选项时,需要同时说明第三个参数mode,用其说明该新文件的存取许可权限。
O_EXCL :如果同时指定了OCREAT,而文件已经存在,则出错。
O_APPEND :每次写时都加到文件的尾端。
O_TRUNC :去打开文件时,如果这个文件中本来是有内容的,而且为只读或只写成功打开,则将其长度截短为0(把原有内容清空)
- 文件权限:
- 可读 r 4
- 可写 w 2
- 执行 x 1
示例代码:
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
int main(){
int fd;
fd = open("./file1",O_RDWR);//打开当前程序路径“file1”文件,可读可写方式打开
if(fd == -1){//文件打开成功返回>1的值,打开失败返回-1
printf("Open file1 failed\n");
fd = open("./file1",O_RDWR|O_CREAT,0600);//可读可写:4+2
if(fd > 0){
printf("creat file1 success\n");
}
}
return 0;
}
举例: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 指新创建文件的权限,可读可写
例如:0600
给文件所有者 .cl
0 6 0 0
4+2 同组 其他组
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("./cl.txt",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;
}
结果如下: 注 hello world是之前在cl.txt文件中写好的。
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("./cl.txt",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;
}
结果如下:注 原来的文件中还有"hello world"字符串,执行后都给清零了,然后重新写入的新文件
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_IRUSR | 4 | 可读 |
S_IWUSR | 2 | 可写 |
S_IXUSR | 1 | 可执行 |
S_IRWXU | 7 | 可读、写、执行 |
示例代码:
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
int main(){
int fd;
fd = creat("/home/CL/FILE/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 要写入文件的大小
write功能可以理解为将缓冲区buf的指针指向内存里面的数据,写“count”这么多个字节到fp(刚刚打开的文件)里,写入成功返回一个写入个数的整型数,写入失败返回-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("./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");
}
}
write(fd,buf,strlen(buf));
close(fd);
return 0;
}
#strlen()函数 指计算字符串buf的长度的函数
#close()函数 用于关闭一个已经打开的文件
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;
}
三、面试常问sizeof与strlen的区别
(1)sizeof是一个操作符,而strlen是库函数。
(2)sizeof的参数可以是数据的类型,也可以是变量,而strlen只能以结尾为’\0’的字符串作参数
(3)编译器在编译时就计算出了sizeof的结果,而strlen必须在运行时才能计算出来。
(4)sizeof计算数据类型占内存的大小(计算指针的长度),strlen计算字符串实际长度。
(5)sizeof 为数组时候,不退化, 传递给strlen时数组会被退化橙指针;
使用小技巧:遇见要分配内存的用sizeof,遇见要输出字符串长度的用strlen。
举例:
分配内存:readBuf = (char *)malloc(sizeof(char) * size);
计算字符串长度:int n_write = write(fdDes,readBuf,strlen(readBuf));