4

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.

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?

GoldenMedal
  • 102
  • 2
  • 9

1 Answers1

5

Use the %n specifier in the format string.

E.g.

#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
*/
anatolyg
  • 26,506
  • 9
  • 60
  • 134
BLUEPIXY
  • 39,699
  • 7
  • 33
  • 70
  • And to replace the position of the "pair" A1 for their respective values, like, A1 has the value of, at my array, of array[0][0]? Using simple substitutions like this, i only use strcpy, but if i use at this case, i will replace all the string, and its not what i want, i want to keep all the other values.. – GoldenMedal Jun 21 '13 at 16:50
  • And i'm getting infinity loop while i'm taking the array[line][col] at the first parram.... [code]sscanf(array[line][col] + offset .....[/code] – GoldenMedal Jun 21 '13 at 18:39
  • `infinity loop while i'm taking the array[line][col] at the first parram` maybe do initialized(each of the loop) it into the loop value of `offset`? – BLUEPIXY Jun 22 '13 at 09:26
  • ok, i fixed it, but, i want to read the OP too, i tryied something like this? sscanf(matris[i][c] + offset, "%c%d%c%n", &col, &line, &op, &readCharCount)==3 printf("%c, %d, %c", col, line, op"); and it dont print all the occurrencis on the cell, if i write A1+B1 with your code, it prints A, 1 | B,1 but when i write with my code, prints A,1 and +(the op) and nothing more...i want to print the b,1 too...What can i change? – GoldenMedal Jun 23 '13 at 16:34
  • @user2505640 i think no problem in your way. but it might be different format of the input strings. I think scanf as not suitable if you think try to respond in a flexible format . – BLUEPIXY Jun 23 '13 at 16:52
  • that way i made, wasnt to work? i'm recieving 3 params, and printing them....it should work. What i made wrong? the input format? – GoldenMedal Jun 23 '13 at 17:16
  • @GoldenMedal do you show me input string as it would not work properly with the minimal amount of code? – BLUEPIXY Jun 23 '13 at 17:27
  • there we go, my entire code, its write in portuguese, but you will understand. At the end, have my input, and output. http://pastebin.com/yDtMTAST – GoldenMedal Jun 23 '13 at 18:07
  • @GoldenMedal Input of data cut into `matris` is not possible. It is because no functioning `colunas = ordenar(vetorDeNumeros);`. it does not work as expected all the following processing of `colunas` value will become 0. `tamanhoVetor = (sizeof vetorDeNumeros)/(sizeof vetorDeNumeros[0]);` at function `ordenar` is a problem. It is not possible to obtain the size in such a way as this array are passed as a pointer to a function. – BLUEPIXY Jun 23 '13 at 18:39
  • i use this function ordenar to catch the max number of colluns. How can i pass the array to the function, and take his size, to use bubble sort to sort from highest to lowest? And i cant understand why, i passing the 3 arguments for sscanf, dont return me all the occurencies. thanks for patience. – GoldenMedal Jun 23 '13 at 19:04
  • @GoldenMedal array `vetorDeNumeros` size is `k`, `k` pass to `ordenar`. E.g. prototype is `int ordenar(int vetorDeNumeros[], int size)` – BLUEPIXY Jun 23 '13 at 19:13
  • or calculate call side `(sizeof vetorDeNumeros)/(sizeof vetorDeNumeros[0])`. E.g. `colunas = ordenar(vetorDeNumeros, k);` or `colunas = ordenar(vetorDeNumeros, (int)((sizeof vetorDeNumeros)/(sizeof vetorDeNumeros[0]));` – BLUEPIXY Jun 23 '13 at 19:19