Gol*_*dal 4 c arrays string scanf
我正在尝试读取多维数组内字符串的内容...问题是,当我这样做时,sscanf 继续仅读取第一个字符...
我的绳子上有这个:
A1+A2+A3+A4.
Run Code Online (Sandbox Code Playgroud)
我想读取 %c%d ,如果只是 A1 我可以读取这个,但是当这种情况发生时,它只读取 A1...
我这样做是为了只读取第一个字符:
if(sscanf(array[line][colum], "%c%d", &colum, %line) == 2){
printf("COL: %c, Line: %d", colum, line);
Run Code Online (Sandbox Code Playgroud)
我该怎么做才能读取整个字符串?
%n在格式字符串中使用说明符。
例如
#include <stdio.h>
int main(void){
const char *str="A1+A2+A3+A4.";
char col;
int line;
int offset = 0, readCharCount;
while(sscanf(str + offset, "%c%d%*c%n", &col, &line, &readCharCount)==2){
printf("%c, %d\n", col, line);
offset += readCharCount;
}
return 0;
}
/* result
A, 1
A, 2
A, 3
A, 4
*/
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
6184 次 |
| 最近记录: |