• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 261
  • Last Modified:

input history?

I've done a program which output strings from the user using the gets() function. Now, as far as I see, with F3 (under DOS) I can print the last entered string, and with the arrows I can edit the string.

Now, I wish to replace the function with something else, which offer me history options, with the up and down arrows, like in doskey, for example, with the minimum changes to the whole program, if any.

Well, I guess TC/BC doesn't has it, so I wish anyone over here to help. Thanks.
0
tomercohen
Asked:
tomercohen
  • 6
  • 3
  • 2
1 Solution
 
djbusychildCommented:
you'll first need to store however many commands the user enters (you may need to limit this to say 10, etc) then
you can detect the up/down arrow key to traverse that list and output that.

did you want a sample code?
0
 
tomercohenAuthor Commented:
Sure I want to. That's emergency. ;-)
0
 
djbusychildCommented:
0
Creating Active Directory Users from a Text File

If your organization has a need to mass-create AD user accounts, watch this video to see how its done without the need for scripting or other unnecessary complexities.

 
djbusychildCommented:
#include <stdlib.h>
#include <stdio.h>

#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <conio.h>

#define MAX_HISTORY 10
#define MAX_COMMAND_LEN 256

typedef char bool;
#define false 0
#define true 1

int echoCommand(int direction, char* history[],int last_cmd,int num_cmd) {
     if (direction==72) { /* up */
          last_cmd--;
          if (last_cmd<0) { last_cmd=num_cmd-1; }
     } else if (direction==80) { /* down */
          last_cmd++;
          if (last_cmd>(num_cmd-1)) { last_cmd=0; }
     } else { return -1; }

     return last_cmd;
}

int main(int argc,char* argv[])
{
     char *history[MAX_HISTORY]; /* I'm being lazy, you should use a circular queue or something */
     char command[MAX_COMMAND_LEN];
     int last_cmd_index=0;
     int char_buf;
     int num_char=0;
     int num_cmd=0;

     bool command_entered=false;

     memset(command,0,MAX_COMMAND_LEN);
     memset(history,0,MAX_HISTORY*sizeof(char*));

     printf(">> "); /* print cursor */
     while (!_kbhit()) {
               char_buf=_getch();
               switch (char_buf)
               {
                    case 0: /* this is the function key */
                         /* as of now it'll just quite after a function key */
                         
                         break;
                    case 224: /* possibly up,down */
                         last_cmd_index=echoCommand(_getch(),history,last_cmd_index,num_cmd);
                         if (last_cmd_index>=0) {
                              /* wipe out the command line */
                              while (num_char>0) {
                                   printf("\b \b");
                                   num_char--;
                              }
                              num_char=strlen(history[last_cmd_index]);
                              strcpy(command,history[last_cmd_index]);
                              printf("%s",command);
                         } else { last_cmd_index=0; }
                         break;
                    case 8: /* backspace */
                         if (num_char>0) {
                              command[--num_char]='\0';
                              printf("\b \b"); /* back delete back again */
                         }
                         break;
                    case 13: /* newline */
                         if (num_char>0) {
                              num_char=0;
                              if (num_cmd>=MAX_HISTORY) {
                                   num_cmd=0;
                              }
                              if (history[num_cmd]) { /* if there's a command there already, we have to free it */
                                   free(history[num_cmd]);
                                   history[num_cmd]=NULL;
                              }
     
                              history[num_cmd]=(char*)malloc(strlen(command)+1);
                              strcpy(history[num_cmd],command); /* assuming buffer check has already been done by command it self */
                              num_cmd++;
                         }
                         printf("\n>> "); /* print a new cursor */
                         break;
                    default: /* just another key */
                         if ( num_char<(MAX_COMMAND_LEN-1) ) {
                              command[num_char]=char_buf;
                              command[++num_char]='\0'; /* NULL terminate it */
                              printf("%s",&command[num_char-1]); /* just print out the last input */
                         }
                         break;
               }
     }

     printf("\n"); /* end with a carriage return */
}
0
 
djbusychildCommented:
this is a rought, it's not really using a circular queue but just a simple array, so it's not truly circular. I haven't dealt with the situation where you go beyond the history limit and you have to push the others away... Instead I just start overwriting from index 0.

you get the idea, though.
0
 
yabelsonCommented:
as a default,the DOS also has up to 10(if i am not mistaken) commands back.if you press up further,it simply returns you to the last command.this means that if you recieve more hten 10 "up"s you reset your counter,and start all over again.
0
 
yabelsonCommented:
tomer-(fonetic hebrew)tikanes laatar shel rotter,ulai yuchlu laazor lecha sham.yesh sham harbe anashiim shemevinim besi.
0
 
djbusychildCommented:
Hi, did this help at all?
0
 
tomercohenAuthor Commented:
Yabelson, I guess you wrong. DOS has no built-in history options at all, except the F3 key, for the last command. This let me use gets() with only one line of history. In other operating system, this is not the same.

Oh, (in fonetic hebrew) - Ani lo ohev et haatar shel rotter. En sham anashim nechmadim. ;-)
0
 
djbusychildCommented:
tomercohen, did the code help?
0
 
tomercohenAuthor Commented:
djbusychild, not at all, I made it with another way.
Thanks anyway. ;-)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Improve Your Query Performance Tuning

In this FREE six-day email course, you'll learn from Janis Griffin, Database Performance Evangelist. She'll teach 12 steps that you can use to optimize your queries as much as possible and see measurable results in your work. Get started today!

  • 6
  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now