LINUX系统:
打开:open
读写:write/read
光标定位:lseek
关闭:close
#include <sys/types.h> //查看这三个头文件可以输入man 2 open
#include <sys/stat.h>
#include <fcntl.h>
int open(const char *pathname, int flags);
int open(const char *pathname, int flags, mode_t mode);
// Pathname:要打开的文件名(含路径,缺省为当前路径)
/* Flags:
O_RDONLY 只读打开
O_WRONLY 只写打开
O_RDWR 可读可写打开
当附带权限后,打开的文件只能按照这种权限来操作,以上三个常数应当只指定一个,以下常数可选择:
O_CREAT 若文件不存在则创建它。使用此选项时,需要同时说明第三个参数mode,用其说明新文件的存取许可权限
O_EXCL 如果同时指定O_CREAT,而文件已经存在,则返回-1
O_APPEND 每次写时都加到文件的尾端
O_TRUNC 属性去打开文件时,如果这个文件中本来是有内容的,而且为只读或只写成功,则将其长度截断为0
Mode:一定是在flags中使用了O_CREAT标志,mode记录待创建的文件的访问权限
可读:r 4
可写:w 2
执行:x 1
*/
int creat(const char *pathname, mode_t mode);
ls -l:列出文件清单
创建一个文件,打开文件并且打印返回的值
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
int main()
{
int fd;
fd = open("file1", O_RDWR);
printf("fd = %d\n", fd);
return 0;
}
打开一个文件,判断文件是否存在,若不存在,打开失败并且新建文件,并且输出打开成功
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
int main()
{
int fd;
fd = open("file1", O_RDWR);
if(fd == -1)
{
printf("open fail\n");
fd = open("file1", O_RDWR|O_CREAT, 0600);//0600为权限
if(fd > 0)
{
printf("open success\n");
}
}
return 0;
}
打开文件,将定义的字符串写入文件
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <unistd.h>//write close
#include <string.h>//strlen
int main()
{
int fd;
char *buf = "you are beautiful!";
fd = open("file1", O_RDWR);
if(fd == -1)
{
printf("open fail\n");
fd = open("file1", O_RDWR|O_CREAT, 0600);
if(fd > 0)
{
printf("open success\n");
}
}
printf("fd = %d\n", fd);
//ssize_t write(int fd, const void *buf, size_t count);
write(fd, buf, strlen(buf));//使用sizeof只能计算出字符类型大小
close(fd); //写完记得关闭!
return 0;
}
读取文件,不采用光标移动的方法,而是写入文件之后,关闭重新打开
#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;
char *buf = "you are beautiful!";
fd = open("file1", O_RDWR);
if(fd == -1)
{
printf("open fail\n");
fd = open("file1", O_RDWR|O_CREAT, 0600);
if(fd > 0)
{
printf("open success\n");
}
}
printf("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 bytes to file!\n", n_write);
}
close(fd);//关闭文件重新打开,避免因光标位置的问题读取不到内容
fd = open("file1", O_RDWR);
//ssize_t read(int fd, void *buf, size_t count);
char *readBuf;
readBuf = (char *)malloc(sizeof(char)*n_write+1);
int n_read = read(fd, readBuf, n_write);
printf("read %d bytes, content:%s\n", n_read, readBuf);
close(fd);
return 0;
}
光标移动函数:
lseek(int fd, off_t offset, int whence);
fd:文件名
offset:偏移值
whence:
- SEEK_SET:文件开头位置
- SEEK_CUR:光标在文件当前的位置
- SEEK_END:文件末尾
采用光标移动的方式来读取文件
#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;
char *buf = "you are beautiful!";
fd = open("file1", O_RDWR);
if(fd == -1)
{
printf("open fail\n");
fd = open("file1", O_RDWR|O_CREAT, 0600);
if(fd > 0)
{
printf("open success\n");
}
}
printf("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 bytes to file!\n", n_write);
}
//ssize_t read(int fd, void *buf, size_t count);
char *readBuf;
readBuf = (char *)malloc(sizeof(char)*n_write+1);
//off_t lseek(int fd, off_t offset, int whence);
lseek(fd, 0, SEEK_SET);//偏移值为负值光标向当前位置的左边移动,为正向右移动
int n_read = read(fd, readBuf, n_write);
printf("read %d bytes, content:%s\n", n_read, readBuf);
close(fd);
return 0;
}
采用光标移动的方式来计算文件中的字节数
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <unistd.h>
int main()
{
int fd;
fd = open("file1", O_RDWR);
//off_t lseek(int fd, off_t offset, int whence);
int sizefile = lseek(fd, 0, SEEK_END);
printf("file's size is:%d\n", sizefile);
close(fd);
return 0;
}
采用O_APPEND(在文件中原本存在的内容的基础上另起一行重新写入,保留原来的内容,若没有O_APPEND则覆盖原本内容)来写入文件内容;O_TRUNC将原本的内容全部删除,重新写入要写的内容;O_EXCL可检查文件是否存在
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
int main()
{
int fd;
char *buf = "you are beautiful!";
fd = open("file1", O_RDWR|O_APPEND);
printf("open success:fd = %d\n", fd);
int n_write = write(fd, buf, strlen(buf));
if(n_write != -1)
{
printf("write %d bytes to file1\n", n_write);
}
close(fd);
return 0;
}
创建文件creat函数
creat(const char *filename, mode_t mode)
file:要创建的文件名(包含路径,没有则为当前路径)
mode:创建模式 //可读可写可执行
宏表示 数字
S_IRUSR 4 可读
S_IWUSR 2 可写
S_IXUSR 1 可执行
S_IRWXU 7 可读,写,执行