-2 c formatting scanf
假设我有一个这样的输入,每行一个单元格:
[0,0]
[0,2]
[0,4]
[2,4]
[4,4]
Run Code Online (Sandbox Code Playgroud)
我设法使用类似的方法逐一阅读它们,[%d,%d]并得到如下输出:
cell1: row: 0, col:0
cell1: row: 0, col:2
Run Code Online (Sandbox Code Playgroud)
然后我有另一个像这样的输入
[0,0]->[0,1]->
[0,2]->[0,3]->
[0,4]->[1,4]->
[2,4]->[3,4]->
[4,4]->[5,4]
Run Code Online (Sandbox Code Playgroud)
如何在不被箭头'打断的情况下读取输入->'?
好的,快速且便宜的解决方案:扫描所有内容,直到一个左括号,而不管它是什么。然后扫描实际数据。重复。
int x, y;
scanf("%*[^[]"); // ignore everything up to the first [
while (scanf(" [%d ,%d]", &x, &y) == 2) {
printf("[%d, %d]\n", x, y);
scanf("%*[^[]"); // ignore stuff between ] and [
}
Run Code Online (Sandbox Code Playgroud)