I run this program like this from the command line:
Code:
./writer mytext.txt
and then the file "mytext.txt" is created and cli notifies and prompts for writing into it, then I write something like "Hello World!" and finish it by giving the specific EOF of my OS and the program ends without any errors. But when I open the text file nothing is in there. I've also "cleaned up after myself" by flushing wherever I felt it's needed. Please help.
Code:
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char **argv){
FILE *fp;
int ch;
if(argc != 2){
printf("Wrong number of arguments entered. Filename: %s",argv[1]);
exit(EXIT_FAILURE);
}
fp = fopen(argv[1],"a+");
printf("File opened successfully. Write to file now:\n");
fflush(NULL);
while((ch = getchar()) != EOF){
fprintf(fp,"%c",ch);
}
fclose(fp);
return 0;
}