add a data parser to dump content of the file

This commit is contained in:
JT Acquaviva 2019-08-25 22:35:07 +02:00
parent fed97e48bc
commit db7e403a10
1 changed files with 33 additions and 2 deletions

View File

@ -5,17 +5,28 @@
#include <stdint.h> #include <stdint.h>
#include <stdbool.h> #include <stdbool.h>
#include <float.h> #include <float.h>
#include <time.h>
#include <errno.h>
/* /*
* Generate test data * Generate test data
*/ */
// generate float number between 0 and 99 with at most 2 decimal
float gen_float(void){
u_int32_t my_int = rand() % 10000;
float my_float = (float) my_int / 100;
return my_float;
}
// generate some data such that we can reduce it later // generate some data such that we can reduce it later
void prepare_data(int size){ void prepare_data(int size){
float * data = malloc(size * sizeof(float)); float * data = malloc(size * sizeof(float));
for(int i=0; i < size; i++){ for(int i=0; i < size; i++){
data[i] = (float) i; data[i] = (float) gen_float();
} }
FILE * file = fopen("data.bin", "wb"); FILE * file = fopen("data.bin", "wb");
@ -25,8 +36,28 @@ void prepare_data(int size){
free(data); free(data);
} }
// parse generated data
void parse_data(int size){
float data ;
FILE * file = fopen("data.bin", "r");
for(int i=0; i < size; i++){
int res = fread(&data, sizeof(float), 1, file);
if (res != 1){
res = errno;
fprintf(stderr, "fread failed to read the right amount of float: %s\n",strerror(res));
exit(EXIT_FAILURE);
}
fprintf(stdout, "%.2f ", data);
}
fclose(file);
}
int main(){ int main(){
int size = 100; int size = 40;
srand(time(NULL));
prepare_data(size); prepare_data(size);
return 0; return 0;
} }