Dear Adak, invinciblevn,
Good day everyone. Thank you for the website you've shared Adak, I try to work on this out based on the website you gave, and I have come up with the code, here it is (please feel free to suggest/correct me more if I "lack" or "violate" something):
Thanks again for the site and info Adak, invinciblevn. But I want to add more to this simple program, that is, after writing the string to my sample.txt, I want my program to read it now, I want to put this line of code before the getch() function:
FILE *stream, *fopen();
stream = fopen("sample.txt","w");
if ( (stream = fopen("sample.txt","w") ) == NULL)
printf("Sorry, cannot open the file %s","sample.txt");
printf("Please enter your name: ");
if ( (stream = fopen("sample.txt","w") ) != NULL)
fprintf(stream, "%s", name);
} /*end of main */
From the code above, what C code will I add so that if I press Y or y, my program will read the file sample.txt.
printf("Do you want to open and read your file?[Y/N]: ");
if( (choice=='Y') || (choice=='y') )
/* what C code will I put here? */
else if ( (choice=='N') || (choice=='n') )
printf("Invalid input, please try again.");
And one more thing, the program automatically creates SAMPLE.TXT (all caps), but while in the source code, the filename is all lowercase? Do I have a workaround on this? I mean, can I make it all lowercase or it depends on my compiler? I'm using Turbo C by Borland (running in DOS).
Thank you and God bless everyone.