C 中的字符串操作?

发布于 2024-09-27 20:38:34 字数 590 浏览 11 评论 0原文

我想打印一个字符数组,这些字符首先是下划线。 然后用户可以在这些下划线上写入字符。我使用了gotoxy(),但它不能正常工作。 这就是我写的:

int main(void)
{
    char arr[20];
    int i;
    char ch;
    clrscr();

    for(i=0;i<=20;i++)
    {
        textattr(0x07);
        cprintf("_");
    } 

    do
    {
        for(i=0;i<=20;i++)
        {
            //gotoxy(i,0);
            //ch = getche();
            if( isprint(ch) == 1)
            {
                arr[i] = ch;
                gotoxy(i,0);
                //printf("%c",ch);
            }
        }
    } while(i == 20);

    getch();
    return 0;
}

I want to print an array of characters, these characters are underscores first.
Then the user can write characters on these underscores.I used gotoxy() but it doesn't work properly.
That is what i wrote:

int main(void)
{
    char arr[20];
    int i;
    char ch;
    clrscr();

    for(i=0;i<=20;i++)
    {
        textattr(0x07);
        cprintf("_");
    } 

    do
    {
        for(i=0;i<=20;i++)
        {
            //gotoxy(i,0);
            //ch = getche();
            if( isprint(ch) == 1)
            {
                arr[i] = ch;
                gotoxy(i,0);
                //printf("%c",ch);
            }
        }
    } while(i == 20);

    getch();
    return 0;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

趴在窗边数星星i 2024-10-04 20:38:35

第一件事是:您可能不希望在主函数中调用所有这些对 gotoxytextattrcprintf 的调用,因为这不是 main 函数应该做的。

主函数的目的更有可能是“从用户那里读取一些文本,并在输入字段中很好地呈现”。所以你应该把它变成一个函数:

static int
nice_input_field(char *buf, size_t bufsize, int x, int y) {
  int i, ch;

  gotoxy(x, y);
  for (i = 0; i < bufsize - 1; i++) {
    cprintf("_");
  }

  i = 0;
  gotoxy(x, y);
  while ((ch = readkey()) != EOF) {
    switch (ch) {

    case '...': /* ... */
      break;

    case '\b': /* backspace */
      cprintf("_");
      i--;
      gotoxy(x + i, y);
      break;

    case '\t': /* tabulator */
    case '\n': /* enter, return */
      buf[i] = '\0';
      return 0; /* ok */

    default: /* some hopefully printable character */
      if (i == bufsize - 1) {
        cprintf("\a"); /* beep */
      } else {
        buf[i++] = ch;
        gotoxy(x + i, y);
        cprintf("%c", buf[i]);
      }
    }
  }

  /* TODO: null-terminate the buffer */
  return 0;
}

The first thing is this: You probably don't want to have all those calls to gotoxy, textattr and cprintf in your main function, since that is not what the main function is supposed to do.

It is much more likely that the main function's purpose is "to read some text from the user, presented nicely in an input field". So you should make this a function:

static int
nice_input_field(char *buf, size_t bufsize, int x, int y) {
  int i, ch;

  gotoxy(x, y);
  for (i = 0; i < bufsize - 1; i++) {
    cprintf("_");
  }

  i = 0;
  gotoxy(x, y);
  while ((ch = readkey()) != EOF) {
    switch (ch) {

    case '...': /* ... */
      break;

    case '\b': /* backspace */
      cprintf("_");
      i--;
      gotoxy(x + i, y);
      break;

    case '\t': /* tabulator */
    case '\n': /* enter, return */
      buf[i] = '\0';
      return 0; /* ok */

    default: /* some hopefully printable character */
      if (i == bufsize - 1) {
        cprintf("\a"); /* beep */
      } else {
        buf[i++] = ch;
        gotoxy(x + i, y);
        cprintf("%c", buf[i]);
      }
    }
  }

  /* TODO: null-terminate the buffer */
  return 0;
}
晨光如昨 2024-10-04 20:38:35

打印字符数组相当容易:

char* str = your_array;
while(*str) {
   putc(*str++);
}

从内存中将字符串打印到屏幕上。

Printing an array of characters is fairly easy:

char* str = your_array;
while(*str) {
   putc(*str++);
}

From memory that should print a string out to the screen.

泼猴你往哪里跑 2024-10-04 20:38:35

您的代码非常特定于 DOS。对于以可移植的方式读取立即输入的问题,没有一个好的通用解决方案。它确实经常被问到,所以我认为 C 常见问题解答已分解并包含您可能想要寻找的答案。

也就是说,我认为你的错误是 gotoxy(1, 1) 是屏幕的上角,而不是 0,0。所以你想要 gotoxy(i, 1)

Your code is very DOS-specific. There is not a good general solution to the problem of reading immediate input in a portable way. It does get asked quite often, so I think the C FAQ broke down and included an answer which you might want to seek out.

That said, I think your bug is that gotoxy(1, 1) is the upper corner of the screen, not 0,0. So you want gotoxy(i, 1)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文