how can I "extract" from a string in C using fscanf?

1

I want to use fscanf () as a filter, for example if I do the following with a record with two fields, the first field is "ignored" and it reads the second field

fscanf(fd, "%*d, %d", &numero)

As I would if I want to read a part of the chain of a field, I'll explain having a URL ex: link that only saves me the last part of the server domain or com

Can I somehow ignore part of the chain?

    
asked by Bubblegum Crisis 17.10.2017 в 05:04
source

1 answer

0

In fact there is no way to make that filter in C, you have to implement it all.
If you need the scanf() you can use the library <string.h> to analyze the url, I'll give you a solution without using it.

#include <stdio.h>

int main()
{
    //creo un lugar para almacenar com, net, etc, nunca seran mas de 3 letras
    char array[3];
    //creo recorredores it y r, c es un entero porque convierto chars a enteros por tabla ascii
    int c = 0, it = 0, r = 0;
    while (c = getchar()) {
        //si el iterador encuentra q esta en el 2do punto, vamos a guardarlo
        if (it > 1 && r < 3) {
            array[r] = c;
            r++;
        }
        //si encuentro el punto, aumento al iterador
        if (c == '.') it++;
        //si el limite del almacenamiento llega, nos salimos del bucle
        if (r >= 3) {
            break;
        }
    }
    //para imprimir, el arreglo de chars hacemos..
    c = 0;
    while (c < 3) {
        printf("%c", array[c]);
        c++;
    }
    return 0;
}

I do not like to use scanf() for this situation, since I will be very efficient in the analysis of the url getchar() is the adequate function for an analysis in time of execution, but you can condition the program so that it does with scanf() if you like.

The logic is simple, if you count the points, you give with the domain com , net , etc.

    
answered by 17.10.2017 / 06:20
source