I was hoping someone could help me point out what is wrong in my program as I have a hard time figuring out. So basically I have a structure Location and a data file that gives fields separated by "|", for instance 1|VA|12345|New York|... (20 fields in total every line, the first field is a unique ID but they're out of order, some fields are optional [blank] but the first field will always be there)
struct _Location{
char** array; };
int compareLoc(const void *left, const void *right){
const Location *l = (Location*) left;
const Location *r = (Location*) right;
if (l->array[0]<r-> array[0]){return -1;}
else if (l->array[0]==r-> array[0]){return 0;}
else{return 1;}
}
int main(int argc, char* argv[]){
FILE* GISfile = fopen(argv[1], "r");
typedef struct _Location Location;
Location l[argv[2]]; //the size is given as a command
int count =0;
while (1){ l[count].array = malloc(20*sizeof(char*));
l[count].array[0] = malloc(121*sizeof(char)); //max data length of 121
if (fscanf(GISfile, "%[^|]s", l[count].array[0]==1){
for (int i=1; i<20; i++){
l[count].array = malloc(121*sizeof(char));
}
}
else {break;}
}
Location* point = l; qsort (l, size,sizeof(Location), compareLoc);
}
any insight? I know it is probably something to do with the way I'm reading in the data but I can't figure out why it would be inaccessable since I ensure that each spot is allocated... (perhaps it has to do with understanding the professor's given "size" and getting rid of null entries if size is generously too big, but I would like to make sure I haven't made any other logic errors up to this point)
struct _Location{
char** array; };
int compareLoc(const void *left, const void *right){
const Location *l = (Location*) left;
const Location *r = (Location*) right;
if (l->array[0]<r-> array[0]){return -1;}
else if (l->array[0]==r-> array[0]){return 0;}
else{return 1;}
}
int main(int argc, char* argv[]){
FILE* GISfile = fopen(argv[1], "r");
typedef struct _Location Location;
Location l[argv[2]]; //the size is given as a command
int count =0;
while (1){ l[count].array = malloc(20*sizeof(char*));
l[count].array[0] = malloc(121*sizeof(char)); //max data length of 121
if (fscanf(GISfile, "%[^|]s", l[count].array[0]==1){
for (int i=1; i<20; i++){
l[count].array = malloc(121*sizeof(char));
}
}
else {break;}
}
Location* point = l; qsort (l, size,sizeof(Location), compareLoc);
}
any insight? I know it is probably something to do with the way I'm reading in the data but I can't figure out why it would be inaccessable since I ensure that each spot is allocated... (perhaps it has to do with understanding the professor's given "size" and getting rid of null entries if size is generously too big, but I would like to make sure I haven't made any other logic errors up to this point)