什么?ACGO的家被偷啦?3
2026-02-11 16:23:43
发布于:湖南
有人说我的这个系列根本造成不了什么,所以,有了今天的讨论
话不多说,进入正题。
创建文件:
#include <iostream>
#include <fcntl.h>
#include <unistd.h>
#include <errno.h>
#include <cstring>
bool createFileWithCreat(const char* filename) {
std::cout << "当前运行用户ID:" << getuid() << ",组ID:" << getgid() << std::endl;
int fd = creat(filename, 0644);
if (fd == -1) {
std::cerr << "[错误1] 创建文件失败 " << filename << ",原因:" << strerror(errno) << std::endl;
return false;
}
std::cout << "[成功] 文件描述符:" << fd << ",文件创建成功" << std::endl;
const char* content = "aaa";
size_t content_len = strlen(content);
ssize_t bytes_written = write(fd, content, content_len);
if (bytes_written == -1) {
std::cerr << "[错误2] 写入失败,原因:" << strerror(errno) << ",错误码:" << errno << std::endl;
close(fd);
return false;
} else if (bytes_written != (ssize_t)content_len) {
std::cerr << "[警告] 写入不完整:预期写入 " << content_len << " 字节,实际写入 " << bytes_written << " 字节" << std::endl;
} else {
std::cout << "[成功] 写入 " << bytes_written << " 字节,内容:" << content << std::endl;
}
if (close(fd) == -1) {
std::cerr << "[错误3] 关闭文件失败,原因:" << strerror(errno) << std::endl;
return false;
}
std::cout << "[验证] 文件路径:" << filename << std::endl;
int fd_check = open(filename, O_RDONLY);
if (fd_check != -1) {
char buf[10] = {0};
read(fd_check, buf, sizeof(buf)-1);
std::cout << "[程序内验证] 读取到文件内容:\"" << buf << "\"" << std::endl;
close(fd_check);
} else {
std::cerr << "[程序内验证] 无法打开文件读取内容:" << strerror(errno) << std::endl;
}
return true;
}
int main() {
const char* filename = "/tmp/creat_file.txt";
bool result = createFileWithCreat(filename);
if (result) {
std::cout << "[最终] 文件创建并写入成功!" << std::endl;
} else {
std::cout << "[最终] 文件操作失败!" << std::endl;
return 1;
}
return 0;
}
输出:
当前运行用户ID:12002,组ID:12002
[成功] 文件描述符:7,文件创建成功
[成功] 写入 3 字节,内容:aaa
[验证] 文件路径:/tmp/creat_file.txt
[程序内验证] 读取到文件内容:"aaa"
[最终] 文件创建并写入成功!
失败案例:
#include <iostream>
#include <fstream>
#include <string>
bool createFileWithFstream(const std::string& filename, const std::string& content) {
std::ofstream file(filename, std::ios::out | std::ios::trunc | std::ios::binary);
if (!file.is_open()) {
std::cerr << "错误:无法创建文件 " << filename << std::endl;
return false;
}
file.write(content.c_str(), content.size());
if (file.fail()) {
std::cerr << "错误:写入文件 " << filename << " 失败" << std::endl;
file.close();
return false;
}
file.close();
std::cout << "成功创建文件:" << filename << std::endl;
return true;
}
int main() {
createFileWithFstream("/tmp/creat_file.txt", "aaa");
return 0;
}
输出:运行错误
当时气死我了,差点就没有这个讨论了
删除文件(请勿用于坏事):
#include <iostream>
#include <string>
#include <ctime>
#include <cstdarg>
#include <unistd.h>
#include <dirent.h>
#include <errno.h>
#include <string.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <cstring>
bool createFileWithCreat(const char* filename) {
std::cout << "当前运行用户ID:" << getuid() << ",组ID:" << getgid() << std::endl;
int fd = creat(filename, 0644);
if (fd == -1) {
std::cerr << "[错误1] 创建文件失败 " << filename << ",原因:" << strerror(errno) << std::endl;
return false;
}
std::cout << "[成功] 文件描述符:" << fd << ",文件创建成功" << std::endl;
const char* content = "aaa";
size_t content_len = strlen(content);
ssize_t bytes_written = write(fd, content, content_len);
if (bytes_written == -1) {
std::cerr << "[错误2] 写入失败,原因:" << strerror(errno) << ",错误码:" << errno << std::endl;
close(fd);
return false;
} else if (bytes_written != (ssize_t)content_len) {
std::cerr << "[警告] 写入不完整:预期写入 " << content_len << " 字节,实际写入 " << bytes_written << " 字节" << std::endl;
} else {
std::cout << "[成功] 写入 " << bytes_written << " 字节,内容:" << content << std::endl;
}
if (close(fd) == -1) {
std::cerr << "[错误3] 关闭文件失败,原因:" << strerror(errno) << std::endl;
return false;
}
std::cout << "[验证] 文件路径:" << filename << std::endl;
int fd_check = open(filename, O_RDONLY);
if (fd_check != -1) {
char buf[10] = {0};
read(fd_check, buf, sizeof(buf)-1);
std::cout << "[程序内验证] 读取到文件内容:\"" << buf << "\"" << std::endl;
close(fd_check);
} else {
std::cerr << "[程序内验证] 无法打开文件读取内容:" << strerror(errno) << std::endl;
}
return true;
}
#define LOG_INFO(msg) std::cout<<"[信息]"<<msg<<std::endl;
#define LOG_ERROR(msg) std::cout<<"[错误]"<<msg<<std::endl;
bool delete_file(const std::string& filepath) {
LOG_INFO("开始删除文件: " + filepath);
int ret = unlink(filepath.c_str());
if (ret == 0) {
LOG_INFO("文件删除成功: " + filepath);
return true;
} else {
LOG_ERROR("删除文件失败: " + filepath + " (errno: " + std::to_string(errno) + ", " + strerror(errno) + ")");
return false;
}
}
int main() {
std::string test_file = "/tmp/creat_file.txt";
createFileWithCreat("/tmp/creat_file.txt");
LOG_INFO("==================== 开始处理删除请求 ====================");
delete_file(test_file);
LOG_INFO("==================== 删除请求处理完成 ====================");
return 0;
}
输出(其实在里面套了一个创建文件的函数):
当前运行用户ID:12002,组ID:12002
[成功] 文件描述符:7,文件创建成功
[成功] 写入 3 字节,内容:aaa
[验证] 文件路径:/tmp/creat_file.txt
[程序内验证] 读取到文件内容:"aaa"
[信息]==================== 开始处理删除请求 ====================
[信息]开始删除文件: /tmp/creat_file.txt
[信息]文件删除成功: /tmp/creat_file.txt
[信息]==================== 删除请求处理完成 ====================
所以说,还是可以干些什么的。。。。。。(坏笑)
本文只用于学术讨论,请勿作于非法用途
全部评论 2
对着评测机改这些有啥用
1周前 来自 广东
0?so 似乎并没有什么卵用
1周前 来自 浙江
0





























有帮助,赞一个