You should post a small, complete, runnable program so we can run it.
You should test if fp is NULL after attempting to open the file to see if it failed.
You shouldn't use fflush(stdin) as it is "undefined behaviour" in Standard C (although it may do what you want on a particular system). You don't seem to have any need for it in your code.
It is best to test the return value of fgets for NULL to indicate the end-of-file condition (or an error condition) instead of using feof.
Ctrl-Z is not the "eof" for stdin. It is a keyboard signal to send the eof signal to the program. It's also just for Windows. It is Ctrl-D on unix.
This is how your code would normally be written:
Code:
FILE *fp = fopen("par3.txt", "w");
if (fp == NULL) {
perror("fopen");
exit(EXIT_FAILURE); // EXIT_FAILURE is defined in stdlib.h
}
while (fgets(line, 40, stdin) != NULL) {
fputs(line, fp);
fputs(line, stdout);
fputc('\n', stdout); // this adds an extra newline
}
fclose(fp);
Overall I can't see anything that would stop the output file from having the data.