使用sscanf会多次读取字符串 [英] Use sscanf to read multiple times string

查看:2256
本文介绍了使用sscanf会多次读取字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图读取字符串的内容,多维数组里面......问题是,当我这样做时,sscanf的继续读取只有第一个字符只有...

I'm trying to read the content of a string, inside a multidimensional array...the problem is, when I do this, the sscanf continues reading only the first character only...

在我的字符串我有这样的:

On my string I have this:

A1+A2+A3+A4.

我想读%C%D,我可以读这一点,如果它只是A1,但这种情况发生的时候,它只读取A1 ...

I want to read %c%d , I can read this if it was only A1, but when this case happens, it only reads A1...

我这样做是为了只读的第一个字符:

I did this to read only the first character:

if(sscanf(array[line][colum], "%c%d", &colum, %line) == 2){
   printf("COL: %c, Line: %d", colum, line);

我能做些什么来读取整个字符串?

What can I do to read the entire string?

推荐答案

使用%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
*/

这篇关于使用sscanf会多次读取字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