How can I write an input string to a text file? Please help.
Dear Cprogramming.com members and administrator,
Good day to everyone. I programmed this simple C program that accepts an string input and display it on the screen:
Code:
#include<stdio.h>
#include<string.h>
int main()
{
char name[20];
printf("Please enter your input string: ");
gets(name);
printf("Hello. Your input is: %s", name);
getch();
return 0;
}
It works fine, but I just have certain questions on this like:
1. What necessary C code will I add on this so that my input will be saved on a textfile? Let's
say when I input a string, the string (or maybe the message plus the name) will be
saved to a file named"hello.txt" (I just wonder about this thing because in Java, I have
this is possible using the BufferedWriter class to put my string in a textfile).
2. Can I use a "maximum" length in array without defining it? I mean what if I want more than
20 characters, 40 characters, 60, etc? Or there's no other way but to "maximize"
beforehand the array length of the said array declaration?
Thank you and more power to everyone.
Respectfully yours,
MarkSsquall
The code was successfully working, how would I read the file then? Please help.
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 :)
Thanks for all the replies.
Dear Cprogramming.com members,
First I want to thank for those who make time correcting my code and gave proper coding in my sample C program, I really do appreciate it, I'll remember such suggestions, thanks again. I have downloaded Dev-C++ and Code::Blocks and it is really a nice environment, I'll be familiarizing them as of now, but anymore IDE you can suggest? I have Microsoft Visual C++ but I'm always encountering a problem when I tried to compile them, eventhough I just followed what the books have written, that's why I'd uninstalled it and kept just Visual Basic, I'll just post what kind of problem is that.
Adding to dwks code, what if the program (as I want to add) ask if the user wants to read that file, and if I type Y/(or y) for YES, what code will I add? If I enter: Cprogramming.com and it will save to a file (it works) and if the program ask "Do you want to read the file?" if I enter Y, it will just display Cprogrammming.com in the output.
God bless everyone.
Repectfully yours,
MarkSquall