I have a very large (half a million plus records) data file that is made up of records of 25 text characters.
3 sample records
----------------
AB10 043SAA1011NYYYYYN?
AB103 043SAA1011NYYYYYN?
AB103A 043SAA1011NYYYYYN?
(displayed on seperate rows for ease of viewing but actual file does not contain carriage returns)
Because this file is very large (10Mb+) I wish to compact it to make it easier to distribute (possibly looking at weekly updates to 5000 users). To do this I wish to turn it into a binary file. This will also have the added bonus of making the file unreadable to the human eye.
My problem is that the my output file is identical to the input one. Same size and also readable in notepad.
Can anyone spot what I'm doing wrong and point me in the right direction.
Alternatively, any suggestions for making the file smaller but not reducing the time it takes to read would be welcome.
Putting the data in a database is not possible as the program using the data has to run on windows AND *nix and we don't want to have to support multiple versions of the data.
I've spent the last three days on this and searching all over the web and am posting here in desperation.
Code below
***************************************
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct gazrec GAZ;
struct gazrec {
char details[25];
};
int main(void) {
char c[25];
FILE *file;
GAZ *gazrec = NULL;
int reccnt = 0;
int i;
FILE *fp;
file = fopen("test.asc", "r"
; /*open text file*/
if(file==NULL){
printf("Error: can't open file.\n"
;
return 1;
} else {
while(fgets(c, 26, file)!=NULL) { /*get (size of structure) bytes from file*/
gazrec = realloc(gazrec, (reccnt+1)*sizeof(GAZ)); /*increase size of array to include room for new record*/
for(i=0 ; i<25 ; i++) { /*copy data from file into array*/
gazrec[reccnt].details = c;
}
printf("'%s' \n", gazrec[reccnt].details); /*display current record so can see something happening*/
reccnt++;
}
fclose(file); /*close text file*/
fp = fopen( "test.bin", "wb" ); /*open binary file*/
fwrite( gazrec, sizeof(GAZ), reccnt, fp ); /*write array to binary file*/
fclose( fp ); /*close binary file*/
if(gazrec!=NULL) { /*free memory allocated to array*/
free(gazrec);
}
return 0;
}
}
3 sample records
----------------
AB10 043SAA1011NYYYYYN?
AB103 043SAA1011NYYYYYN?
AB103A 043SAA1011NYYYYYN?
(displayed on seperate rows for ease of viewing but actual file does not contain carriage returns)
Because this file is very large (10Mb+) I wish to compact it to make it easier to distribute (possibly looking at weekly updates to 5000 users). To do this I wish to turn it into a binary file. This will also have the added bonus of making the file unreadable to the human eye.
My problem is that the my output file is identical to the input one. Same size and also readable in notepad.
Can anyone spot what I'm doing wrong and point me in the right direction.
Alternatively, any suggestions for making the file smaller but not reducing the time it takes to read would be welcome.
Putting the data in a database is not possible as the program using the data has to run on windows AND *nix and we don't want to have to support multiple versions of the data.
I've spent the last three days on this and searching all over the web and am posting here in desperation.
Code below
***************************************
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct gazrec GAZ;
struct gazrec {
char details[25];
};
int main(void) {
char c[25];
FILE *file;
GAZ *gazrec = NULL;
int reccnt = 0;
int i;
FILE *fp;
file = fopen("test.asc", "r"
if(file==NULL){
printf("Error: can't open file.\n"
return 1;
} else {
while(fgets(c, 26, file)!=NULL) { /*get (size of structure) bytes from file*/
gazrec = realloc(gazrec, (reccnt+1)*sizeof(GAZ)); /*increase size of array to include room for new record*/
for(i=0 ; i<25 ; i++) { /*copy data from file into array*/
gazrec[reccnt].details = c;
}
printf("'%s' \n", gazrec[reccnt].details); /*display current record so can see something happening*/
reccnt++;
}
fclose(file); /*close text file*/
fp = fopen( "test.bin", "wb" ); /*open binary file*/
fwrite( gazrec, sizeof(GAZ), reccnt, fp ); /*write array to binary file*/
fclose( fp ); /*close binary file*/
if(gazrec!=NULL) { /*free memory allocated to array*/
free(gazrec);
}
return 0;
}
}