RealityVelJackson
Programmer
Suppose I am reading in the following text file, I would like to properly handle missing data between the #-delims when reading.
#mike#tyson#11898
#carl#lopez#98878
#vincent##11212
#utah#smith#
...
typedef struct{
char* field1;
char* field2;
int field3;
} CUSTOMER;
int main(){
char* s;
CUSTOMER* C = (CUSTOMER*) malloc (sizeof(CUSTOMER));
FILE* fp = fopen("customers.txt", "r");
if(fp == NULL){
fprintf(stderr, "file not found\n");
exit(1);
}
while(!feof(fp)){
s = (char*) calloc (255, sizeof(char));
fgets(s, 255, fp);
/* Add error checking code for missing fields...*/
C->field1 = strtok(s, "#");
C->field2 = strtok(NULL, "#");
C->field3 = atoi(strtok(NULL, "#"));
printStructMembers(C);
free(s);
}
free(C);
return 0;
}
#mike#tyson#11898
#carl#lopez#98878
#vincent##11212
#utah#smith#
...
typedef struct{
char* field1;
char* field2;
int field3;
} CUSTOMER;
int main(){
char* s;
CUSTOMER* C = (CUSTOMER*) malloc (sizeof(CUSTOMER));
FILE* fp = fopen("customers.txt", "r");
if(fp == NULL){
fprintf(stderr, "file not found\n");
exit(1);
}
while(!feof(fp)){
s = (char*) calloc (255, sizeof(char));
fgets(s, 255, fp);
/* Add error checking code for missing fields...*/
C->field1 = strtok(s, "#");
C->field2 = strtok(NULL, "#");
C->field3 = atoi(strtok(NULL, "#"));
printStructMembers(C);
free(s);
}
free(C);
return 0;
}