• [^] # Re: strchr

    Posté par . En réponse au message sscanf et expressions régulières. Évalué à 1.

    J'execute ce programme (test_read.c) comme ceci :

    ./test_read < test_read.txt

    après avoir compilé comme cela :

    gcc -W -Wall -ansi -pedantic test_read.c -o test_read

    avec le fichier suivant (test_read.txt, mélange d'espaces et de tabulations comme tu as spécifié) :

    aaa bbb ccc ddd eee
    111 222 333 444

    et j'obtiens :

    c1 [aaa] c2 [bbb ccc ddd] c3 [eee]
    c1 [111] c2 [222 333] c3 [444]


    --------
    #include <stdio.h>
    #include <string.h>

    #define MAX_CHAR_BY_LINE 1024
    #define MAX_STR 1024

    int main()
    {
    char champ1[MAX_STR], champ2[MAX_STR], champ3[MAX_STR];
    char *sp_debut, *sp_fin;
    char buffer[MAX_CHAR_BY_LINE];

    while (fgets(buffer, MAX_CHAR_BY_LINE, stdin) != NULL) {
    sp_fin = strchr(buffer, '\t');
    *sp_fin = '0円';
    strcpy(champ1, buffer);
    *sp_fin = '\t';
    sp_debut = sp_fin + 1;
    sp_fin = strchr(sp_debut, '\t');
    *sp_fin = '0円';
    strcpy(champ2, sp_debut);
    *sp_fin = '\t';
    sp_debut = sp_fin + 1;
    sp_fin = strchr(sp_debut, '\n');
    *sp_fin = '0円';
    strcpy(champ3, sp_debut);
    *sp_fin = '\n';
    printf("c1 [%s] c2 [%s] c3 [%s]\n", champ1, champ2, champ3);
    }

    return 0;
    }
    --------