turning userinput into a text file

Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Reply

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
  #21
Feb 14th, 2007
Originally Posted by Lazaro Claiborn View Post
The reason why I used a goto statement is because it is very intuitive and thus would be easier for a begginer to understand.
Newbies should generally not use goto statements at all. It's not that they're completely bad; in fact, they can be used in many situations where it can significantly speed up the program or remove the need to use lots of code.

However, gotos are still statements that you have to be careful with. And generally a newbie shouldn't be using them, because they don't know "proper use" of the statement. An expert will know when it's necessary, and will only use it at the right time.

So I suppose you're a newbie?!?!
I can't even comment on this one... :rolleyes:
"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
  #22
Feb 14th, 2007
Originally Posted by joeprogrammer View Post
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.
It seems you're being defensive which is common. I wrote that program in 3 minutes, not to brag. YOU can say that its majorly over commented but that is just YOUR opinion. Everyone is different. Frankly, you're words are futile and everything you'd said so far indicates that you're a newbie yourself. I guess I could of held back on the comments... and let the newbie try to understand just by reading the code, yes, I could of done that. I'm not here for debates, I'm here to help the begginners. I was a begginner once, everyone is, and I got help from online forums and etc. Let me state the obvious once more, the code I wrote was a C-O-N-T-R-I-B-U-T-I-O-N and the originator of this thread, or anyone who reads it, can take it or leave it. I just hope it'll give someone some incentive or 'flip on the light' for new ideas derived in anyway from that program. FYI, I've seen many debates on usage of comments, just like the one you seem like you want to start and I've realized that if you're a programmer for some buisness writing a library or an individual learning, you've got to consider the circumstances. If you're a senior programmer for a buisness and the buisness regulates comment usage in source code then it is ok, but if you're a newbie, therefore probably not working for a buisness, yet, or just in school, comments support and sometimes facilitate the learning experience. Anyway I hope I've helped out a little my fellow Daniweb friends.

LamaBot
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
  #23
Feb 14th, 2007
Originally Posted by joeprogrammer View Post
Newbies should generally not use goto statements at all. It's not that they're completely bad; in fact, they can be used in many situations where it can significantly speed up the program or remove the need to use lots of code.

However, gotos are still statements that you have to be careful with. And generally a newbie shouldn't be using them, because they don't know "proper use" of the statement. An expert will know when it's necessary, and will only use it at the right time.
That is weird, you've inadvertentely called many of my past programming instructors thier teaching methods are skewed. Hmm.... I guess you're one hell of a teacher then huh. Of course goto statements are speedy, they essentially change the address of the instruction register to that of the label. I do agree that it takes an expert to know when it is necessary or the right time, trust me, I've seen some buggy code. Nevertheless, goto statements are OK for a newbie to learn first because it is, as I said before, easy to understand and intuitive. I figured they'd eventually learn that it is generally not good programming practice to use them unless you know what you're doing - which is why I add that comment you were so bothered about.

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
  #24
Feb 14th, 2007
Originally Posted by Lazaro Claiborn View Post
It seems you're being defensive which is common.
If you'll look back on the post that I wrote about your code, it was actually quite near the time that you posted yours. I wrote my post as you were writing yours; so I was not being defensive in the least.

I wrote that program in 3 minutes, not to brag. YOU can say that its majorly over commented but that is just YOUR opinion.
I never said you were trying to brag. And I do realize that this is simply my opinion (I was a bit harsh, sorry), and everyone shares different viewpoints.

Frankly, you're words are futile and everything you'd said so far indicates that you're a newbie yourself.
Meaning which words? My viewpoint on comments? An opinion does not make you a newbie, and I never called you a newbie based on my disagreeing opinion with you. Essentially everyone must learn to accept different viewpoints, or else we'd constantly be having a civil war.

Let me state the obvious once more, the code I wrote was a C-O-N-T-R-I-B-U-T-I-O-N and the originator of this thread, or anyone who reads it, can take it or leave it.
I understand that. Thanks for posting it.

However, my main point was that the problem had almost been solved. It was just a matter of using double quotes instead of single quotes, and using the correct variable.

By posting the entire code, the OP has to read all the comments and the code, which is tremendously long. I'm not saying that this is a totally incorrect thing to do, but it might be easier if you could simplify your code a bit? Adding lots of comments does not necessarily make code easy to understand.

I just hope it'll give someone some incentive or 'flip on the light' for new ideas derived in anyway from that program.
I appreciate and applaud your insight on posting this.

Originally Posted by Lazaro Claiborn View Post
That is weird, you've inadvertentely called many of my past programming instructors thier teaching methods are skewed.
Not surprising; there are many instructors who teach skewed coding methods. Don't get me wrong, I'm not trying to bash your teachers. It all depends on the problem.

In this example, it would be simply easier just to return from main. There's no need to jump to the end of the program with a goto statement. Or you could simply use an if() statement, which although might complicate the program a little; the complexity could be reduced by splitting the various snippets of code into functions, which are then called from the main() function.
"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
  #25
Feb 15th, 2007
Originally Posted by joeprogrammer View Post

