I'm using <ncurses.h>
library for a simple program in terminal. It reads a file character by character in a conventional way:
FILE* f = fopen(filename, "r");
int c;
while ((c = fgetc(f)) != EOF) { /* process c */ }
The characters are put inside an array of chars. I need to know the (y,x) coordinates of each character on terminal screen. To do so, I keep indexes that determine the first and last char in line on screen in another array. It's easily adjustable when the terminal window is resized - each line consists of either max_x
characters, or a smaller number of characters, if there's a '\n'
.
Tabs cause a problem - although they are recognised as '\t'
, their width and therefore number of "pixels" in text interface is different (when displayed by cat
in terminal or as a consecutive char in a string containing all the characters from file). However, when separated from the rest of file by:
if (c == '\t') {...}
they do not differ. I've also tried printing those detected tabs and comparing the cursor's position to get their width, but once again they look the same.
How to get the actual width of a tab when reading a file? I'd like to convert the width to a number of spaces. Thank you for attention and perhaps hints.