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:

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

    ReplyDelete
  2. 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;
    }

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

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

    ReplyDelete