I'm writing a program which is going to read a dictionary with the following syntax:
john
philips
dudule
henry
I have therefore wrote this program:
#define SIZEWORD 10
int main(int argc, char **argv)
{
/*get dictionary words*/
FILE * pFile_dictionary;
char dictionary_name[SIZEWORD];
int len;
pFile_dictionary = fopen(argv[1] , "r");
if (pFile_dictionary == NULL)
perror("Error while opening the dictionary");
else
{
while (!feof (pFile_dictionary) )
{
if (fgets(dictionary_name , SIZEWORD , pFile_dictionary) == NULL)
break;
printf("Before : dictionary_name is = [%s] \n", dictionary_name);
/*remove new line*/
char *tmp_LF = strrchr(dictionary_name, '\n');
if (tmp_LF != NULL)
*tmp_LF = '\0';
printf("After : dictionary_name is = [%s] \n", dictionary_name);
}
}
fclose(pFile_dictionary);
return 0;
}
Could someone explain to me why I have this output ? I'm trying to remove the new line added by the fgets function, but it appears to have no effect:
Before : dictionary_name is = [john
]
After : dictionary_name is = [john
]
Before : dictionary_name is = [philips
]
After : dictionary_name is = [philips
]
Before : dictionary_name is = [dudule
]
After : dictionary_name is = [dudule
]
Before : dictionary_name is = [henry
]
After : dictionary_name is = [henry
]
Thanks for your help
Ps: Sorry for the bad display, but I'm struggling to understand how to do it easily