Reputation: 135
I want to be able to print using x and y coordinates, using a SetCursorPosition function but as far as I can find, the proper way to do that is by using ncurses. Is it possible to do it using a default c library, like stdio or stdlib?
Upvotes: 1
Views: 1187
Reputation: 17593
The C Standard Library I/O functions are designed around using streams of bytes that are sent or received. Those streams of bytes are sent to some device such as a terminal or a printer or a file or a network device. The Standard Library I/O is really oriented for text terminals, text printers, and files in which a stream of bytes or text characters is written to a device handle or read from a device handle.
About the only kind of device specific positioning of the Standard Library is for files using functions such as ftell()
and fseek()
as well as rewind()
and fgetpos()
and fsetpos()
(see what is difference between fgetpos/fsetpos and ftell/fseek an explanation as to the differences).
The C Standard Library I/O functions do not have any terminal or printer device specific functionality such as positioning a cursor, setting text colors or text font, positioning windows, changing windows, or any of the other functionality common to Graphical User Interfaces or graphical printers (laser or inkjet).
A common work around is to use special characters, escape codes, to indicate that the text following is a command to the device itself and to then specify a command such as setting a cursor position or setting a text color. There are a number of defacto standards for these command sequences which depend on the target device.
In the old days with 80 column, 25 line terminals, most terminals supported a defacto standard with some elements of cursor positioning modeled on what was provided by the VT-100 video terminal from Digital Equipment Corporation.
In order to access that cursor positioning functionality, the first C compatible libraries were developed so that a programmer could specify a cursor position and the library function would create the proper escape code sequence for the position and send it to the terminal.
ncurses is a modern version of those early libraries and is probably the most commonly used for text terminal emulation when a full blown GUI is not needed.
You might find this List of platform-independent GUI libraries helpful though a GUI library may be overkill for what you actually need.
See also the various answers to Cross Platform C library for GUI Apps?
Upvotes: 3
Reputation: 47962
You can do something like
printf("\033[%d;%dH", row, column);
printf("text I want to print");
fflush(stdout);
If you'll be doing it a lot, you could encapsulate it as a function:
void mySetCursorPosition(int row, int column)
{
printf("\033[%d;%dH", row, column);
}
This is totally nonportable, it blindly assumes your terminal (or terminal emulation window) uses the traditional ANSI escape sequences -- which these days they all do, so this is quite likely to work fine for you.
When the day comes that you want to do something higher-level or more portable, you can learn about that then.
Upvotes: 0