0

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.

4

1 回答 1

0

Of course the actual tab width is dependant on the way you are presenting it on screen. For the classical presentation, that is a tabulation every eight column, you have to keep track on the column number where the tab occurs. You can then use:

tab_width = 8 - column % 8

Where column starts counting at 0 for the first column.

于 2012-12-26T14:02:13.237 回答