#1
  1. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2003
    Posts
    7
    Rep Power
    0

    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?
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2002
    Posts
    56
    Rep Power
    13
    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
  4. #3
  5. jasondoucette.com
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2003
    Location
    Canada
    Posts
    378
    Rep Power
    12
    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. :)
  6. #4
  7. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,145
    Rep Power
    2222
    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/de...crt_rewind.asp ] and the VC++ 1.52 help file say about it:
    To clear the keyboard buffer, use rewind with the stream stdin, which is associated with the keyboard by default.
    and
    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:
    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.

    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".

    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.
  8. #5
  9. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2003
    Posts
    7
    Rep Power
    0
    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/de...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.
  10. #6
  11. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2003
    Posts
    17
    Rep 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.

IMN logo majestic logo threadwatch logo seochat tools logo