Hi all,
I am currently trying to retrieve a string from a textfile, separate that and put it in an array.
I stumbled on the problem that *token is a pointer, and not the string. However when i printf("%s", token) it prints out the word that it points to. Could anyone explain how to fix this problem?
Words.txt:
dog|cat|hamster
The code:
char c[50000]; /* declare a char array */
int n;
FILE *file; /* declare a FILE pointer */
file = fopen("words.tx t", "r");
n = fread(c, 1, 50000, file);
c[n] = '\0';
/* Separating the contents, divided by | */
char *token = strtok(c, "|");
char words[50000];
int f = 0;
while(token != NULL)
{
printf("[%s]\n", token);
/* Here the error is caused: invalid conversion from char* to char */
words[f] = token;
token = strtok(NULL, "|");
f++;
}
fclose(file);
I am currently trying to retrieve a string from a textfile, separate that and put it in an array.
I stumbled on the problem that *token is a pointer, and not the string. However when i printf("%s", token) it prints out the word that it points to. Could anyone explain how to fix this problem?
Words.txt:
dog|cat|hamster
The code:
char c[50000]; /* declare a char array */
int n;
FILE *file; /* declare a FILE pointer */
file = fopen("words.tx t", "r");
n = fread(c, 1, 50000, file);
c[n] = '\0';
/* Separating the contents, divided by | */
char *token = strtok(c, "|");
char words[50000];
int f = 0;
while(token != NULL)
{
printf("[%s]\n", token);
/* Here the error is caused: invalid conversion from char* to char */
words[f] = token;
token = strtok(NULL, "|");
f++;
}
fclose(file);
Comment