I appreciate and applaud your insight on posting this.


Not surprising; there are many instructors who teach skewed coding methods. Don't get me wrong, I'm not trying to bash your teachers. It all depends on the problem.

In this example, it would be simply easier just to return from main. There's no need to jump to the end of the program with a goto statement. Or you could simply use an if() statement, which although might complicate the program a little; the complexity could be reduced by splitting the various snippets of code into functions, which are then called from the main() function.
I can respect that. And I'm sorry if I was subletly harsh too. :lol: I guess I should consider your view point. I'm sorry for implying you were a newbie. Overall, I guess I can appreciate and applause your insight and opinion as well. Basically confirming what I'd said, many teachers do have skewed teaching methods but depends on the problem or circumstances, in essence. It was necessary, however, for me to give all the code or else it'd truely be useless, at least to a newbie :lol: You're forgetting that even a newbie can Copy-and-paste the code into his or her IDE Window. He or she can cut or delete comments thereof. The format looks like crap because of the limited horizontal character space. You should of seen it when I just let one line wrap to another, it look confusing! For pete's sake, I'll say it again, I wrote that program in response to the originator of this thread in like three minutes. Yes I could of just return from main instead of jumping to the end. My view point obviously differs from yours so I put the goto statement in there for a reason :surprised Also I could of used some if statements among other ways, and used more functions but I figured for a newbie it'd better be as simple as possible. For my last post, I'll give the code without the comments.

The code:
  1. #include <stdio.h>
  2. #include <string.h>
  3.  
  4. #define MAXINPUT 100
  5.  
  6. void getUserInput(void);
  7.  
  8. char udtf[MAXINPUT];
  9.  
  10. const char fext[] = "txt";
  11.  
  12. static inline void _header(void)
  13. {
  14.  
  15. puts("UDTF (User Define Text Filename) version 1.0");
  16.  
  17. 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");
  18.  
  19. printf("For usage \ninformation pass the -h parameter to the program.\n\n");
  20.  
  21. }
  22.  
  23. static inline void _usage(const char *prog_name)
  24. {
  25.  
  26. printf("%s -h Show usage help information\n", prog_name);
  27. printf("%s filename", prog_name);
  28.  
  29. }
  30.  
  31. int main(int argc, char *argv[])
  32. {
  33.  
  34. FILE *fp;
  35.  
  36. _header();
  37.  
  38. if (argc > 1)
  39. {
  40.  
  41. if (!strcmp(argv[1], "-h"))
  42. {
  43.  
  44. _usage(argv[0]);
  45. goto End;
  46.  
  47. }
  48.  
  49. if (strlen(argv[1]) > MAXINPUT)
  50. getUserInput();
  51. else
  52. strcpy(udtf, argv[1]);
  53.  
  54. }
  55. else
  56. getUserInput();
  57.  
  58. sprintf(udtf, "%s.%s", udtf, fext);
  59.  
  60. if ((fp = fopen(udtf, "r+")) == NULL)
  61. {
  62.  
  63. printf("Error: unable to open file %s", udtf);
  64. return 1;
  65.  
  66. }
  67.  
  68. fclose(fp);
  69.  
  70. End:
  71. return 0;
  72. }
  73.  
  74. void getUserInput(void)
  75. {
  76. char cinc;
  77. int a = 0;
  78.  
  79. while ((cinc = getc(stdin)) != '\n')
  80. {
  81.  
  82. if (a == MAXINPUT)
  83. break;
  84.  
  85. udtf[a] = cinc;
  86. a++;
  87.  
  88. }
  89.  
  90. udtf[a]= '\0';
  91. }
That is very basic programming and no new concept incorporated.

LamaBot
Last edited by WaltP; Feb 15th, 2007 at 3:50 am. Reason: Added the tags...
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
  #26
Feb 15th, 2007
You forgot the code tags.

One trick if you've got comment-heavy code is to use DaniWeb's built-in code coloring tags, [code=c] or [code=cplusplus]. It can work wonders.
"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
  #27
Feb 15th, 2007
Originally Posted by joeprogrammer View Post
You forgot the code tags.

One trick if you've got comment-heavy code is to use DaniWeb's built-in code coloring tags, [code=c] or [code=cplusplus]. It can work wonders.
I know, I'm just as lazy on Actionscript.org :o I'll make sure I use them from now on as I should. Thanks buddy.

  1. /* This function will be used to prompt the user and get the file name via stdin */
  2. void getUserInput(void)
  3. {
  4. char cinc;
  5. int a = 0;
  6.  
  7. /*
  8. While loop getting character by character off stdin using the
  9. 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
  10. or a == MAXINPUT then we want to break the loop and move on.
  11. */
  12. while ((cinc = getc(stdin)) != '\n')
  13. {
  14. if (a == MAXINPUT)
  15. break;
  16. udtf[a] = cinc;
  17. a++;
  18. }
  19.  
  20. /* Before we return add a end-of-string character so we don't print out garbage. */
  21.  
  22. udtf[a]= '\0';
  23. }

Hey it looks good for once! Hope I've helped 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