Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

Aranince

macrumors 65816
Original poster
Apr 18, 2007
1,104
0
California
I have this(albeit messy) code that parses a string and fills a struct with the data. However, I get weird results.

Code:
int main(int argc, char **argv)
{
 
    Task *task = load_task("Check email,none,computer,0,1,1|");
    
    printf("%s\n", task_serialize(task));
    free(task);
    return 0;
}

char *task_serialize(Task *task)
{
    char *result = malloc(sizeof(task)); 
    sprintf( result, "%s,%s,%s,%i,%i,%i|",
            task->task,
            task->project,
            task->context,
            task->priority,
            task->done,
            task->nextAction);
                                                        
    return result;
}

Task *load_task(char *data)
{
    Task *result = malloc(sizeof(Task));
    
    // First get the task description
    char *i = strchr(data, ',');
    int in = i - data;
    char *task = malloc(in);
    strncpy(task, data, in - 1);
    
    result->task = task;
    free(task);
    
    // Remove the task desc.
    //strncpy(data, data + in, strlen(data));
    
    // Get the task project
    i = strchr(data, ',');
    in = in + i - data;
    char *project = malloc(in);
    strncpy(project, data, in - 1);
    
    result->project = project;
    free(project);
    
    // Remove the task desc.
    //strncpy(data, data + in, strlen(data));
    
    // Get the context
    i = strchr(data, ',');
    in = in + i - data;
    char *context = malloc(in);
    strncpy(context, data, in - 1);
    
    result->context = context;
    free(context);
    
    // Remove the task desc.
    //strncpy(data, data + in, strlen(data));
    
    // Get the priority
    i = strchr(data, ',');
    in = in + i - data;
    char *priority = malloc(in);
    strncpy(priority, data, in - 1);
    
    result->priority = atoi(priority);
    free(priority); 
    
    // Remove the task desc.
    //strncpy(data, data + in, strlen(data));
    
    // Get if the task is done
    i = strchr(data, ',');
    in = in + i - data;
    char *done = malloc(in);
    strncpy(done, data, in - 1);
    
    result->done = atoi(done);
    free(done);
    
    // Remove the task desc.
    //strncpy(data, data + in, strlen(data));
    
    // Get if the task is next action
    i = strchr(data, ',');
    in = in + i - data;
    char *nextaction = malloc(in);
    strncpy(nextaction, data, in - 1);
    
    result->nextAction = atoi(nextaction);
    free(nextaction);
    
    return result;
}

I get this weird output:
Check email,none,computer,0,1,1|,,computer,0,1,1|,,0,0,0|

It should be what I inputted:
Check email,none,computer,0,1,1|
 

Aranince

macrumors 65816
Original poster
Apr 18, 2007
1,104
0
California
This doesn't seem to work

Code:
       int size = strlen(task);
    size += strlen(project);
    size += strlen(context);
    // one for priority
    size += 1;
    // one for done bool
    size += 1;
    // one for nextAction bool
    size += 1;
    
    Task *result = malloc(size);
    
    result->task = task;
    result->project = project;
    result->context = context;
    result->priority = atoi(priority);
    result->done = atoi(done);
    result->nextAction = atoi(nextaction);
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.