[英]Writing C program to create a 1 MB file on Linux and getting Segmentation Fault
I'm trying to write a simple C program for Linux that will generate a 1 MB file but I can't get this code to work. 我正在尝试为Linux编写一个简单的C程序,该程序将生成一个1 MB的文件,但是我无法使此代码正常工作。 When I try to run it I get a seg fault error message thrown and I'm not really sure where it's going wrong.
当我尝试运行它时,我会收到一条段错误消息,并且我不确定该出哪里了。 I do have a hunch that it is with fseek though:
我确实有一个预感,尽管它与fseek有关:
#include <stdio.h>
int main(int argc, char *argv[])
{
FILE *fp;
fp = fopen(argv[0], "w");
fseek(fp, 1000000 - 1, SEEK_SET);
fputc('\0', fp);
return 0;
}
Can anyone point me in the right direction? 谁能指出我正确的方向?
argv[0]
is the name of your binary. argv[0]
是您的二进制文件的名称。 You want to use argv[1]
as your filename. 您想使用
argv[1]
作为文件名。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.