So I use fgets to get strings from a file then add them to an array. But before I add them to the array, I want to strip the "\n". This is what I used for that
strtok(str,"\n");
array[i] = strdup(str);
array is initialized in char *array[32];
Now the functions work perfect, I can use the array and it's elements how I wan't to. However, whenever I try to print the elements, it doesn't work properly.
for example if array[i] = hello
printf("%s:TEST",array[i]);
will print
:TEST
but
printf("%s\n:TEST",array[i]);
will print
hello
:TEST
I need it to be on the same line, so I can't use \n, and it just weird how it will only print if there is a new line after it.
Edit: not strdup(strok) but strdup(str)
Edit: Complete example of code:
char *array[32]
char holder[32]
FILE *fp = fopen("file.txt","r");
int i = 0;
while(fgets(holder, 100, fp)){
strtok(holder,"\n");
array[i] = strdup(holder);
i++;
}
fclose(fp);
printf("word: %s",words[1]);
array[i] will not print unless there is a '\n' after it, as mentioned above
Edit: added print function text file is in format
string1
string2
string3