| | |
turning userinput into a text file
![]() |
•
•
Join Date: Jan 2007
Posts: 27
Reputation:
Solved Threads: 0
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
and then i also tried
any ideas on why it wont create the text file?
I have tried to work it out but it still doesnt work properly.
i used
c Syntax (Toggle Plain Text)
getc(stdin); char filename[80]; sprintf(filename,"%d.txt", stdin);
and then i also tried
c Syntax (Toggle Plain Text)
char line[255]; fgets(line, 255, stdin); char filename[80]; sprintf(filename,"%d.txt", stdin);
This line is incorrect:
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
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.
sprintf(filename,"%d.txt", stdin);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."
•
•
Join Date: Jan 2007
Posts: 27
Reputation:
Solved Threads: 0
the program runs, but no output file is generated? am i missing a command still?
c Syntax (Toggle Plain Text)
char line[255]; fgets(line, 255, stdin); char filename[80]; sprintf(filename,"%s.txt",line);
Read my last sentance:
You're going to have to remove the newline character (if it's there), which is usually as simple as doing the following
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?
•
•
•
•
Originally Posted by Me
Also beware of newlines in your string, which could thoroughly mess up your program.
C Syntax (Toggle Plain Text)
if (string[strlen(string)-1] == '\n') 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."
•
•
Join Date: Jan 2007
Posts: 27
Reputation:
Solved Threads: 0
well the entirety is only
c Syntax (Toggle Plain Text)
#include <stdio.h> #include <string.h> #include <ctype.h> int main(int argc, char *argv[]) { char line[255]; fgets(line, 255, stdin); char filename[80]; sprintf(filename,"%s.txt",line); return 0; }
•
•
•
•
the program runs, but no output file is generated? am i missing a command still?
c Syntax (Toggle Plain Text)
#include <stdio.h> #include <string.h> #include <ctype.h> int main(int argc, char *argv[]) { char line[255]; fgets(line, 255, stdin); char filename[80]; // This line can't be here. It has to // be above all executable code in C sprintf(filename,"%s.txt",line); return 0; }
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
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
•
•
Join Date: Jan 2007
Posts: 27
Reputation:
Solved Threads: 0
well i used
but how do i represet "%s.txt" as the user's input?
c Syntax (Toggle Plain Text)
fout = fopen("%s.txt", "w");
Last edited by Ancient Dragon; Feb 13th, 2007 at 6:08 pm. Reason: removed ugly colors and added code tags
•
•
•
•
well i used
fout = fopen("%s.txt", "w");
but how do i represet "%s.txt" as the user's input?
c Syntax (Toggle Plain Text)
sprintf(filename,"%s.txt",line); // <<< from your previous post 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.
![]() |
Similar Threads
- connect to text file database (Visual Basic 4 / 5 / 6)
- Store Bluetooth remote address to a text file (C++)
- 10 line text file (Java)
- Read and write to an ASCII Text file (Java)
Other Threads in the C Forum
- Previous Thread: Appending to a text file
- Next Thread: recv error and synchronization problems
| Thread Tools | Search this Thread |
* adobe ansi api array asterisks binarysearch calculate centimeter changingto char character cm convert copyimagefile copypdffile cprogramme creafecopyofanytypeoffileinc createcopyoffile csyntax database directory feet fflush fgets file fork forloop frequency givemetehcodez grade gtkgcurlcompiling gtkwinlinux hacking highest histogram homework i/o inches infiniteloop input intmain() iso kernel keyboard km linked linkedlist linux linuxsegmentationfault list locate looping loopinsideloop. lowest match microsoft mqqueue mysql number oddnumber odf open opendocumentformat owf pattern pdf performance posix probleminc process program programming radix recv recvblocked repetition research reversing scanf scheduling segmentationfault send sequential socket socketprograming stack standard string systemcall threads turboc unix user variable voidmain() wab whythiscodecausesegmentationfault windows.h windowsapi






