turning userinput into a text file

Reply

Join Date: Jan 2007
Posts: 27
Reputation: cusado is an unknown quantity at this point 
Solved Threads: 0
cusado cusado is offline Offline
Light Poster

turning userinput into a text file

 
0
  #1
Feb 12th, 2007
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?
Reply With Quote Quick reply to this message  
Join Date: Apr 2006
Posts: 5,050
Reputation: John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold 
Solved Threads: 331
Moderator
John A's Avatar
John A John A is offline Offline
Vampirical Lurker

Re: turning userinput into a text file

 
0
  #2
Feb 12th, 2007
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.
"Technological progress is like an axe in the hands of a pathological criminal."
Reply With Quote Quick reply to this message  
Join Date: Jan 2007
Posts: 27
Reputation: cusado is an unknown quantity at this point 
Solved Threads: 0
cusado cusado is offline Offline
Light Poster

Re: turning userinput into a text file

 
0
  #3
Feb 13th, 2007
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);
Reply With Quote Quick reply to this message  
Join Date: Apr 2006
Posts: 5,050
Reputation: John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold 
Solved Threads: 331
Moderator
John A's Avatar
John A John A is offline Offline
Vampirical Lurker

Re: turning userinput into a text file

 
0
  #4
Feb 13th, 2007
Read my last sentance:
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?
"Technological progress is like an axe in the hands of a pathological criminal."
Reply With Quote Quick reply to this message  
Join Date: Jan 2007
Posts: 27
Reputation: cusado is an unknown quantity at this point 
Solved Threads: 0
cusado cusado is offline Offline
Light Poster

Re: turning userinput into a text file

 
0
  #5
Feb 13th, 2007
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. }
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 3,114
Reputation: WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of 
Solved Threads: 281
Moderator
WaltP's Avatar
WaltP WaltP is offline Offline
Posting Sensei

Re: turning userinput into a text file

 
0
  #6
Feb 13th, 2007
Originally Posted by cusado View Post
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?
The 3 Laws of the Procrastination Society:
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
Reply With Quote Quick reply to this message  
Join Date: Jan 2007
Posts: 27
Reputation: cusado is an unknown quantity at this point 
Solved Threads: 0
cusado cusado is offline Offline
Light Poster

Re: turning userinput into a text file

 
0
  #7
Feb 13th, 2007
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
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 3,114
Reputation: WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of 
Solved Threads: 281
Moderator
WaltP's Avatar
WaltP WaltP is offline Offline
Posting Sensei

Re: turning userinput into a text file

 
0
  #8
Feb 13th, 2007
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()
The 3 Laws of the Procrastination Society:
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
Reply With Quote Quick reply to this message  
Join Date: Jan 2007
Posts: 27
Reputation: cusado is an unknown quantity at this point 
Solved Threads: 0
cusado cusado is offline Offline
Light Poster

Re: turning userinput into a text file

 
0
  #9
Feb 13th, 2007
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
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,142
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1434
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Most Valuable Poster

Re: turning userinput into a text file

 
0
  #10
Feb 13th, 2007
Originally Posted by cusado View Post
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.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC