Thread: Moving a character to stdin?

  1. #1
    Registered User
    Join Date
    Feb 2003
    Posts
    265

    Moving a character to stdin?

    Okeydokey, long story short, i havent screwed around in C in QUITE a while. After i discovered the wonderful joys of the STL i never touched it again and thought it an evil archaic thing to deal with, however im faced with editing a small part of a program and im trying to move a single character from one place to another (the most trivial tasks seem the most complicated, dont they?)

    A nice excerpt from the 1000+ lines of code in this file:
    Code:
    ...
            if (ch == EOF_CHAR) done(0);
    
            /* Do Table Commands */
            for (ccmd= cmd_char; ccmd->cmd != 0; ccmd++)
              /*RATBAIT WAS HERE added ||...) TO ADD DEFAULT COMMAND STATE OF 1!*/
               if (ch == ccmd->cmd || ccmd->cmd == 1)
               {
                    /*RATBAIT WAS HERE added entire line TO NOT REMOVE PRESSED KEY AS PROMPT*/
                    if (ccmd->cmd == 1) { ccmd->prompt = ch; }
                    /* Check if command is enabled */
                    if (ccmd->enable == OPT_NONE || opt[ccmd->enable].yes)
                    {
                        /* Print the prompt */
                        STTY(0,&cooked);
                        putc(ccmd->prompt,stderr);
    
                    if (ccmd->cmd == 1) { /*MAGIC OCCOURS HERE*/ }
    
                        /* Read the command */
                        if (fgets(txbuf,BFSZ,stdin))
                            /* Execute the command */
                            (*ccmd->func)(txbuf);
                        else
                        {
                            /* Input aborted */
                            fputs(cancel,stderr);
                            clearerr(stdin);
                        }
                        STTY(0,&cbreak);
                    }
                    else if (ccmd->failmsg != NULL)
                        err(ccmd->failmsg);
                    else
                        speak(ch);
                    return;
               }
    ...
    Ok, there is a nice comment in there MAGIC OCCOURS HERE. Its the line above where we FGETS to read their input, and im trying to stick the character ch, or ccmd->prompt in FRONT of their input. Now its fine to stick it into stdin before their input, or concatinate their txbuf onto the end of it, or whatever. I just need a way to do this (note its 4am and i have been screwing around in console programming from pico for the last few hours over the world slowest modem to a terminal halfway across the country so my brain isnt working well, and i cant remember how to concatinate character to character * array thingy and i need aspirine) How do i concatinate to do what i want. (English shut off about an hour ago, plz bare with.)

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Well for strings, just use the strcat function.

    If you have a buffer that you want to append to, and you're just adding a single character, then just add it into the array (assuming this isn't just some sized block of dynamicly allocated memory). Like so:
    Code:
    char buf[BUFSIZ] = {0};
    int len;
    
    ...stuff...
    
    len = strlen( buf );
    if( len < BUFSIZ-2 )
    {
        buf[len] = the_char;
        buf[len+1] = '\0';
    }
    If it's dynamicly allocated memory, of which there is no free room on what you've allocated, then you need to realloc some memory, with enough room to add the letter, and free the old stuff.

    But basicly it sounds like you just want to add a character to a buffer before you display it, so just seek to the end of the used space in the buffer and add it on. Then make sure you have nulls on the end.

    [edit]
    If you were just reading characters off a file stream and you wanted to stick one back on (ie: unread it) you'd use ungetc.
    [/edit]

    Quzah.
    Last edited by quzah; 09-26-2003 at 04:32 AM.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Feb 2003
    Posts
    265
    Bleh thanks for the function. ungetc( ch, stdin ); is exactly what i was looking for. Im constantly reading, echoing, transmitting, and accepting more imput and i need to peek at stdin rather than irrevocably accept input from it so i can take the first character and print it as a prompt, while at the same time use it to figure out what they are looking for. I know im vague and irritating, but i passed my english 2 years ago and can completely focus on coding C on an archaic router. BLEH! lusers dont need an interface! im working on the part that auto-searches based on the first few letters of the command. like you want to run the command asdf. after the A it limits it to the A commands, second letter, third letter, and when you finally get a 100% match it finishes the command for you, auto complete, and then gives you a list of parameters it takes.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sequential file program
    By needhelpbad in forum C Programming
    Replies: 80
    Last Post: 06-08-2008, 01:04 PM
  2. Interpreter.c
    By moussa in forum C Programming
    Replies: 4
    Last Post: 05-28-2008, 05:59 PM
  3. get wide character and multibyte character value
    By George2 in forum C++ Programming
    Replies: 27
    Last Post: 01-27-2008, 05:10 AM
  4. pipe
    By smart girl in forum C Programming
    Replies: 4
    Last Post: 04-30-2006, 09:17 AM
  5. Moving ASCII Character
    By Ranedhel in forum Game Programming
    Replies: 10
    Last Post: 07-17-2003, 06:58 AM