simple scanf issue ?


 
Thread Tools Search this Thread
Top Forums Programming simple scanf issue ?
# 1  
Old 04-06-2007
simple scanf issue ?

Hello everyone,

I hope someone is awake to help me on this..

hey How can I do something like this:

The user is asked is asked to enter an int value, but I want to provide a default value on stdout, which they can back space and change it to whatever they want..

for e.g:

Enter the number of worker threads: 1

so here, the words "Enter the number of threads" is a printf statement. 1 is the default value. But the user should be able to backspace the 1 and enter whatever he wants.

Can i do something like this in C ?

Thank you..
# 2  
Old 04-06-2007
hmm on googling, it seems that there is no such a way !
# 3  
Old 04-06-2007
Actually you can do it in an xterm windowed environment using a textbox widget -but not in straight C ....
# 4  
Old 04-10-2007
It can be done in straight C, although admittedly it is not exactly a piece of cake. I've been working on this all day... it's still a little crude, but it works well enough to show how to do it. I'm too tired to refine further. So...
Code:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <termios.h>

// buffer is an area for the characters to be stored
// max is how big the field is
// init is optional initial value for field
// no 0 will placed after the returned characters
// if + return value is the number of chars returned
// if 0 is returned, user provides no input
// if - user aborted input by typing his intr or quit character
// if user types his kill character the field is reset to init value


int get_field(char *buffer, int max, char *init) {
        struct termios orig, now;
        int col, save_col, i, count, done, abort;
        char *pinit;
        char rdbuff[10], *prdbuff;
        char *dbuff, *pdbuff;
        char *bsbuff1, *pbsbuff1;
        char *bsbuff2, *pbsbuff2;
        char dummy[]="";

// If we get a null pointer for init, replace it with a valid pointer to an empty string.
        if(!init)init=dummy;

// Let's say our field is 10 characters long.  We then grab 30 consecutive bytes.
// Bytes 0 - 9 are bsbuff1 (backspace buffer) and are filled with backspace chars
// Bytes 10 - 19 are dbuff (display buffer) and reflect what is on the screen
// Bytes 20 - 29 are bsbuff2 (backspace buffer) and are filled with backspace chars
// So by carefully picking a starting byte and a length, we can backspace over what
// we have displayed, overwrite the display, and position the cursor in one write
// system call.
// These are not strings...no zero terminator is present.

        bsbuff1 = (char *) malloc(max*3);
        dbuff = bsbuff1+max;
        bsbuff2 = dbuff+max;
        col=0;
        for(i=0, pdbuff=dbuff, pbsbuff1=bsbuff1, pbsbuff2=bsbuff2, pinit=init; i<max; i++,pdbuff++) {
                *pbsbuff1++ = '\b';
                *pbsbuff2++ = '\b';
                if (*pinit) {
                        *pdbuff=*pinit++;
                        col++;
                } else {
                        *pdbuff=' ';
                }
        }
        fflush(stdout);
        write(1,dbuff,2*max-col);
        save_col=col;

// Set up tty
        tcgetattr(0,&orig);
        now=orig;
        now.c_lflag &= ~(ISIG|ICANON|ECHO);
        now.c_cc[VMIN]=10;
        now.c_cc[VTIME]=2;
        tcsetattr(0,TCSANOW, &now);

// Loop to read characters.  We read 10 chars at a time because
// function keys return a sequence of characters and we want to ignore them all.

        abort=0;
        done=0;
        while(!done) {
                count = read(0,rdbuff,10);
                for(i=0, prdbuff=rdbuff; i<count && !done; prdbuff++,i++) {
                        if(*prdbuff  == now.c_cc[VERASE]) {
                                if(col) {
                                        col--;
                                        write(1,"\b \b",3);
                                } else {
                                        write(1,"\a",1);
                                }
                        } else if(*prdbuff  == now.c_cc[VKILL]) {
                                for(i=0, pdbuff=dbuff, pinit=init; i<max; i++,pdbuff++) {
                                        if (*pinit) {
                                                *pdbuff=*pinit++;
                                        } else {
                                                *pdbuff=' ';
                                        }
                                }
                                write(1,bsbuff1+max-col,2*max+col-save_col);
                                col=save_col;
                        } else if (*prdbuff == '\033') {
                                write(1,"\a",1);
                                i=count;
                        } else if (*prdbuff == '\n') {
                                done=1;
                        } else if (*prdbuff == '\r') {
                                done=1;
                        } else if(*prdbuff  == now.c_cc[VINTR]) {
                                done=1;
                                abort=1;
                        } else if(*prdbuff  == now.c_cc[VQUIT]) {
                                done=1;
                                abort=1;
                        } else {
                                write(1,prdbuff,1);
                                dbuff[col++]=*prdbuff;
                        }


                }
        }

// Prepare for return

        tcsetattr(0,TCSANOW, &orig);
        free(bsbuff1);

// Return to caller

        if(abort) {
                return -1;
        } else {
                for(i=0; i<col; i++) {
                        buffer[i]=dbuff[i];
                }
                return col;
        }
        return 0;
}



