Reputation: 83
The code that I'm trying to run is working flawlessly in linux but unfortunately it's not working on macOS.
For the same code below, when I try to run on macOS Mojave with a gcc compiler I'm getting a Segmentation Fault: 11
#include <stdio.h>
#include <stdlib.h>
int main() {
FILE *fa;
FILE *fb;
int ca, cb;
char buf[1000];
fa = fopen("q4in.c", "r");
if (fa == NULL) {
printf("Cannot open file");
exit(0);
}
fb = fopen("q4out.c", "w");
ca = getc(fa);
while (ca != EOF) {
int i = 0;
while (ca != '\n') {
buf[i++] = ca;
ca = getc(fa);
buf[i] = '\0';
}
ca = getc(fa);
}
for (int i = 0; i < 5; i++) {
printf("%c",buf[i]);
}
}
Upvotes: 0
Views: 385
Reputation: 3315
I'm going to take a wild guess, and offer as an alternative that the MacOs vs Linux is a red herring.
And that the real problem is that the last line of the input file on the Mac doesn't terminate with \n.
Upvotes: 3