|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
Get inside! Sample the range of functionality easily built with JMSL Library for Time Series Data Analysis, Heat Maps, Portfolio Optimization, Monte Carlo Simulation, Stock Price Charting and more. Download Now! |
|
#1
|
|||
|
|||
|
Code:
void consoleInput()
{
char arg[25] = {0};
while(strcmp(arg,"quit"))
{
rewind(stdin);
gets(arg);
}
}
Would that be a valid, functioning, dedicated basis for text inputing for a DOS-like command prompt? |
|
#2
|
|||
|
|||
|
that says that while 0 != "quit", it will 'rewind' 'stdin' and read 'arg' which is 0
i'm into C++, not C and i'm not very good at it so please answer my stupid questions: 1. what is "rewind"?, it's not listed as a function in my C/C++ book as a function...... 2. since 0 is never equal to "quit", will this code not just constantly read the value 0 over and over again? 3. are you sure you can perform a function on a function? stdin is a function itself, not a variable of any kind. IMO this code wouldn't work but keep in mind that i suck at this lol.
__________________
PHP is fun
|
|
#3
|
||||
|
||||
|
rewind
Repositions the file pointer to the beginning of a file. void rewind( FILE *stream ); it requires: <stdio.h> and is ANSI compatible I didn't know it existed either. Must be left over from the days of Fortran. ![]()
__________________
Jason Doucette / Xona.com™ - Programming Windows Errata Addendum "Discussion is an exchange of knowledge; argument is an exchange of ignorance." |
|
#4
|
|||||||
|
|||||||
|
Ghetalion:
One of the reasons I hang around here is that I am learning a lot from everybody. For example, I already knew about the rewind() function, but I never would have thought of applying it to stdin. When I saw that line of code, I thought it was an error. But here is what the MSDN site [http://msdn.microsoft.com/library/d..._crt_rewind.asp ] and the VC++ 1.52 help file say about it: Quote:
and Quote:
Also, I've used fgets() several times, but almost never gets(), so I immediately thought that you needed to remove the newline ('\n') before doing the compare. Wrong. fgets() reads in the newline character, but gets() removes it automatically. However, strcmp is case-sensitive, so if the user types in "QUIT", your code will not recognize it. You might want to use stricmp() if you want to be case-insensitive. ShawnD: Quote:
rewind() is a standard C stream I/O function which repositions to the beginning of the file. As Jason suggested, the name appears to be a carry-over from the tape-drive days, much as the standard file-archive command, tar, stands for "tape archive" and will output to the tape drive (/dev/rmt0) if you don't use the f option to specify an output file. VC++ 1.52 help, the MSDN site, and the fseek man page all say that rewind() is equivalent to (void) fseek( stream, 0L, SEEK_SET ); except that rewind clears the error indicators while fseek does not. Since C++ supports C, rewind() is in C++ along with the other functions in stdio.h . However, I could not find it in C++ iostreams. To do the same thing, it looks like you would have to use the seekg() method as fseek is used above. Quote:
char arg[25] = {0}; is an initialization array in which only the first character, NULL ('\0') is given. Hence arg is being initialized to an empty string. The more common and IMHO clearer way to do it is: char arg[25] = ""; So in the loop, gets() will load the input string into arg, which could be a string of "quit". Quote:
stdin is not a function, but a file descriptor. Consider it to be of type (FILE*). One of the ideas that Bill stole (excuse me -- "researched") from UNIX are the three files that every console application has opened for it automatically: standard input (stdin -- by default from the keyboard), standard output (stdout -- by default to the screen), and standard error (stderr -- by default to the screen). They can be redirected and piped from the command line (though stderr can be tricky to do under DOS) and there are some tricks you can do with them within the program, but which I'm not up on yet. |
|
#5
|
|||
|
|||
|
Quote:
Thanks, folk. Most appreciative. |
|
#6
|
|||
|
|||
|
im working on something similar using STL. i needed a command prompt too. my main function is a forever loop like this:
int main() { loadOperations(); // load operation table while (true) // loop indefinitely { std::string line; // holds command line input line = cmdPrompt(); // prompt and interpret commands cmdInterpret(line); } return EXIT_SUCCESS; // successfully finished execution } then the cmdPrompt function is: string cmdPrompt() { string line(""); // holder for line input cout << ">> "; // present command prompt getline(cin,line); // get input return line; } the cmdInterpret function does the real work but the point is that you could interpret the function by e.g.: 1) convert the string to all lower case 2) check against 'quit' [i.e. if (line == quit){ exit(0);}] 3) use the exit command to quit the program the only issue i have with this is that i cant have much of an interactive command line. id like to have autocompletion and command history but if youre looking for a place to start which is easy to deal with this may help. |
![]() |
| Viewing: Dev Shed Forums > Programming Languages > C Programming > Basic Text Console needs Verification |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|