Well you're just going to have to attach this mystery text file of yours.
Because there isn't anything wrong you're doing. The only time you dereferenced NULL was in the debugger (that doesn't count).
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(int argc, char *argv[])
{
FILE *stream;
size_t word = 100;
char *buf;
stream = fopen(argv[1], "r");
if ((buf = calloc(word, sizeof(char))) == NULL) {
printf("error");
exit(-1);
}
while (getline(&buf, &word, stream) != -1) {
char *l = buf;
l = strchr(l, '\n');
if (l == NULL) {
printf("error - no newline\n");
}
printf("%s", buf);
if ( l == NULL ) putchar('\n');
}
fclose(stream);
free(buf);
return 0;
}
Run on itself, with no last newline, results in
Code:
$ gcc -Wall foo.c
$ ./a.out foo.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(int argc, char *argv[])
{
FILE *stream;
size_t word = 100;
char *buf;
stream = fopen(argv[1], "r");
if ((buf = calloc(word, sizeof(char))) == NULL) {
printf("error");
exit(-1);
}
while (getline(&buf, &word, stream) != -1) {
char *l = buf;
l = strchr(l, '\n');
if (l == NULL) {
printf("error - no newline\n");
}
printf("%s", buf);
if ( l == NULL ) putchar('\n');
}
fclose(stream);
free(buf);
return 0;
error - no newline
}