小编tia*_*nya的帖子

如何检测磁盘已满错误并在获得可用磁盘空间后让程序恢复

我正在编写一个可在LINUX上运行的应用程序,该程序使用fprintf和fwrite写入磁盘。我希望能够捕获“磁盘已满”错误,提示用户腾出更多空间,然后恢复运行,好像什么都没发生一样。有没有合适的解决方案?

c c++ linux filesystems

4
推荐指数
1
解决办法
3367
查看次数

posix_fallocate是否适用于以appened模式打开的文件?

我正在尝试为文件操作预分配磁盘空间,但是,我遇到一个奇怪的问题,即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)

c c++ linux

4
推荐指数
1
解决办法
309
查看次数

标签 统计

c ×2

c++ ×2

linux ×2

filesystems ×1