简体   繁体   English

用printf跳过字符

[英]skipping characters with printf

I'm trying to write a program which would write on the console at a very specific position in the screen. 我正在尝试编写一个程序,它可以在控制台上在屏幕上的一个非常特定的位置写入。 Say for example from column 20 to column 39. After each write, the line is "reset" thanks to the \\r parameter. 比如说从第20列到第39列。在每次写入之后,由于\\r参数,该行被“重置”。 This ensures that the line remains static and only the specific fields are updated. 这可确保该行保持静态,仅更新特定字段。

Problem is, i can instruct printf to write from column 0 to 19 without erasing the rest of the line, but it seems i'm not able to instruct printf to write from column 20 onwards without erasing in the process columns 0 to 19. 问题是,我可以指示printf从第0列到第19列写入而不删除行的其余部分,但似乎我无法指示printf从第20列开始写入而不在进程列0到19中擦除。

Is there a (standard) way to do this ? 有(标准)方法吗? using something else than printf is possible. 使用除printf以外的东西是可能的。

[Edit] I've read there is a gotoxy() function in C which is available for windows apparently, and can be emulated in Linux using ncurses. [编辑]我已经读过在C中有一个gotoxy()函数,它可以用于Windows,并且可以使用ncurses在Linux中进行模拟。 Is there any problem with this function ? 这个功能有问题吗?

AFAIK没有标准的方法来执行此操作,因为没有控制台行为标准化(例如Windows的控制台不像Linux的行为)

According to ANSI documentation , you can use "\\033[1;20H" to position the cursor. 根据ANSI文档 ,您可以使用"\\033[1;20H"来定位光标。

It will move the cursor to the needed position. 它会将光标移动到所需的位置。 The values 1 and 20 are the row and the column, just change it to position correctly your print. 值1和20是行和列,只需将其更改为正确定位打印。

Or you can try with only "\\033[20C" to move your cursor to column 20. 或者您可以尝试仅使用"\\033[20C"将光标移动到第20列。

You can try printing as many backspaces (and spaces to clear old text) as needed to position the cursor. 您可以根据需要尝试打印多个退格(以及清除旧文本的空格)以定位光标。

No guarantee it works for you ... if it does: no guarantee it works on the other computer :) 不保证它对你有用......如果有:不保证它适用于其他计算机:)

#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int main(void) {
  int i, k;
  time_t oldtime = time(0);
  if (oldtime == (time_t)-1) {
    fprintf(stderr, "time function does not work on this machine\n");
    exit(EXIT_FAILURE);
  }
  while (time(0) == oldtime) /* void */;

  printf("fixed stuff: ");
  for (i = 1; i < 6; i++) {
    int val = pow(10, i) * i;
    printf("%d", val);
    fflush(stdout);
    oldtime = time(0);
    while (time(0) == oldtime) /* void */;
    if (i < 5) {
      for (k = 0; k < i + 1; k++) printf("\b \b"); // go back; erase; go back again
      fflush(stdout);
    } else {
      puts("");
    }
  }

  return 0;
}

It works for me, on both Linux and Windows computers 它适用于Linux和Windows计算机

I have had the same trouble before. 我之前遇到过同样的麻烦。 I used gotoxy() when I coded in TurboC++. 当我在TurboC ++中编码时,我使用了gotoxy()。

Check this out. 看一下这个。 Looks good. 看起来不错。

http://www.daniweb.com/software-development/c/code/216326# http://www.daniweb.com/software-development/c/code/216326#

为什么你不只是sprintf(myStr,....并在内存中构建行然后一次输出它?你可以保留你想要的字段来自上一次更新。

Marc Rochkind wrote a realy good book which is the leading reference on the subject! Marc Rochkind写了一本真正的好书,这是该主题的主要参考! Advanced C Programming for Displays: Character Displays, Windows, and Keyboards for the Unix and Ms-DOS Operating Systems 显示器的高级C编程:Unix和Ms-DOS操作系统的字符显示器,Windows和键盘

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM