Two codes from different programs. Each should be exactly the same. No problems in one; the other fails at strcpy(arr[fld],p) in parse which runs when it is commented out. Do see anything I have missed? Any suggestions on how to debug? Could it be entirely unrelated to this even though that's where it fails.
from one program:
from another code
from one program:
Code:
arr = (char **)calloc(100, sizeof(char*));
for(j = 0; j < 100; j++)
arr[j] = (char *)calloc(10,sizeof(char));
fscanf(fp, "%s\n", listname); //get first file
action = fopen(listname, "r"); //open first file
fscanf(action, "%s\n", line); // discard first lineprintf("%s\n", arr[i]);
printf("%s\n", listname);
while (fgets(tmp,1024,action) != 0)
{
parse(tmp,",",(char**)arr,&fldcnt); // whack record into fields
void parse( char *record, char *delim, char **arr,int *fldcnt)
{
char* p=strtok(record,delim);
int fld=0;
while(p)
{
strcpy(arr[fld],p);
fld++;
p=strtok('\0',delim);
}
}
from another code
Code:
arr = (char **)calloc(100, sizeof(char*));
for (i = 0; i < 100; i ++)
arr[i] = (char*)calloc(10, sizeof(char));
fscanf(fl, "%s\n", listname); //get first file
printf("%s\n", listname);
if ((action = fopen(listname, "r")) == NULL)
printf("can't open action\n");
fscanf(action, "%s\n", line); // discard first line
while (fgets(tmp,1024,action) != 0)
{
parse(tmp,",",(char**)arr,&fldcnt);
void parse( char *record, char *delim, char **arr,int *fldcnt)
{
char* p=strtok(record,delim);
int fld=0;
while(p)
{printf("%d %s\n", fld, arr[fld]);
//strcpy(arr[fld],p);
fld++;
p=strtok('\0',delim);
}
}