int main(int argc, char *argv[])  {

        char ibuf[80];
        int iret;
        fputs("generic prompt -", stdout);
        if ((iret = get_field(ibuf, 10, "123" )) > 0) {
                ibuf[iret]=0;
                printf("\n ibuf is now \"%s\"  %d\n", ibuf, iret);
        } else if (iret == 0) {
                printf("\n no input \n");
        } else {
                printf("\n input was aborted\n");
        }
        exit(0);
}

# 5  
Old 04-11-2007
You can implement this in similar 'standard' way, e.g

Enter the number of worker threads (1):

If user enters nothing default value (1) is used.


Quote:
Originally Posted by the_learner
Hello everyone,

I hope someone is awake to help me on this..

hey How can I do something like this:

The user is asked is asked to enter an int value, but I want to provide a default value on stdout, which they can back space and change it to whatever they want..

for e.g:

Enter the number of worker threads: 1

so here, the words "Enter the number of threads" is a printf statement. 1 is the default value. But the user should be able to backspace the 1 and enter whatever he wants.

Can i do something like this in C ?

Thank you..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Simple date issue

Hi , Here is the smaller version of the problem. Working individually as command ************************>echo $SHELL /bin/bash ************************>TO_DAY=`date` ************************>echo $TO_DAY Tue Jul 16 02:28:31 EDT 2013 ************************> Not working when... (5 Replies)
Discussion started by: Anupam_Halder
5 Replies

2. Shell Programming and Scripting

Simple issue, what is wrong?

I had this working a few days ago but I since changed it. Heres the code x=1 while 1 2 3 4 5 6 1=$(ps -ef | grep process | awk '{ print $2}') if then echo "The database is accepting connections..." echo "Now I will check the next process" 2=$(ps -ef | grep process1 |... (10 Replies)
Discussion started by: jeffs42885
10 Replies

3. Shell Programming and Scripting

Simple SED issue

Hi! I'm a newbie and can't find the exact sed to make this work. I've installed CentOS and am trying to pass variables to a network-config file. variables: $ipAddress $netmask $gateway file: DeviceList.Ethernet.eth0.IP=192.168.1.10 DeviceList.Ethernet.eth0.Netmask=255.255.255.0... (5 Replies)
Discussion started by: greipr
5 Replies

4. Programming

Better than scanf

I don't know how to do this: printf("creazione nuovo messaggio\n"); printf("insert dest\n"); scanf("%s",dest); printf("insert object\n"); scanf("%s",ogg); printf("inserire text\n"); scanf("%s",test); ... (7 Replies)
Discussion started by: italian_boy
7 Replies

5. Shell Programming and Scripting

Simple Cut issue

I have a long string that looks something like this.... <string>http://abc.com/40/20/zzz061-3472/dP3rXLpPPV2KC846nJ4VXpH7jt4b3LJgkL/tarfile_date.tar</string> I need to but the tar file name. So I need to put between "/" and ".tar</string>". My desired result should be "tarfile_date". (7 Replies)
Discussion started by: elbombillo
7 Replies

6. Shell Programming and Scripting

Simple date issue

Hi all, i have used the search already before someone shouts at me and i have seen the 'datecalc' program but this is not working correctly for me in the shell and environment i am using. I am using solaris 10 and bourne shell. I have two dates '07-04-2009' and '05-05-2009'. I just need to... (2 Replies)
Discussion started by: muay_tb
2 Replies

7. UNIX for Advanced & Expert Users

simple issue..

I have a program. Everytime that I run that program ,it prints a disclaimer msg and prompts the user to accept the agreement (i.e: type 'y' or 'n'). Now I want to run the program multiple times with different inputs. So I wrote a script which runs in a loop and calls the program multiple times.... (2 Replies)
Discussion started by: the_learner
2 Replies

8. Programming

problem with scanf

hi all! i've written a simple c program: #include<stdio.h> #include<stdlib.h> int main() { int a; char b; char c; ... (4 Replies)
Discussion started by: mridula
4 Replies

9. Programming

scanf with strings... please help

hi i am a beginner to C i have encountered a problem with my assignment, and i have researched it on the internet, but unfortunately i didn't find anything related to that. i am writing a simple program that takes user's input by prompt command, and parse the whole line into an array of... (1 Reply)
Discussion started by: inkfish
1 Replies

10. Programming

Scanf problem under LINUX...

I have a problem reading characters from keyboard with the scanf function. Here there is a little piece of code: #include <stdio.h> #include <sys/types.h> #include <sys/socket.h> #include <netinet/in.h> /* The last 3 libraries are included because in the real program I use some... (4 Replies)
Discussion started by: robotronic
4 Replies
Login or Register to Ask a Question