Domanda

Here are some chunks of my code to give you a view of my problem

    typedef struct {
    int count;
    char *item;
    int price;
    char *buyer;
    date *date;
    }transaction;

transaction *open(FILE *src, char* path) {
char buffer[100], *token;
int count = 0;
transaction *tlist = (transaction*)malloc(sizeof(transaction));
tlist = alloc(tlist, count);
src = fopen(path, "r");
if (src != NULL) {
    printf("\nSoubor nacten.\n");
}
else {
    printf("\nChyba cteni souboru.\n");
    return NULL;
}
while (fgets(buffer, sizeof(buffer), src)) {
    tlist = alloc(tlist, count+1);
    token = strtok(buffer, "\t"); //zahodit jméno obchodníka
    tlist[count].count = strtok(NULL, "x");
    tlist[count].item = strtok(NULL, "\t");
    tlist[count].item++;
    tlist[count].item[strlen(tlist[count].item)] = '\0';
    tlist[count].price = atoi(strtok(NULL, "\t "));
    token = strtok(NULL, "\t"); //zahodit md
    tlist[count].buyer = strtok(NULL, "\t");
    tlist[count].date = date_autopsy(strtok(NULL, "\t"));
    count++;
}
fclose(src);
return tlist;
}

transaction *alloc(transaction *tlist, int count) {
if (count == 0) {
    tlist[0].item = (char*)malloc(20 * sizeof(char));
    tlist[0].buyer = (char*)malloc(20 * sizeof(char));
}
else {
    tlist = (transaction*)realloc(tlist, count * sizeof(transaction));
    tlist[count - 1].item = (char*)malloc(20 * sizeof(char));
    tlist[count - 1].buyer = (char*)malloc(20 * sizeof(char));
}
return tlist;
}

First in main(), I create the list

transaction *list = (transaction*)malloc(sizeof(transaction));

Then with the right command, I call my opening function that loads a file, then it tokens a line from that file into pieces that then puts into the structure. It all works fine.. When I want to print(for testing) tlist[count].item inside the opening function, it prints the right thing. But when I try it outside(in main()), it prints garbage. It somehow works for the date and price parts of sturcture.. I assume the "buyer" string will be broken as well. Thanks in advance

È stato utile?

Soluzione

  1. Since you are overwriting the allocated memory with the local buffer for the item , bueyr fields so it is not reflected in the caller function. Modify the code as below

    tlist[count].count = strtok(NULL, "x") -> strcpy(tlist[count].count, strtok(NULL, "x"))

    tlist[count].buyer = strtok(NULL, "\t") -> strcpy(tlist[count].buyer , strtok(NULL, "\t"))

    and also check the tlist[count].date , You should allocate the memory for the date and also use memcpy to copy the contents.

  2. Since strtok returns the NULL termintated string what is the use of the following lines ?

    tlist[count].item++;

    tlist[count].item[strlen(tlist[count].item)] = '\0';

Altri suggerimenti

strtok holds an internal static buffer which is filled and returned after every call.

somerthing like this:

char *strtok(...)
{
  static char buf[XX];

  // get next token

  return buf;
}

You are effectively assigning and therefore overwriting the data the pointer points to after every call. A better use would be to allocate memory for the char * fields and use strcpy for the data returned from strtok

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top