Hi all, I'm learning C for the first time (first time learning any programming language). I'm currently going through the tutorial on this site. After reading the tutorial on file I/O I've been trying a few things out.
I'm trying to find a way of asking the user for an input, and then using this input to name a file.
Originally I had:
Then i want to make this something like the following:Code:#include<stdio.h> int main(){ FILE *fp; fp=fopen("d:\\name_of_file.txt","w"); fprintf(fp, "this is text printed in the file"); getchar(); return 0; }
Is there a way of doing something like :Code:#include<stdio.h> int main(){ char x[20]; FILE *fp; printf("Please enter a name for the file...\n"); fgets(x, 20, stdin); fp=fopen("d:\\name_of_file.txt","w"); //here i want x to be the name of the file fprintf(fp, "this is text printed in the file"); getchar(); return 0; }
Thanks for any help.Code:fp=fopen("d:\\%c",x,"w");



LinkBack URL
About LinkBacks


