【发布时间】:2020-09-05 02:08:47
【问题描述】:
我很不擅长在 C/C++ 中使用 char*'s,我要求这样的效果:
假设我有一个文本编辑器或查看器,我想在侧面显示行号。我的实现如下(如果可能,请指出其中的任何错误):
unsigned int line_number_width = 5;
char* lines = strdup(buffer->text);
char* line = strtok(lines, "\n");
unsigned line_number = 1;
while (line != NULL && line_number<= buffer->height) {
printf("???:%.*s\n", !!line_number, line_number_width!!, buffer->width - line_number_width - 1, line);
line = strtok(NULL, "\n");
line_number++;
}
free(lines);
我想打印以下内容:
1:line1
2:line2
3:line3
4:line4
5:line5
6:line6
7:line7
8:line8
9:line9
10:line10
..........
..........
..........
..........
..........
..........
..........
..........
100:line100
注意: 是如何在同一列中对齐的。
我应该用什么替换???,以便获得以下效果,其中传递的数字包含在长度为line_number_width的矩形中,这是!!..!!之间的参数,这样传递的数字就是正确的假想的矩形?
【问题讨论】:
-
没有C/C++之类的语言,很明显你是真的在用C编程。
-
%*d不适合你吗?
标签: c char-pointer