It was in my function prototype. I was trying to pass a character string to a function that opens and reads the file. I didn't realize to do that, I needed to pass a FILE *pointer. Here it is, a bit more complete.
In my main, I'm initializing the strings and passing by reference to my function.
Code:
int main (void){
char *source = "zips_short.txt";
char *dest = "binary.bin";
int n_read = 0;
int n_skip = 0;
ZIPS_create_bin_from_ascii(source, dest, &n_read, &n_skip);
printf("n_read = %d\n", n_read);
printf("n_skip = %d\n", n_skip);
system("pause");
return 0;
}
Purpose of the function is to read a text file containing record data, line by line, and send them to a parse function to be validated and written to a structure.
Code:
int ZIPS_create_bin_from_ascii(char *ascii_file, char *binary_file,
int *recs_read, int *recs_skipped){
FILE *sfp = NULL;
FILE *dfp = NULL;
char line[512];
ZIPS_data_t rec;
sfp = open_source(ascii_file);
dfp = open_dest(binary_file);
while(fgets(line,512,sfp)!=NULL){
line[strlen(line)-1] = '\000';
memset(&rec, 0, sizeof(rec));
if(ZIPS_parse_zips_rec(line, &rec)){
write_rec(dfp, rec);
++(*recs_read);
}
else
++(*recs_skipped);
}
fclose(sfp);
fclose(dfp);
}
These functions support the last one.
Code:
FILE* open_source(char *sfp){
FILE *file = fopen(sfp, "r");
if(file == NULL){
printf("Could not open source file: %s\n", sfp);
exit (1);
}
return file;
}
Code:
FILE* open_dest(char *dfp){
FILE *file = fopen(dfp, "wb");
if(file == NULL){
printf("Could not open destination file: %s\n", dfp);
exit (1);
}
return file;
}
Code:
int write_rec(FILE *fp, ZIPS_data_t frec)
fwrite(&frec, sizeof(frec), 1, fp);
I know I should likely combine the open functions, and prompt the user for the file names and modes, but what else in here reaks of poor practices.