Je récupère un texte sur une socket. plusieurs champs concaténé avec un séparateur (le pipe).
Le but est de séparer les éléments.
PB : j'ai des caractères parasites :s
voici mon code :
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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
 
#define SEPARATOR '|'
void receiveStructFile(SOCKET sock, structFile *file){
    char buffer[BUFFER];
    int bytesLus;   
    char *tmp;
    char *debut;
    char *fin;
    int taille;
 
    bytesLus=recv(sock,buffer,BUFFER,0);
    printf("Client %d> ",sock);
    write(1,&buffer,bytesLus);
    printf("\n"); 
 
    debut=buffer+2;
    printf("%s\n",debut);
 
    fin=(char *)strchr(debut,SEPARATOR);
    taille=(fin-debut);
//    printf("%s\n",fin);  
    tmp=(char *) malloc(taille*sizeof(char));
    strncpy(tmp,debut,taille);
    printf("%s\n",tmp);    
 
 
    debut=fin+1;
    fin=(char *)strchr(debut,SEPARATOR);
    taille=(fin-debut);
//    printf("%s\n",fin);    
    tmp=(char *) realloc(tmp,taille*sizeof(char));
    strncpy(tmp,debut,taille);
    printf("%s\n",tmp);   
 
 
    debut=fin+1;
    fin=(char *)strchr(debut,SEPARATOR);
    taille=(fin-debut);
//    printf("%s\n",fin);    
    tmp=(char *) realloc(tmp,taille*sizeof(char));
    strncpy(tmp,debut,taille);
    printf("%s\n",tmp);   
    free(tmp);
 
    debut=fin+1;    
//    tmp=atoi(debut); 	
    printf("%i\n",atoi(debut)); 
 
}
voici le résultat :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
 
C:\Documents and Settings\admin\Bureau\C3007>serveur
WIN: winsock2: OK
# Socket 1992 is now opened in TCP/IP mode
Client 1992> 0|Outlook.pst|C:\Documents and Settings\admin\Local Settings\Application Data\Microsoft\Outlook\|OutlookSAVE|109
Outlook.pst|C:\Documents and Settings\admin\Local Settings\Application Data\Microsoft\Outlook\|OutlookSAVE|109A¶w
Outlook.pstec=C:ç
C:\Documents and Settings\admin\Local Settings\Application Data\Microsoft\Outlook\A⌂
OutlookSAVEs andç
109
# Socket 1992 (TCP/IP mode) is now closed
 
C:\Documents and Settings\admin\Bureau\C3007>
Pourquoi j'ai des caractères parasites à la fin du char * tmp ?