1

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!

Maxim Neaga
  • 921
  • 3
  • 17
  • 29

4 Answers4

2

You never initialized result, before the call to readlist or after.

just add before the call to readlist: strcpy(result, "");

Pradeep Pati
  • 5,779
  • 3
  • 29
  • 43
dmg
  • 4,231
  • 1
  • 18
  • 24
  • Thanks for reply, but this is not the problem (I tried that). – Maxim Neaga Apr 30 '13 at 03:35
  • i wonder if your problem is that you were reading a file with RETURNS and LINEFEEDS, and the RETURNS were messing your output and making it look as if nothing was matched. I recommend you redirect the output to a file (eg. ./program > save.output) and inspect the output in an editor. – dmg Apr 30 '13 at 05:37
2
char result[2048];

Initialize this statement otherwise result will contain garbage values as its an auto variable.

So use char result[2048] = ""; before readlist() is called in your main function.

Makoto
  • 104,088
  • 27
  • 192
  • 230
anshul garg
  • 473
  • 3
  • 7
1

Make sure you initialize result to an empty string. Either

char result[2048] = "";

in the caller, or

result[0] = '\0';

at the top of readlist().

John Kugelman
  • 349,597
  • 67
  • 533
  • 578
  • Thanks, but that doesn't help. If I change the while loop to just strcat(result, buffer); it returns the contents of entire file fine. – Maxim Neaga Apr 30 '13 at 03:36
1

I ran your code (or at least, I created a single program from the snippets of your code) and it worked fine for me:

#include <stdio.h>
#include <string.h>

void readlist(char* filename, char* result);

int main(void) {
char result[2048];
readlist("test.txt", result);
printf("%s", result);
}

void readlist(char* filename, char* result) {
    FILE *fp;
    char buffer[2048];

    fp = fopen(filename, "r");

    while (fgets(buffer, sizeof(buffer), fp)) {
        char *token = NULL;
        token = strtok( buffer, ":" );
        strcat(result, token);
    }


    fclose(fp);
}

When I ran it on your input file, I got as output

Name1Name2Name3

Exactly as expected.

This was using the gcc compiler version 4.2.1 on Mac OS . It suggests your code isn't as far off as you think (whether the compiler initializes the string to 0 before starting is implementation dependent, apparently). To be safe though, you need to make sure your initial result is all zeros. You could do

char result[2048] = {'\0'};

This would guarantee all elements are initialized to zero. Another method would be to use

static char result[2048];

Since any variable declared static will be initialized to zero.

Finally, things like

result[0] = '\0';

would start the string with zero length - and whatever follows doesn't matter. This is probably the cleanest.

Floris
  • 45,857
  • 6
  • 70
  • 122
  • Thank you for checking this out for me. I've spent many hours trying to figure out the problem. Looks like something is wrong with my compiler... – Maxim Neaga Apr 30 '13 at 03:57
  • Thanks for the accept, but I think you are jumping to conclusions about your compiler being "wrong". I think that behavior is "not defined" for things like this. Follow one of the suggestions I made for ensuring that `result` is initialized to something sensible (as was also pointed out by other answers) - don't just blame your compiler! – Floris Apr 30 '13 at 04:00
  • Yes, I tried that, however this was not the case. I have a while(1) {} loop later on in the main() function and for some reason it was causing the output getting stuck, even though the output code is before and outside the loop. Thank very much! – Maxim Neaga Apr 30 '13 at 04:10
  • Ah... The infamous "other bit of code" . This is the reason why it is always recommended that you ask your problems as small, self contained, complete programs that reproduce the problem. Well I am glad you figured it out! – Floris Apr 30 '13 at 10:52