Let fp1 point to a file called file1, fp2 point to a file called file2. file1 and file2 are exactly same. And no file open errors.
But function test_fgets prints out different strings. The line printf("%s", lptr1) prints out NULL, but printf("%s", lptr2) prints out the last line of file2.
Since file1 and file2 are exactly the same, why print different outputs?
Besides, when I tried to change printf("%s", lptr1) to printf("%s\n", lptr1), it gave me a segmentation fault(core dump) error. Why is it? How does a newline character affect here?
...
#define MAXLINE 100
char line1[MAXLINE];
char line2[MAXLINE];
...
void test_fgets(FILE *fp1, FILE *fp2){
char *lptr1;
char *lptr2;
while( (lptr1 = fgets(line1, MAXLINE, fp1)) && (lptr2 = fgets(line2, MAXLINE, fp2)))
;
printf("%s", lptr1);
printf("%s", lptr2);
}