My file contains:
Hook,Mark,In, , ,Tire,Matt,Out,01/01/2001,Mike
My structure:
struct part {
char name [NAME_LEN+1];
char owner [OWNER_LEN+1];
char status [STATUS_LEN+1];
char date [DATE_LEN+1];
char renter [RENTER_LEN+1];
} parts [MAX_PARTS], temp [1];
And the code:
FILE *fp;
fp=fopen("parts.csv","r");
printf("%d", fp); //Added this for debugging
char buf[100];
int i;
for(i=0; i < MAX_PARTS; i++) {
while(i<100 && fgets(buf,sizeof(buf),fp) != NULL){
printf("%s", buf); //for debugging
strcpy(parts[i].name, strtok(buf,","));
strcpy(parts[i].owner, strtok(NULL,","));
strcpy(parts[i].status, strtok(NULL,","));
strcpy(parts[i].date, strtok(NULL,","));
strcpy(parts[i].renter, strtok(NULL,","));
}
}
In this example, I am trying to save "Hook" into parts[0].name, "Mark" to parts[0].owner, "In" into parts[0].status, and then blank strings in parts[0].date and parts[0].renter. Then i++, so it should save "Tire" into parts[1].name, "Matt" into parts[1].owner, etc. Once the end of the file is reached, the loop should exit and it should continue with whatever code follows it. When I print fp, it is not null so it is opening, and when I print buf, it prints my file contents, so up to that point it is working. But if I try to print parts[0] or part[1] they are both blank. Why is nothing being copied?