943,534 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Unsolved
  • Views: 5371
  • C RSS
You are currently viewing page 1 of this multi-page discussion thread
Feb 12th, 2007
0

turning userinput into a text file

Expand Post »
how to turn a user input into a text file for example user inputs 100 and the program creates a text file named 100.txt?
I have tried to work it out but it still doesnt work properly.
i used
  1. getc(stdin);
  2.  
  3. char filename[80];
  4. sprintf(filename,"%d.txt", stdin);

and then i also tried

  1. char line[255];
  2. fgets(line, 255, stdin);
  3.  
  4. char filename[80];
  5. sprintf(filename,"%d.txt", stdin);
any ideas on why it wont create the text file?
Similar Threads
Reputation Points: 10
Solved Threads: 0
Light Poster
cusado is offline Offline
27 posts
since Jan 2007
Feb 12th, 2007
0

Re: turning userinput into a text file

This line is incorrect:
sprintf(filename,"%d.txt", stdin);
Since you've already gotten the input from stdin, you should be supplying the variable which contains the user input as an argument instead of stdin.

And since you read it in as a string, you'll want to change "%d" to "%s". Also beware of newlines in your string, which could thoroughly mess up your program.
Team Colleague
Reputation Points: 2240
Solved Threads: 338
Vampirical Lurker
John A is offline Offline
5,055 posts
since Apr 2006
Feb 13th, 2007
0

Re: turning userinput into a text file

the program runs, but no output file is generated? am i missing a command still?
  1. char line[255];
  2. fgets(line, 255, stdin);
  3. char filename[80];
  4. sprintf(filename,"%s.txt",line);
Reputation Points: 10
Solved Threads: 0
Light Poster
cusado is offline Offline
27 posts
since Jan 2007
Feb 13th, 2007
0

Re: turning userinput into a text file

Read my last sentance:
Quote originally posted by Me ...
Also beware of newlines in your string, which could thoroughly mess up your program.
You're going to have to remove the newline character (if it's there), which is usually as simple as doing the following
  1. if (string[strlen(string)-1] == '\n')
  2. string[strlen(string)-1] = '\0';

Anyhow, the code works fine for me, even without the newline character removal. So perhaps it would be best for you to show us the code you're using?
Team Colleague
Reputation Points: 2240
Solved Threads: 338
Vampirical Lurker
John A is offline Offline
5,055 posts
since Apr 2006
Feb 13th, 2007
0

Re: turning userinput into a text file

well the entirety is only
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <ctype.h>
  4. int main(int argc, char *argv[]) {
  5. char line[255];
  6. fgets(line, 255, stdin);
  7. char filename[80];
  8. sprintf(filename,"%s.txt",line);
  9. return 0;
  10. }
Reputation Points: 10
Solved Threads: 0
Light Poster
cusado is offline Offline
27 posts
since Jan 2007
Feb 13th, 2007
0

Re: turning userinput into a text file

Click to Expand / Collapse  Quote originally posted by cusado ...
the program runs, but no output file is generated? am i missing a command still?
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <ctype.h>
  4. int main(int argc, char *argv[]) {
  5. char line[255];
  6. fgets(line, 255, stdin);
  7. char filename[80]; // This line can't be here. It has to
  8. // be above all executable code in C
  9. sprintf(filename,"%s.txt",line);
  10. return 0;
  11. }
Yeah. Where do you:
1) open the file after you generate the name?
2) write to the file?
3) close the file before exiting?
Moderator
Reputation Points: 3275
Solved Threads: 890
Posting Sage
WaltP is offline Offline
7,716 posts
since May 2006
Feb 13th, 2007
0

Re: turning userinput into a text file

well all i want to do is for the user to input a name or number and for the program to create a text file with that name. im not actually writing anything into the file, its just an empty .txt file with a name that hte user chose
Reputation Points: 10
Solved Threads: 0
Light Poster
cusado is offline Offline
27 posts
since Jan 2007
Feb 13th, 2007
0

Re: turning userinput into a text file

You still have to open the file for writing. It doesn't magically get created just because you create a name in a variable...

Look up fopen()
Moderator
Reputation Points: 3275
Solved Threads: 890
Posting Sage
WaltP is offline Offline
7,716 posts
since May 2006
Feb 13th, 2007
0

Re: turning userinput into a text file

well i used

  1. fout = fopen("%s.txt", "w");
but how do i represet "%s.txt" as the user's input?
Last edited by Ancient Dragon; Feb 13th, 2007 at 6:08 pm. Reason: removed ugly colors and added code tags
Reputation Points: 10
Solved Threads: 0
Light Poster
cusado is offline Offline
27 posts
since Jan 2007
Feb 13th, 2007
0

Re: turning userinput into a text file

Click to Expand / Collapse  Quote originally posted by cusado ...
well i used

fout = fopen("%s.txt", "w");

but how do i represet "%s.txt" as the user's input?
you already posted the answer to that question in your post #5. Just use variable filename
  1. sprintf(filename,"%s.txt",line); // <<< from your previous post
  2. fout = fopen(filename, "w");
Last edited by Ancient Dragon; Feb 13th, 2007 at 6:07 pm.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,945 posts
since Aug 2005

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: Appending to a text file
Next Thread in C Forum Timeline: recv error and synchronization problems





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC