Use sscanf to read multiple lines

I am trying to read the contents of a string inside a multidimensional array ... the problem is that when I do this, sscanf continues to read only the first character ...

In my line, I have the following:

A1+A2+A3+A4. 

I want to read% c% d, I can read this if it was only A1, but when that happens, it reads only 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 line?

+4
source share
1 answer

Use the %n qualifier in the format string.

eg.

 #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 */ 
+3
source

Source: https://habr.com/ru/post/1487311/


All Articles