Bonjour, j'ai créé un programme qui a pour objectif de : lire dans un fichier ligne par ligne, puis extraire les mots de chacun de ces lignes, et enfin placer ces mots dans une liste. Tout marche, jusqu'à la rentrée dans une liste, où j'obtiens une segmentation fault, mais je vois pas d'où ça vient. Voilà le main.c et la définition de ma liste :

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
62
63
64
65
66
67
68
 
 
typedef struct elem {
char* word;
struct elem* next;
} Element;
 
typedef Element* List;
 
int main(int argc,char* argv[])
{
	List l=NULL;
	char str[200];
	FILE *fp = fopen("./test.txt","r");
	char* wrd; /*a array of caracters representing ONE word*/
 
   	if(!fp) return 1; /* bail out if file not found*/
 
    /*
    fgets function :
    
    La fonction fgets() prends comme premier paramètre la chaîne dans laquelle elle va placer la saisie. Le deuxième paramètre est le nombre maximal 
    de caractères (n-1 plus exactement) qui seront lus et le dernier paramètre est le flux à lire. Ici se sera stdin. (le flux est un fichier)
    */
 
	while(fgets(str,sizeof(str),fp) != NULL)
	{
		/* strip trailing '\n' if it exists*/
		int len = strlen(str)-1;
		if(str[len] == '\n')
		{ 
	    	str[len] = 0;
	    	}
 
		/*
		char * strtok ( char * str, const char * delimiters );
		str :
		C string to truncate.
		Notice that the contents of this string are modified and broken into smaller strings (tokens).
		Alternativelly, a null pointer may be specified, in which case the function continues scanning where a previous successful call to the function ended.
		delimiters :
		C string containing the delimiter characters.
		These may vary from one call to another.
		*/
 
  		printf ("Splitting string \"%s\" into words:\n",str);
		wrd = strtok (str," ");
		l = insertTail(l,wrd);
		while (wrd != NULL)
		{
			printf ("word extracted : %s\n",wrd);
			wrd = strtok(NULL," "); /* the second time we call the function, we have to put NULL into the second parameter*/
			if(wrd != NULL)
			{
				l = insertTail(l,wrd);
			}
		}
 
	}
	printf("\n");
	fclose(fp);
 
 
 
	printList(l);
 
	return EXIT_SUCCESS;
}
voici maintenant les fonctions printlist et inserttail :

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
 
List insertTail(List l, char* p)
{
	List newel; 
	List pl;
 
	newel = (Element *) malloc(sizeof(Element));
	newel->word = p;
	newel->next = NULL;
 
	if(l == NULL)
	{
		l = newel;
	} else {
		pl = l;
		while(pl->next != NULL){
			pl=pl->next;
		}
		pl->next=newel;
	}
 
	return l;
}
 
void printList(List l)
{
	if(l==NULL)
	{
		printf("Empty List\n");
	}
	else
	{
		Element* w = w;
		while(w->next != NULL)
		{
			printf("%s",w->word);
			w=w->next;
		}
		printf("%s\n",w->word);
	}
}
Si vous pouviez m'aider ou m'indiquer comment trouver, ça m'aiderait beaucoup... Merci