Thursday, October 4, 2018

Too Much Lost

char* line;
FILE* source;
line=malloc(2000);
source = fopen(argv[1]);
if (source != NULL)
{
    fgets(line, 2000, source);
segmentation fault on fgets.

It is so discouraging that something that used to be simple for me to do for a living is now gone.

4 comments:

coderpunk said...

On linux fopen takes a mode argument, like "r", so maybe that's the problem?

Rick C said...

Hmm. this worked for me on Windows 10 with Visual Studio. (I skipped a bit, like closing the file).

#include >stdio.h>
#include >stdlib.h>

int main(int argc, char *argv[])
{
char *line;
FILE *source;
line = malloc(2000);
source = fopen(argv[1], "r");
if (source != NULL)
{
fgets(line, 2000, source);
puts(line);
}
return 0;
}

Jim said...

It's been a long time but did you remember to #include ?

Jim said...

I also think you have to give a mode to fopen(), not just a filename.