omitir caracteres con 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. 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.

Is there a (standard) way to do this ? using something else than printf is possible.

[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. Is there any problem with this function ?

preguntado el 08 de noviembre de 11 a las 15:11

\r no quiere decir reajustar pero retorno de carro. -

yes, thanks for the reminder. I know, it's just that in this context, \r serves this purpose. -

What Operating System do you use? For UNIX-like there is the curses- or ncurses-library. -

Primary target OS is windows. Linux comes afterwards. I would prefer to avoid using an external library. -

6 Respuestas

AFAIK there's no standard way to do this because there's no console behavior standarization (eg Windows' console does not behave like Linux's)

respondido 08 nov., 11:19

Según ANSI documentation, puedes usar "\033[1;20H" to position the cursor.

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.

Or you can try with only "\033[20C" to move your cursor to column 20.

respondido 08 nov., 11:19

Thanks for the tip Cédric. Unfortunately, it did not work. I guess this could be because it needs ANSI.SYS driver loaded. - Cian

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

respondido 08 nov., 11:19

OK, this is the better work-around so far. It's not that easy though, since i would like to have several processes updating their fields on the same line. With the \b command, i can at least come up with a 2-fields line mechanism. But for 3 and more, it doesn't work. - Cian

I have had the same trouble before. I used gotoxy() when I coded in TurboC++.

Check this out. Looks good.

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

respondido 08 nov., 11:19

Why Don't you just sprintf (myStr,.... and build the line in memory then output it in one go? You could keep the fields you want from the previous update.

respondido 08 nov., 11:19

Several different processes are writing on the line. It's not a single program. - Cian

Marc Rochkind wrote a realy good book which is the leading reference on the subject! Advanced C Programming for Displays: Character Displays, Windows, and Keyboards for the Unix and Ms-DOS Operating Systems

respondido 09 nov., 11:21

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.