turning userinput into a text file

Please support our C advertiser: Programming Forums - DaniWeb Sister Site
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

Re: turning userinput into a text file

 
0
  #11
Feb 13th, 2007
but i though the entry in fopen has to be a either in "" or predefined?

  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <ctype.h>
  4. #define INFILE "questions.txt"
  5.  
  6.  
  7. int main(int argc, char *argv[]) {
  8. char mc;
  9. char rt;
  10. char filename[80];
  11. char line[255];
  12. FILE *fin,*fout,*fid;
  13. fin = fopen(INFILE, "r");
  14.  
  15.  
  16. printf("enter student ID:");
  17. fgets(line, 255, stdin);
  18. sprintf(filename,"%s.txt",line);
  19.  
  20. fout = fopen(filename, "w");
  21. return 0;
  22. }
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,485
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: 1478
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: turning userinput into a text file

 
0
  #12
Feb 13th, 2007
Originally Posted by cusado View Post
but i though the entry in fopen has to be a either in "" or predefined?
It can be, but it can also be a variable, such as I posted. All it is looking for is const char*, doesn't care if it is a char variable or a literal string.
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  
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
  #13
Feb 13th, 2007
but when i leave it as (filename,'w') and compile and run, the program notes a windows error and closes, and no file is generated
Last edited by cusado; Feb 13th, 2007 at 6:50 pm.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,485
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: 1478
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: turning userinput into a text file

 
0
  #14
Feb 13th, 2007
fgets() function leaves the '\n' in the input buffer, so you need to truncate it
  1. printf("enter student ID:");
  2. fgets(line, 255, stdin);
  3. // truncate '\n'
  4. if( line[strlen(line)-1] == '\n')
  5. line[strlen(line)-1] = '\0';
  6. sprintf(filename,"%s.txt",line);
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  
Join Date: May 2006
Posts: 3,117
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
  #15
Feb 14th, 2007
Originally Posted by cusado View Post
but i though the entry in fopen has to be a either in "" or predefined?
Why would you think that?

Originally Posted by cusado View Post
well i used
  1. fout = fopen("%s.txt", "w");
Originally Posted by cusado View Post
but when i leave it as (filename,'w') and compile and run, the program notes a windows error and closes, and no file is generated
Why did you change "w" to 'w'? Change it back and it will work.
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: 171
Reputation: Lazaro Claiborn is an unknown quantity at this point 
Solved Threads: 13
Lazaro Claiborn's Avatar
Lazaro Claiborn Lazaro Claiborn is offline Offline
Junior Poster

Re: turning userinput into a text file

 
0
  #16
Feb 14th, 2007
Originally Posted by cusado View Post
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);
Here is a program to solve your problems. Make sure you're include files are being properly included.

  1. #include <stdio.h>
  2. #include <string.h>
  3. #define MAXINPUT 100
  4.  
  5. /* Function used if the user didn't pass program parameter */
  6. void getUserInput(void);
  7.  
  8. /* udtf (user defined text filename) is the global character variable buffer. */
  9. char udtf[MAXINPUT];
  10.  
  11. /* Create a constant character array holding the file extentions - you can change the saved file type simply by changing this initialization */
  12. const char fext[] = "txt";
  13.  
  14. /* See below for information regarding the keywords "static" and "inline". This is just a header printed for standard information to the user about the program, including how view usage information
  15. or they'll probably never know how. */
  16. static inline void _header(void)
  17. {
  18. puts("UDTF (User Define Text Filename) version 1.0");
  19. printf("Passed parameters or a prompt will allow a user to create a text file whose \nfilename is specifed by the user using the sprintf function");
  20. printf("For usage \ninformation pass the -h parameter to the program.\n\n");
  21. }
  22.  
  23. /* This is a static funciton which simply means that only the scope of this program is able to access it. Inline means that the function is expanded where it is called; Note that it is not good practice to use inline functions that contain a massive block of code. This function is for usage information at the users request. */
  24. static inline void _usage(const char *prog_name)
  25. {
  26. printf("%s -h Show usage help information\n", prog_name);
  27. printf("%s filename", prog_name);
  28. }
  29.  
  30. int main(int argc, char *argv[])
  31. {
  32. /* For the noobs sake, the FILE pointer fp will hold a handle or the address returned after issuing the fopen(...) function and will be used for referrence to the open file - which in this case will just close because you simply specified the user create a text file. */
  33. FILE *fp;
  34.  
  35. /* Show program header - refer above for an explaination */
  36. _header();
  37.  
  38. /* Check to see if the user passed any program parameters so we can use it instead of prompting the user for a name. Essentially, the argc variable holds the number of parameters passed to the program including the program name itself, which is why its argc > 1 rather than argc > 0. */
  39. if (argc > 1)
  40. {
  41.  
  42. /* Check if the user requests usage information */
  43. if (!strcmp(argv[1], "-h"))
  44. {
  45.  
  46. /* The program name is pointed to by argv[0] - the first element of the array */
  47. _usage(argv[0]);
  48. goto End;
  49. }
  50.  
  51. /* So if the user passed a program paremeter which'll represent the name of the text file so we don't have to prompt the user, we first make sure the string length of the first parameter char array pointed to by argv[1] doesn't exceed the MAXINPUT constant defined above.
  52. You can change the max input simply by changing the value assigned to the MAXINPUT constant (i.e #define MAXINPUT 80) */
  53.  
  54. if (strlen(argv[1]) > MAXINPUT) {
  55.  
  56. /* If the string legnth exceeds MAXINPUT prompt the user */
  57. getUserInput();
  58. }
  59. else
  60.  
  61. /* Copy the string pointed to by argv[1] into the udtf buffer */
  62. strcpy(udtf, argv[1]);
  63. }
  64. else
  65. /* If no parameters were passed prompt the user */
  66. getUserInput();
  67.  
  68. sprintf(udtf, "%s.%s", udtf, fext);
  69. /* Attempt to open file use the filename now stored in the udtf buffer and show error if unsuccessful. */
  70. if ((fp = fopen(udtf, "r+")) == NULL) {
  71. printf("Error: unable to open file %s", udtf);
  72. return 1;
  73. }
  74.  
  75. fclose(fp); /* Close the file */
  76.  
  77. /* You really shouldn't use goto's they're gay but what-the-hell */
  78. End:
  79. return 0;
  80. }
  81.  
  82. /* This function will be used to prompt the user and get the file name via stdin */
  83. void getUserInput(void)
  84. {
  85. char cinc;
  86. int a = 0;
  87.  
  88. printf("Filename: ");
  89.  
  90. /* While loop getting character by character off stdin using the increment counter a into the global udtf buffer. If you're not aware of what a global variable is, it is a variable that is accessible to all code within the scope of that program. If when the enter key is pressed
  91. or a == MAXINPUT then we want to break the loop and move on. */
  92. while ((cinc = getc(stdin)) != '\n')
  93. {
  94. if (a == MAXINPUT)
  95. break;
  96. udtf[a] = cinc;
  97. a++;
  98. }
  99.  
  100. /* Before we return add a end-of-string character so we don't print out garabage. */
  101. udtf[a]= '\0';
  102. }
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 3,117
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

 
1
  #17
Feb 14th, 2007
Originally Posted by Lazaro Claiborn View Post
  1. /* You really shouldn't use goto's they're gay but what-the-hell */
  2. End:
"Practice what you preach."
If you
1) know you shouldn't use them,
2) then say you shouldn't use them,
3) and do it anyway,
what are you teaching the people that are new to the language? It's confusing because for them, the code is now useless. They don't understand enough to know what to ignore and what is good... FYI...
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: 171
Reputation: Lazaro Claiborn is an unknown quantity at this point 
Solved Threads: 13
Lazaro Claiborn's Avatar
Lazaro Claiborn Lazaro Claiborn is offline Offline
Junior Poster

Re: turning userinput into a text file

 
0
  #18
Feb 14th, 2007
Originally Posted by WaltP View Post
"Practice what you preach."
what are you teaching the people that are new to the language? It's confusing because for them, the code is now useless. They don't understand enough to know what to ignore and what is good... FYI...
I think you should speak for yourself buddy. The code isn't useless BECAUSE of the comments. The reason why I used a goto statement is because it is very intuitive and thus would be easier for a begginer to understand. However, I just wanted to imply that using the goto statement is usually not good depending on the application and/or specifications of a software project - which they're not quite up to, yet, but it is good to keep this in mind becuase instructors discourage its use. I do understand what you say by it might be confusing but even a newbie will know how to distinguish comments from actual code, so, therefore, it is less confusing and more informative - which is what comments are for, right. Last, when you wrote, and I quote, "the code is now useless", well, that just your opinion; one opinion. An absolute newbie, if truely curious, could just google the keyword goto and it'll give a brief, easy-to-understand description of it. So I suppose you're a newbie?!?!

LamaBot
Reply With Quote Quick reply to this message  
Join Date: Apr 2006
Posts: 5,051
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: 332
Team Colleague
John A's Avatar
John A John A is offline Offline
Vampirical Lurker

Re: turning userinput into a text file

 
0
  #19
Feb 14th, 2007
Also, not to be rude, but the code posting was kind of pointless.
  • The code was MAJORLY overcommented.
  • The fix the OP needed was just a matter of one or 2 lines.
  • There was nothing the OP needed to know in your post that hadn't already been shown already.
"Technological progress is like an axe in the hands of a pathological criminal."

All my posts may be freely redistributed under the terms of the MIT license.
Reply With Quote Quick reply to this message  
Join Date: Jan 2007
Posts: 171
Reputation: Lazaro Claiborn is an unknown quantity at this point 
Solved Threads: 13
Lazaro Claiborn's Avatar
Lazaro Claiborn Lazaro Claiborn is offline Offline
Junior Poster

Re: turning userinput into a text file

 
0
  #20
Feb 14th, 2007
Oh if you're a newbie and are looking for a very intuitive C/C++ IDE I recommend Code::Blocks if you were not aware. It is absolutely free for download. Here is the link for the download:

http://www.codeblocks.org/downloads.shtml

You'll see a seciton labeled "Code::Blocks IDE (Windows Binaries)", and if you're using Windows, you'll click the "Code::Blocks IDE with MINGW Compiler" if you need too. Good luck my fellow Daniweb friends

LamaBot
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