我试图打印一条消息,并将光标放在该消息的一个单词或某个位置下,例如:
Error : Type incompatibility, line:column 25:6, in a=b;
^我尝试使用两个printf函数来完成这个任务,如果我们使用我给出的相同的示例,看起来如下所示:
printf("Error : Type incompatibility, line:column 25:6, in a=b;");
printf(" ^");注意:第二个printf中的空格是表格。
但是当我尝试它时,它会显示开始部分中的游标,因为它忽略了表格。
我用类型化的表格(\t)代替了表格,它正确地移动了,但是我必须每次测试字符串长度函数中所需的表格数。
我有没有办法(方法/函数)做任何字符串长度,而不每次测试?
提前谢谢。
发布于 2016-11-29 23:51:06
这样的东西呢?
void point_cursor(char* error_string, char* expression)
{
char* cursor_ptr = strstr(error_string, expression);
int spaces = cursor_ptr - error_string;
printf("%s\n", error_string);
for (int i = 0; i < spaces; i++)
{
printf(" ");
}
printf("^\n");
}然后你把它叫做:
char* error = "Error : Type incompatibility, line:column 25:6, in a=b;";
char* expr = "a=b";
point_cursor(error, expr);产出将是:
Error : Type incompatibility, line:column 25:6, in a=b;
^https://stackoverflow.com/questions/40876992
复制相似问题