I have a text file, similar to the following:
Name1: ID1
Name2: ID2
Name3: ID3
I am trying to parse it to get
Name1
Name2
Name3
stored in a variable.
I wrote the following function:
/*
* filename Name of file to read
* result The result will be stored here
*/
void readlist(char* filename, char* result) {
FILE *fp;
char buffer[2048];
memset((void *)result, '\0', BUFFER_SIZE);
fp = fopen(filename, "r");
while (fgets(buffer, sizeof(buffer), fp)) {
char *token = NULL;
token = strtok( buffer, ":" );
strcat(result, token);
}
fclose(fp);
}
However when I call it:
char result[2048];
readlist("test.txt", result);
printf("%s", result);
I'm getting an empty output. It seems that strtok() messes up the data, but I might be wrong.
What am I doing wrong here?
Thank you in advance!