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 69 70
|
main.c
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," ");
if(wrd != NULL)
{
l = insertTail(l,wrd);
printf("Extracted word : %s\n",wrd);
}
while(wrd != NULL)
{
wrd = strtok(NULL," ");
if(wrd != NULL)
{
l = insertTail(l,wrd);
printf("Ectracted word : %s\n",wrd);
}
}
}
printf("\n");
fclose(fp);
printf("Number of words in the list : %d\n",nbwords(l));
printList(l); /*le problème du programme ne vient pas de la printList, mais de l'insertion des mots,
(de plus, la fonction insertTail fait bien son job) puisqu'il y a bien 8 mots (mais les mauvais)*/
return EXIT_SUCCESS;
} |
Partager