| | |
turning userinput into a text file
Please support our C advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
•
•
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, 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?!?!
"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.
All my posts may be freely redistributed under the terms of the MIT license.
•
•
•
•
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.
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
•
•
•
•
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.
LamaBot
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 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.
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. 
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 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 wrote that program in 3 minutes, not to brag. YOU can say that its majorly over commented but that is just YOUR opinion.
•
•
•
•
Frankly, you're words are futile and everything you'd said so far indicates that you're a newbie yourself.

•
•
•
•
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.
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.
•
•
•
•
That is weird, you've inadvertentely called many of my past programming instructors thier teaching methods are skewed.
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.
All my posts may be freely redistributed under the terms of the MIT license.
•
•
•
•
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.
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:
c Syntax (Toggle Plain Text)
#include <stdio.h> #include <string.h> #define MAXINPUT 100 void getUserInput(void); char udtf[MAXINPUT]; const char fext[] = "txt"; static inline void _header(void) { puts("UDTF (User Define Text Filename) version 1.0"); 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"); printf("For usage \ninformation pass the -h parameter to the program.\n\n"); } static inline void _usage(const char *prog_name) { printf("%s -h Show usage help information\n", prog_name); printf("%s filename", prog_name); } int main(int argc, char *argv[]) { FILE *fp; _header(); if (argc > 1) { if (!strcmp(argv[1], "-h")) { _usage(argv[0]); goto End; } if (strlen(argv[1]) > MAXINPUT) getUserInput(); else strcpy(udtf, argv[1]); } else getUserInput(); sprintf(udtf, "%s.%s", udtf, fext); if ((fp = fopen(udtf, "r+")) == NULL) { printf("Error: unable to open file %s", udtf); return 1; } fclose(fp); End: return 0; } void getUserInput(void) { char cinc; int a = 0; while ((cinc = getc(stdin)) != '\n') { if (a == MAXINPUT) break; udtf[a] = cinc; a++; } udtf[a]= '\0'; }
LamaBot
Last edited by WaltP; Feb 15th, 2007 at 3:50 am. Reason: Added the tags...
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.

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.
All my posts may be freely redistributed under the terms of the MIT license.
•
•
•
•
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.
c Syntax (Toggle Plain Text)
/* This function will be used to prompt the user and get the file name via stdin */ void getUserInput(void) { char cinc; int a = 0; /* 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 or a == MAXINPUT then we want to break the loop and move on. */ while ((cinc = getc(stdin)) != '\n') { if (a == MAXINPUT) break; udtf[a] = cinc; a++; } /* Before we return add a end-of-string character so we don't print out garbage. */ udtf[a]= '\0'; }
Hey it looks good for once! Hope I've helped my fellow Daniweb friends.
LamaBot
![]() |
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 arrays bash binarysearch centimeter char convert copyanyfile copypdffile cprogramme createcopyoffile createprocess() csyntax directory dynamic fflush file floatingpointvalidation fork frequency getlasterror getlogicaldrivestrin givemetehcodez global graphics gtkgcurlcompiling hardware highest homework i/o ide inches infiniteloop initialization interest kilometer km linked linkedlist linux linuxsegmentationfault list locate logical_drives match matrix meter microsoft motherboard multi mysql odf open opendocumentformat opensource openwebfoundation owf pattern pdf performance pointer pointers posix power probleminc program programming pyramidusingturboccodes read recursion recv repetition scanf scheduling segmentationfault send shape single socketprograming socketprogramming stack standard strchr string strings structures suggestions systemcall test testautomation unix urboc user voidmain() wab win32api windows.h






