【问题标题】:Edit text which printed on stdout from stdin in c/c++在 c/c++ 中编辑从标准输入打印在标准输出上的文本
【发布时间】:2015-06-19 01:11:00
【问题描述】:

我有以下问题:

如何在我的程序中打印文本,以便进行编辑?

例如程序打印到标准输出:

C:\\BlaBlaBla\file.txt

我可以按退格键,编辑此文本:

C:\\BlaBlaBla\file_1.txt

我会很高兴收到任何信息。

【问题讨论】:

  • 您是更改文件内容还是仅更改文件名?
  • 您想在 shell 中交互式地编辑文本吗?还是在 GUI 中?
  • 我想更改控制台中的文本。 "C:\\BlaBlaBla\file.txt" 是示例。
  • 什么操作系统?您将需要一个库的帮助,例如 ncursesWindows Console API。查看here 了解更多信息。
  • 标准库(我用的是Windows,MSVS)有这种可能吗?

标签: c++ c stream stdout stdin


【解决方案1】:

获得命令行编辑的一种方法是使用GNU readline 库提供的功能。

【讨论】:

    【解决方案2】:

    我使用 malloc 以这种方式解决了这个问题(使用退格、删除和箭头(左右)):

    #include <stdio.h>
    #include <string.h>
    #include <conio.h>
    #include <malloc.h>
    
    
    #define ENTER 13
    #define ARROW 224
    #define LEFT 75
    #define RIGHT 77
    #define SEPARATOR 219
    #define DELETE 83
    #define BACKSPACE 8
    
    void editText(char *s);
    void printTextWithCursor(char *s, size_t position);
    int insertInStr(char **s, size_t position, char c);
    void deleteSymbol(char *s, size_t position);
    
    #define TEXT "Some text."
    
    int main()
    {
        unsigned char c;
        char *s = malloc(strlen(TEXT)+1);
        __int64 pos;
    
        strcpy(s, TEXT);
        pos = strlen(s);
    
        printf("The original text:"TEXT"\n");
        printf(TEXT"%c", SEPARATOR);
    
    
        while((c = getch())!= ENTER)
        {
            putchar('\r');
            switch(c)
            {
            case ARROW://OR DELETE
                c = getch();
                if (c == LEFT && pos > 0)
                    pos--;
                if (c == RIGHT && pos < strlen(s))
                    pos++;
                if(c == DELETE && pos < strlen(s))
                    deleteSymbol(s, pos);
                break;
            case BACKSPACE:
                if(pos > 0)
                    deleteSymbol(s, --pos);
                break;
            default:
                if(pos >= 0 && pos <= strlen(s))
                {
                    insertInStr(&s, pos++, c);
                }
    
    
            }
            printTextWithCursor(s, pos);
    
        }
    
        return 0;
    }
    
    void deleteSymbol(char *s, size_t position)
    {
        size_t i, len = strlen(s);
    
        for(i = position; i < len; i++)
            s[i] = s[i+1];
    }
    int insertInStr(char **s, size_t position, char c)
    {
        __int64 i;
    
        if((*s = realloc(*s, strlen(*s) +1 +1)) == 0)
            return 0;
        for(i = strlen(*s)+1; i >= position; i--)
            (*s)[i] = i == position ? c : (*s)[i-1];
        return 1;
    }
    void printTextWithCursor(char *s, size_t position)
    {
        size_t currPos, length = strlen(s);;
    
    
        for(currPos = 0; currPos <= length; currPos++)
        {
            putchar(currPos < position ? s[currPos] : currPos == position ? SEPARATOR : s[currPos-1]);
        }
        putchar('\0');
    }
    

    【讨论】:

    • #define _FOR_RUSSIAN 保留名称。而且你从来不用它。
    猜你喜欢
    • 2012-04-25
    • 1970-01-01
    • 2011-09-14
    • 2012-01-25
    • 1970-01-01
    • 2010-10-29
    • 1970-01-01
    • 1970-01-01
    • 2011-11-15
    相关资源
    最近更新 更多