下面的C程序创建一个指定长度的空文件.我已经在我本地的Ubuntu20.04笔记本电脑上测试了这一点,它可以工作.我现在在Digital Ocean(Ubuntu 22.04)的一个实例中运行它,但是行"flose(Outfile);"抛出了一个分段错误.在云中,我在Docker容器中运行它,但不是在本地.
#include <stdio.h>
#include <string.h>
#include <errno.h>
size_t create_empty_file (char * out_fname, size_t file_size)
{
FILE *outfile;
char buf[1024];
outfile = fopen(out_fname, "wb+");
if (outfile == NULL) {
strcpy(buf, strerror(errno));
printf("%d --> %s\n", errno, buf); }
fseek(outfile, file_size, SEEK_SET);
if (errno != 0) {
strcpy(buf, strerror(errno));
printf("%d --> %s\n", errno, buf); }
fputc('\0', outfile);
if (errno != 0) {
strcpy(buf, strerror(errno));
printf("%d --> %s\n", errno, buf); }
fclose( outfile );
return 0;
}
文件确实创建了,并且文件确实关闭了--我知道这一点,因为在文件关闭之前,它显示的文件大小为零,并且文件确实显示了其指定的4 GB长度.
通常情况下,当文件指针为空时,flose会抛出段错误,但这里的情况并非如此.而且它在我本地的笔记本电脑上也能用.
以下是来自gdb的段故障详细信息:
程序接收信号SIGSEGV,分段故障. __GI__IO_setb(f=f@Entry=0x145b210,b=b@Entry=0x0,Eb=EB@Entry=0x0,a=a@Entry=0)中的0x00007fe121321c70位于/libio/genops.c:338 338./libio/genops.c:没有这样的文件或目录.
谢谢你在这方面的帮助.