声明:图片资源摘自于网络
1572612752969.png
创建文件
文件原子操作
- O_APPEND标志,进程对文件偏移量调整和追加数据是原子操作
- O_CREAT和O_EXCL,判定文件是否存在之后进行操作,文件创建和判定过程是原子操作
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <memory.h>
#include <fcntl.h>
#include <sys/msg.h>
#include <algorithm>
constexpr const char new_filename[] = "/tmp/x1";
void create_file()
{
/* int ret;
ret = access(new_filename, F_OK | R_OK | W_OK);
if (ret < 0)
{
perror("Can't permission access");
}*/
int fd = open(new_filename, O_RDWR | O_CREAT | O_TRUNC | O_NDELAY, 0766);
if (fd)
{
char str[] = "1234567890";
write(fd, str, sizeof(str));
}
close(fd);
}
获取文件大小
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <memory.h>
#include <fcntl.h>
#include <sys/msg.h>
#include <algorithm>
void get_file_size()
{
int fd = open(new_filename, O_RDONLY | O_NDELAY, 0766);
if (fd)
{
const long int &size = lseek(fd, 0, SEEK_END);
char str[size];
lseek(fd, 0, SEEK_SET);
read(fd, str, size);
printf("%s \n file size: %ld \n", str, size);
}
close(fd);
}
设置文件size
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <memory.h>
#include <fcntl.h>
#include <sys/msg.h>
#include <algorithm>
void set_file_size_by_seek()
{
int fd = open(new_filename, O_RDWR | O_CREAT | O_TRUNC | O_NDELAY, 0766);
if (fd)
{
char str[] = "1234567890";
write(fd, str, sizeof(str));
printf("str size:%ld", sizeof(str));
lseek(fd, 99, SEEK_SET);
write(fd, "\0", 1);
}
close(fd);
}
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <memory.h>
#include <fcntl.h>
#include <sys/msg.h>
#include <algorithm>
void set_file_size_by_truncate()
{
int fd = open(new_filename, O_RDWR | O_CREAT | O_TRUNC | O_NDELAY, 0766);
if (fd)
{
char str[] = "1234567890";
write(fd, str, sizeof(str));
printf("str size:%ld", sizeof(str));
ftruncate(fd, 1024);
}
close(fd);
}
获取文件的stat
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <memory.h>
#include <fcntl.h>
#include <sys/msg.h>
#include <algorithm>
void print_file_stat_struct()
{
using FileStat = struct stat;
size_t size = sizeof(FileStat);
printf("size = %ld \n", size);
FileStat fileStat{};
lstat(new_filename, &fileStat);
char _data[size];
memcpy(_data, &fileStat, size);
std::string data{_data, size};
std::cout << data << std::endl;
std::cout << fileStat.st_size << std::endl;
std::cout << fileStat.st_gid << std::endl;
std::cout << fileStat.st_uid << std::endl;
std::cout << fileStat.st_ctim.tv_nsec << std::endl;
std::cout << fileStat.st_ctim.tv_sec << std::endl;
std::cout << fileStat.st_atim.tv_nsec << std::endl;
std::cout << fileStat.st_atim.tv_sec << std::endl;
std::cout << fileStat.st_blksize << std::endl;
std::cout << fileStat.st_blocks << std::endl;
std::cout << fileStat.st_dev << std::endl;
std::cout << fileStat.st_mode << std::endl;
std::cout << fileStat.st_nlink << std::endl;
std::cout << fileStat.st_rdev << std::endl;
}
检测文件对当前用户操作的权限(access)
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <memory.h>
#include <fcntl.h>
#include <sys/msg.h>
#include <algorithm>
void file_unlink_access()
{
int ret;
ret = access(new_filename, F_OK | R_OK | W_OK);
if (ret < 0)
{
perror("Can't permission access\n");
}
int fd = open(new_filename, O_RDONLY);
unlink(new_filename); //grant delete power of 目录项
if (fd)
{
const long int &size = lseek(fd, 0, SEEK_END);
char str[size];
lseek(fd, 0, SEEK_SET);
read(fd, str, size);
printf("%s \n file size: %ld \n", str, size);
}
close(fd);
}
标准输入(0),标准输出(1),标准错误(2)
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <memory.h>
#include <fcntl.h>
#include <sys/msg.h>
#include <algorithm>
void stdout_dev()
{
printf("Main precess pid: %u \t thread id = %lu\n", getpid(), pthread_self());
char str[] = "213542363457";
write(STDOUT_FILENO, str, sizeof(str) - 1);
printf("%d\n%d\n%d\n", STDOUT_FILENO, STDIN_FILENO, STDERR_FILENO);
}
fcntl(修改已经打开文件的属性)
1572614202968.png
ioctl(驱动设备操作或外围设备操作)
1572614274071.png
dup和dup2(文件重定向)
1572614788016.png
lseek(定位文件位置)
1572632205025.png
link和unlink(创建硬连接和删除硬连接)
1572634803536.png
symlink和readlink(创建软连接和读取软连接)
1572634572946.png

1572614202968.png
1572614274071.png
1572614788016.png
1572632205025.png
1572634803536.png
1572634572946.png







网友评论