Skip to content

Instantly share code, notes, and snippets.

@deevus
Last active August 29, 2015 14:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save deevus/053dfa26e1d2202fd971 to your computer and use it in GitHub Desktop.
Save deevus/053dfa26e1d2202fd971 to your computer and use it in GitHub Desktop.
#include "question2.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "question1.h"
int
load_data(FILE *fp, t_data *ptr_data) {
//read into array
fread(&ptr_data->n, BINARY_N_LENGTH, 1, fp);
//get n
int n = extract_little((char *)&ptr_data->n, 0, BINARY_N_LENGTH);
//allocate array
ptr_data->array = malloc(sizeof(t_double) * n);
//read into array
fread(ptr_data->array, sizeof(double), n, fp);
return n;
}
void
print_data(const char *filePath) {
//open file
FILE *fp = fopen(filePath, "rb");
//return if failed to open
if (fp == 0) {
printf("File could not be opened.");
return;
}
//get data
t_data fd;
int n = load_data(fp, &fd);
//print file data
printf("Data written on the file:\n");
printf("n = %d\n", n);
printf("array =\n\n");
int i;
double *ptr_array = fd.array;
for (i = 0; i < n; i++) {
double d = (*ptr_array++);
char* padding = (d > 0) ? " " : " ";
printf("%s%f\n", padding, d);
}
//free objects
free(fd.array);
fclose(fp);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment