Writing C program to create a 1 MB file on Linux and getting Segmentation Fault

562 views Asked by At

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. 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:

#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?

1

There are 1 answers

1
xbug On BEST ANSWER

argv[0] is the name of your binary. You want to use argv[1] as your filename.