PDA

View Full Version : c++ modifiable output



cplusrun
April 14th, 2009, 11:26 PM
I'm trying to make a program that let's a user modify an output. Here's what it would look like

Employee's name: | Employee |

Then if they want they can erase a field

Employee's name: | |

Then retype it:

Employee's name: | New name |

If anyone could point me in the direction of the function I would use for this I would appreciate it. Thanks.

cabalas
April 14th, 2009, 11:32 PM
I assume from the way your description that this is meant to be a command line app, to do something like this you will probably be wanting something like ncurses to handle the ui, a quick google should turn up some useful links.

Krupski
April 15th, 2009, 12:10 AM
I'm trying to make a program that let's a user modify an output. Here's what it would look like

Employee's name: | Employee |

Then if they want they can erase a field

Employee's name: | |

Then retype it:

Employee's name: | New name |

If anyone could point me in the direction of the function I would use for this I would appreciate it. Thanks.

Something like this:



#include <stdio.h>
#include <string.h>

int readln(FILE*, char*);

int main(void)
{
char buf[BUFSIZ];
char name[BUFSIZ];

strcpy(name, "John Smith");

while(1) {
fprintf(stdout, "Employee's name: %s\rEmployee's name: ", name);
readln(stdin, buf);
if(buf[0] == 0) { break; }
strcpy(name, buf);
}

fprintf(stdout, "\nName is now: %s\n", name);
return 0;
}


int readln(FILE *fp, char *str)
{
char buffer[BUFSIZ];
int len;

buffer[0] = 0; fgets(buffer, BUFSIZ, fp);

len = strlen(buffer);

while(len >= 0) {
if( buffer[len] == 32 ||
buffer[len] == 13 ||
buffer[len] == 10 ||
buffer[len] == 9 ||
buffer[len] == 0) { buffer[len] = 0; len--; }
else { break; }
}

len++;

buffer[len] = 0;

strcpy(str, buffer);

return len;
}



???

cplusrun
April 15th, 2009, 12:18 AM
This is very close to what I'm looking for Krupski, only problem is that the cursor starts before the word, and Delete key doesnt work. I'm taking a look at the code to see how to change this. Thank you.

And thanks for your post too cabalas, I'll definitely check out curses

Krupski
April 15th, 2009, 12:21 AM
This is very close to what I'm looking for Krupski, only problem is that the cursor starts before the word, and Delete key doesnt work. I'm taking a look at the code to see how to change this.

Well, obviously that code is a very rough and unfinished piece of programming. It's only to get you started in (hopefully) the right direction.

Good luck!

-- Roger

cplusrun
April 15th, 2009, 12:23 AM
Well I shall attempt to finish it then ;) I'll repost my solution later for anyone who's searching for how to do this. Thanks for your help!

cplusrun
April 15th, 2009, 12:56 AM
Hmm... looking deeper into that code I found that the only reason it looks like it's able to manipulate the output is because of that \r in the original fprintf. Other than that there doesnt seem to be a way to actually edit the original string. Thanks anyway Krupski! I'll go check out the curses library, see if theres anything that'll help me there

Arndt
April 15th, 2009, 09:01 AM
I'm trying to make a program that let's a user modify an output. Here's what it would look like

Employee's name: | Employee |

Then if they want they can erase a field

Employee's name: | |

Then retype it:

Employee's name: | New name |

If anyone could point me in the direction of the function I would use for this I would appreciate it. Thanks.

The man page tty_ioctl mentions the ioctl TIOCSTI (simulate terminal input). So the program would output the prompt and the value of the field, but also insert the characters in the input buffer, from which they can be erased by the user, or accepted. I don't think they are echoed when inserted, but maybe they are.

I don't recommend this. If nothing else, the program will probably behave strangely if given input from a pipe.

cplusrun
April 16th, 2009, 04:46 AM
Thank you Arndt, tried looking up a man page for ioctl, and it seems rather complicated. I'll try to work with it and post. Unfortunately, the use of this destroys portability, but at least I'll have the satisfaction of messing with output on screen on a linux system ;D

Btw, if you could provide any c++ code of how to use this, please post it. Thanks!

Arndt
April 16th, 2009, 08:46 AM
Thank you Arndt, tried looking up a man page for ioctl, and it seems rather complicated. I'll try to work with it and post. Unfortunately, the use of this destroys portability, but at least I'll have the satisfaction of messing with output on screen on a linux system ;D

Btw, if you could provide any c++ code of how to use this, please post it. Thanks!

Sorry, I would have to build it from scratch, and it was many years since I actually used these parts of the Unix I/O system. I only remembered that there was such a command.