Question

my program takes two command line inputs, stock.csv and coins.csv. Near the start of my program I have load_check = load_data(&tm, argv[1], argv[2]); which initializes the files into linked lists. Problem is, somehow this line of code is changing the value of argv[1] to d, but argv[2] is still coins.csv. I'm really confused as to what's causing this, it's really weird. Any ideas?

EDIT: Sorry about the slow response, found another problem with the code and was fixing it. Code for load_data (now changing argv[1] to "")

BOOLEAN load_data(tm_type * tm, char * stockfile, char * coinsfile)
{
stock_node *sstream = NULL;
char* token;
char line_no[LINE_LENGTH + 1], *ptr;
FILE *sfile, *cfile;
BOOLEAN check = TRUE;
int denom = 0;

if((sfile = fopen(stockfile, "r")) == NULL){

    fprintf(stderr, "Error in %s.\n", stockfile);
    check = FALSE;

}

if((cfile = fopen(coinsfile, "r")) == NULL){

    fprintf(stderr, "Error in %s.\n", coinsfile);
    check = FALSE;

}

sstream = tm -> stock -> head_stock;

while(fgets(line_no, LINE_LENGTH + 1, sfile) != NULL){

    sstream = malloc(sizeof(tm -> stock));
    sstream -> data = malloc(sizeof(struct stock_data));
    token = strtok(line_no, ",");
    strcpy(sstream -> data -> ticket_name, token);
    token = strtok(NULL, ",");
    sstream -> data -> ticket_type = token[0];
    token = strtok(NULL, ",");
    strcpy(sstream -> data -> ticket_zone, token);
    token = strtok(NULL, ",");
    sstream -> data -> ticket_price = strtol(token, &ptr, 10);
    token = strtok(NULL, ",");
    sstream -> data -> stock_level = strtol(token, &ptr, 10);
    sstream -> next_node = tm -> stock -> head_stock;
    tm -> stock -> head_stock = sstream;

}

while(fgets(line_no, LINE_LENGTH + 1, cfile) != NULL){

    token = strtok(line_no, ",");
    tm -> coins[denom].denomination = strtol(token, &ptr, 01);
    token = strtok(NULL, ",");
    tm -> coins[denom].count = strtol(token, &ptr, 10);
    denom++;

}

return check;
}

The variable change is happening in the fifth iteration of

while(fgets(line_no, LINE_LENGTH + 1, cfile) != NULL){

    token = strtok(line_no, ",");
    tm -> coins[denom].denomination = strtol(token, &ptr, 01);
    token = strtok(NULL, ",");
    tm -> coins[denom].count = strtol(token, &ptr, 10);
    denom++;

}

Also, tm -> coins[denom].denomination = strtol(token, &ptr, 01); is giving me a warning: Invalid parameter passed to C runtime function. error but doesn't seem to be affecting the code any other way.

Definitions: (These were pre-provided startup codes, this is for a class)

typedef struct stock_list * stock_list_ptr;
typedef struct coin * coin_list_ptr;
typedef struct tm
{
coin_list_ptr coins;
stock_list_ptr stock;
} tm_type;

struct stock_data 
{
char ticket_name[TICKET_NAME_LEN+1];
char ticket_type;
char ticket_zone[TICKET_ZONE_LEN+1];
unsigned int ticket_price;
unsigned int stock_level;
};
typedef struct stock_node 
{
struct stock_data * data;
struct stock_node * next_node;
} stock_node;

struct stock_list
{
stock_node * head_stock;
unsigned int num_stock_items;
};
Was it helpful?

Solution

Why don't you change the signature to:

BOOLEAN load_data(tm_type * tm, const char * stockfile, const char * coinsfile)

This way, you ensure that load_data doesn't modify the data that argv[1] and argv[2] point to. That won't solve your problem though.

Reg the warning,

tm -> coins[denom].denomination = strtol(token, &ptr, 01);

is 01 a typo or is denomination an enum with only 2 values?

Also, its important to initialize your pointers and close the file handles.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top