C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming LanguagesC Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
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  
Old March 25th, 2003, 08:47 PM
Ghetalion Ghetalion is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2003
Posts: 7 Ghetalion User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Send a message via AIM to Ghetalion
Post Basic Text Console needs Verification

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?

Reply With Quote
  #2  
Old March 26th, 2003, 02:06 AM
ShawnD ShawnD is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2002
Posts: 56 ShawnD User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 7
Send a message via ICQ to ShawnD
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

Reply With Quote
  #3  
Old March 26th, 2003, 10:45 AM
Jason Doucette's Avatar
Jason Doucette Jason Doucette is offline
jasondoucette.com
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2003
Location: Canada
Posts: 378 Jason Doucette User rank is Private First Class (20 - 50 Reputation Level)Jason Doucette User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 7 h 23 m 8 sec
Reputation Power: 6
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.

Reply With Quote
  #4  
Old March 26th, 2003, 01:51 PM
dwise1_aol's Avatar
dwise1_aol dwise1_aol is offline
Contributing User
Dev Shed Expert (3500 - 3999 posts)
 
Join Date: Jan 2003
Location: USA
Posts: 3,803 dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level) 
Time spent in forums: 1 Month 11 h 55 m 53 sec
Reputation Power: 437
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:
To clear the keyboard buffer, use rewind with the stream stdin, which is associated with the keyboard by default.

and
Quote:
You can also use the rewind function to clear the keyboard buffer. Use the rewind function with the stream stdin, which is associated with the keyboard by default.


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:
Originally posted by ShawnD
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......

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:
2. since 0 is never equal to "quit", will this code not just constantly read the value 0 over and over again?

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:
3. are you sure you can perform a function on a function? stdin is a function itself, not a variable of any kind.

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.

Reply With Quote
  #5  
Old March 26th, 2003, 03:43 PM
Ghetalion Ghetalion is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2003
Posts: 7 Ghetalion User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Send a message via AIM to Ghetalion
Quote:
Originally posted by dwise1_aol
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:

and


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.


Thanks, folk. Most appreciative.

Reply With Quote
  #6  
Old March 31st, 2003, 03:38 PM
antarctic antarctic is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2003
Posts: 17 antarctic User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
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.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > Basic Text Console needs Verification


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 3 hosted by Hostway