我正在编写一个可在LINUX上运行的应用程序,该程序使用fprintf和fwrite写入磁盘。我希望能够捕获“磁盘已满”错误,提示用户腾出更多空间,然后恢复运行,好像什么都没发生一样。有没有合适的解决方案?
我正在尝试为文件操作预分配磁盘空间,但是,我遇到一个奇怪的问题,即posix_fallocate只调用一个字节,当我调用它来为使用追加模式打开的文件分配磁盘空间时,文件内容也是意外的.有谁知道这个问题?我的测试代码是,
#include <cstdio>
#include <fcntl.h>
#include <unistd.h>
#include <sys/stat.h>
#include <cerrno>
int main(int argc, char **argv)
{
FILE *fp = fopen("append.txt", "w");
for (int i = 0; i < 5; ++i)
fprintf(fp, "## Test loop %d\n", i);
fclose(fp);
sleep(1);
int fid = open("append.txt", O_WRONLY | O_APPEND);
struct stat status;
fstat(fid, &status);
printf("INFO: sizeof 'append.txt' is %ld Bytes.\n", status.st_size);
int ret = posix_fallocate(fid, (off_t)status.st_size, 1024);
if (ret) {
switch (ret) {
case EBADF:
fprintf(stderr, "ERROR: %d is not a valid file …Run Code Online (Sandbox Code Playgroud)