j'ai un tableau de caracteres ou je dois saisir les donnees moi meme. Je dois utiliser ctrl d (compilation avec la console sous Unix) pour finir et afficher mes mots. Mais ca ne marche pas ou je ne sais pas comment le controler. J'utilise fgets pour lire les donnees.

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
50
51
52
53
54
55
56
57
58
59
60
61
#include<stdlib.h>
#include<stdio.h>
#include<string.h>
 
/* Fct: read an array of string  */
 
int lecture(char *tab[], int max)
{
	int nbchaine=0;    /* The number of word */
	char chaine[100];  /* Word to read */
	puts("Enter words and Ctrl -d to finish ");
 
	/* lesen und speichern woerter in Felder (saving word in the array) */
 
	while(nbchaine<=max && fgets(chaine,101,stdin))
	{
	        if(nbchaine==max)
		{
			max=max*2;
			tab=realloc(tab,max*sizeof *tab);
		}
		if(strlen(chaine)<100 && strlen(chaine)>0)
		{
			tab[nbchaine]=(char*)malloc(strlen(chaine)+1);
			strcpy(tab[nbchaine],chaine);
			nbchaine++;
		}
		else
			if(strlen(chaine)>=100 || strlen(chaine)<=0)
				perror("Error");
	}
	return nbchaine;   /* Return tne number of word*/
}
 
/* compare 2 words with strcmp */
 
int sort(const void *a, const void*b)
{
	return strcmp(*(char * const *)a, *(char * const *)b);
}
 
/*  Printing words*/
 
void ecrire(char *tab[], int max)
{
	int i;
	/*  sortieren mit qsort */
	qsort((void *)tab, max, sizeof(tab[0]), sort);
	for(i=0;i<max;i++)
		puts(tab[i]);
}
 
int main()
{
	int nblues=0;     /* number of words in the array */
	char **tab=malloc(100*sizeof(char *));   /* Ferder von Zeigern*/
	nblues=lecture(tab,100);
	printf("\n");
	ecrire(tab,nblues);
	return 0;
}