Bonjour, je debute en langage C, surtout dans la gestion des chaines de caractères.
J’ai voulus créer une fonction demandant une saisie utilisateur comme à l’instar de la fonction ‘request’ (langage basic de la TI92).
J’ai concu un projet testant diverses fonctions que dispose la pluapart de langage basic :
void mids(char chaine[], int pos, int nb);
int instring(char chaine[], char substr[]);
void left(char chaine[], int nb);
void right(char chaine[], int nb);
char *substr(char *chaine, int p, int length);
char *str();
char *rturnC(void);
char *rturnC2(char * chaine);
char *request(char *txt);
Mon soucis est le suivant : la fonction request ne fonctionne pas, elle ne demande aucune saisie.
Hors, j’ai créé un projet simplifié :
Listing main.c
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
 
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "str.c"
 
int main(int argc, char *argv[])
{
    char *r, cst[] = "FIN";
    int cnt = 1;
    r = request("Please enter your name ");
    printf("Hello %s, how are you ?\n\n", r);
    while (cnt)
    {
        printf("Continu press any key while 'FIN'\n");
        r = request("Please press anykey and 'RETURN' ");
        if (strcmp(r, "FIN") == 0)
            cnt = 0;
        printf("String in mem : >%s<\n\n", r);
    }
    printf("Fin du programme !!\n");
    return 0;
}
Listing str.C
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
 
#include "str.h"
#define TAILLE_BUFFER 1000
#define TAILLE_MAX (TAILLE_BUFFER - 1)
 
char *request(char *txt)
{
    int i, dimStr;
    char *tmp;
    printf("%s : ", txt);
    char buffer[TAILLE_BUFFER];
    fgets(buffer, TAILLE_MAX, stdin); //warn buffer = "test" >> "t.e.s.t.\n.\0"
    dimStr = strlen(buffer); // dimStr = 5
    buffer[dimStr - 1] = '\0'; //remplace '\n' by '\0'
    tmp = malloc(strlen(buffer) + 1); //add 1 pour '\0'
    if (tmp == NULL)
    {
        fputs(stderr, "Error Allocation : Nok !!");
        exit(EXIT_FAILURE);
    }
    for (i = 0; i < dimStr; i++)
    {
        tmp[i] = buffer[i];
    }
    return tmp;
}
LListing str.h
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
 
#ifndef STRING_H_INCLUDED
#define STRING_H_INCLUDED
char *request(char *txt);
#endif // STRING_H_INCLUDED
Ce projet lui, s’execute correctement :
On entre ‘scroller’ puis ‘essai’, ‘encore’, ‘iygzeriprez’
et ‘FIN’
On obtient l’ecran suivant :
Please enter your name : scroller
Hello scroller, how are you ?

Continu press any key while ‘FIN’
Please press anykey and ‘RETURN’ : essai
String in mem : >essai<

Continu press any key while ‘FIN’
Please press anykey and ‘RETURN’ : encore
String in mem : >encore<

Continu press any key while ‘FIN’
Please press anykey and ‘RETURN’ : iygzeriprez
String in mem : >iygzeriprez<

Continu press any key while ‘FIN’
Please press anykey and ‘RETURN’ : FIN
String in mem : >FIN<

Fin du programme !!

Process returned 0 (0x0) execution time : 33.125 s
Press any key to continue.
_
J’aimerais savoir pourquoi cette fonction fonctionne dans ce projet mais pas dans le premier ????