file reading line by line and printing not working on mac os but works fine in RHEL

20 Views Asked by At

The following code works in Linux but fails with a Segmentation Fault 11 on Mac OS (Catalina). What am I doing wrong?

Thanks.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main (int argc, char *argv[]) {
        FILE *fp;
        char *line;
        int i;
        int max_line = 1000;
        if (--argc > 0) {
                if ( (fp = fopen(argv[1],"r")) != NULL) {
                        printf("File ptr:%p File Name:%s\n", fp, argv[1]);
                        while (fgets (line, sizeof(line), fp) != NULL) {
                                fputs(line, stdout);
                        }   
                        fclose(fp);
                } else {
                        printf("File cannot be opened:%s\n", argv[1]);
                }   
        }   
        return 0;
}

My input file:

0 1
0 2
0 5
0 6
5 3
0 4
0

There are 0 best solutions below