| | |
turning userinput into a text file
Please support our C advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Jan 2007
Posts: 27
Reputation:
Solved Threads: 0
but i though the entry in fopen has to be a either in "" or predefined?
c Syntax (Toggle Plain Text)
#include <stdio.h> #include <string.h> #include <ctype.h> #define INFILE "questions.txt" int main(int argc, char *argv[]) { char mc; char rt; char filename[80]; char line[255]; FILE *fin,*fout,*fid; fin = fopen(INFILE, "r"); printf("enter student ID:"); fgets(line, 255, stdin); sprintf(filename,"%s.txt",line); fout = fopen(filename, "w"); return 0; }
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.
fgets() function leaves the '\n' in the input buffer, so you need to truncate it
c Syntax (Toggle Plain Text)
printf("enter student ID:"); fgets(line, 255, stdin); // truncate '\n' if( line[strlen(line)-1] == '\n') line[strlen(line)-1] = '\0'; 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.
•
•
•
•
but i though the entry in fopen has to be a either in "" or predefined?

•
•
•
•
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
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
•
•
•
•
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);
C Syntax (Toggle Plain Text)
#include <stdio.h> #include <string.h> #define MAXINPUT 100 /* Function used if the user didn't pass program parameter */ void getUserInput(void); /* udtf (user defined text filename) is the global character variable buffer. */ char udtf[MAXINPUT]; /* Create a constant character array holding the file extentions - you can change the saved file type simply by changing this initialization */ const char fext[] = "txt"; /* 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 or they'll probably never know how. */ 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"); } /* 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. */ 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[]) { /* 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. */ FILE *fp; /* Show program header - refer above for an explaination */ _header(); /* 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. */ if (argc > 1) { /* Check if the user requests usage information */ if (!strcmp(argv[1], "-h")) { /* The program name is pointed to by argv[0] - the first element of the array */ _usage(argv[0]); goto End; } /* 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. You can change the max input simply by changing the value assigned to the MAXINPUT constant (i.e #define MAXINPUT 80) */ if (strlen(argv[1]) > MAXINPUT) { /* If the string legnth exceeds MAXINPUT prompt the user */ getUserInput(); } else /* Copy the string pointed to by argv[1] into the udtf buffer */ strcpy(udtf, argv[1]); } else /* If no parameters were passed prompt the user */ getUserInput(); sprintf(udtf, "%s.%s", udtf, fext); /* Attempt to open file use the filename now stored in the udtf buffer and show error if unsuccessful. */ if ((fp = fopen(udtf, "r+")) == NULL) { printf("Error: unable to open file %s", udtf); return 1; } fclose(fp); /* Close the file */ /* You really shouldn't use goto's they're gay but what-the-hell */ End: return 0; } /* This function will be used to prompt the user and get the file name via stdin */ void getUserInput(void) { char cinc; int a = 0; printf("Filename: "); /* 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 garabage. */ udtf[a]= '\0'; }
•
•
•
•
C Syntax (Toggle Plain Text)
/* You really shouldn't use goto's they're gay but what-the-hell */ End:
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
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
•
•
•
•
"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...
LamaBot
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.
All my posts may be freely redistributed under the terms of the MIT license.
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
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
![]() |
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






