J
john
Relatively new to C coding, so any help would greatly be appreciated.
I'm having problems try to return my string array from my parsing
function. When I do a printf I am getting the correct value for my first
element but my subsequent printf's will return garbage. Can someone let
me know what I am doing wrong. Thanks in advance.
-jlewis
typedef char wrkspace[40][200]; <- Locatated in my haeder
int open_file (char *path, char *recNo, int fid) {
FILE *fp;
char str [1000];
int ret;
int count;
int inx;
int i;
wrkspace *value;
if ((fp=fopen(path, "r"))==NULL) {
printf("Cannot open file.\n");
return -1;
}
count = atoi(recNo);
memset (str, NULL, 1000);
fgets( str, sizeof(str), fp );
while (!feof(fp)) {
for (inx = 0; (inx < count ) && (!feof(fp)) && ( strlen(str) > 0
); inx++) {
<--CALLING ROUTINE --->
value = (wrkspace *) parse_record(&str, fid, count);
printf ("0:%s\n", inx, value[0]); <-- returns good data
printf ("1:%s\n", inx, value[1]); <-- Everything else garbage
printf ("2:%s\n", inx, value[2]);
printf ("3:%s\n", inx, value[3]);
printf ("4:%s\n", inx, value[4]);
memset (str, NULL, 1000);
fgets( str, sizeof(str), fp );
}
}
fclose(fp);
return ret;
}
wrkspace *parse_record(char (*record) [], int fid, int structsz) {
int i=0;
int j;
char *token;
char sep_1[] = "|";
char sep_2[] = "'";
wrkspace value;
token = strtok( *record, sep_1 );
while( token != NULL ) { // While there are tokens in "string"
memset (value, NULL, 200);
strcpy (value, token);
token = strtok( NULL, sep_1 ); // Get next token:
i++;
}
j = i - 2; // Minus 2 is remove the last '|'
for (i=0; i <= j; i++) {
token = strtok (value, sep_2);
strcpy (value , token);
}
return &value;
}
I'm having problems try to return my string array from my parsing
function. When I do a printf I am getting the correct value for my first
element but my subsequent printf's will return garbage. Can someone let
me know what I am doing wrong. Thanks in advance.
-jlewis
typedef char wrkspace[40][200]; <- Locatated in my haeder
int open_file (char *path, char *recNo, int fid) {
FILE *fp;
char str [1000];
int ret;
int count;
int inx;
int i;
wrkspace *value;
if ((fp=fopen(path, "r"))==NULL) {
printf("Cannot open file.\n");
return -1;
}
count = atoi(recNo);
memset (str, NULL, 1000);
fgets( str, sizeof(str), fp );
while (!feof(fp)) {
for (inx = 0; (inx < count ) && (!feof(fp)) && ( strlen(str) > 0
); inx++) {
<--CALLING ROUTINE --->
value = (wrkspace *) parse_record(&str, fid, count);
printf ("0:%s\n", inx, value[0]); <-- returns good data
printf ("1:%s\n", inx, value[1]); <-- Everything else garbage
printf ("2:%s\n", inx, value[2]);
printf ("3:%s\n", inx, value[3]);
printf ("4:%s\n", inx, value[4]);
memset (str, NULL, 1000);
fgets( str, sizeof(str), fp );
}
}
fclose(fp);
return ret;
}
wrkspace *parse_record(char (*record) [], int fid, int structsz) {
int i=0;
int j;
char *token;
char sep_1[] = "|";
char sep_2[] = "'";
wrkspace value;
token = strtok( *record, sep_1 );
while( token != NULL ) { // While there are tokens in "string"
memset (value, NULL, 200);
strcpy (value, token);
token = strtok( NULL, sep_1 ); // Get next token:
i++;
}
j = i - 2; // Minus 2 is remove the last '|'
for (i=0; i <= j; i++) {
token = strtok (value, sep_2);
strcpy (value , token);
}
return &value;
}