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):
Code:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int main()
{
char name[20];
char choice;
FILE *stream, *fopen();
clrscr();
stream = fopen("sample.txt","w");
if ( (stream = fopen("sample.txt","w") ) == NULL)
{
printf("Sorry, cannot open the file %s","sample.txt");
exit(1);
}
else
{
printf("Please enter your name: ");
gets(name);
if ( (stream = fopen("sample.txt","w") ) != NULL)
fprintf(stream, "%s", name);
}
getch();
return 0;
} /*end of main */
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:
Code:
printf("Do you want to open and read your file?[Y/N]: ");
scanf("%c", &choice);
if( (choice=='Y') || (choice=='y') )
/* what C code will I put here? */
else if ( (choice=='N') || (choice=='n') )
printf("Thank you...");
else
printf("Invalid input, please try again.");
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.
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.
Respectfully yours,
MarkSquall