Array of struct from binary file - c

I have to write a function that will read an array of structures of type Product with data from a binary file.This file contains the number of products - nr and a number of articles of type Product. What's wrong? Thank you in advance!
#define SIZE 30
typedef struc{
int id;
char[SIZE] name;
float price;
}Product;
void create(Product *p, FILE *fptr)
{
p = malloc(sizeof(Product));
fread(p, 1, sizeof(Product), fptr);
}
int main(int argc, char* argv[])
{
FILE *fptr = fopen(argv[1],"rb");
Product *p;
create(p, fptr);
return 0;
}

You have to modify it to something like this:
#include <stdio.h>
#include <stdlib.h>
#define SIZE 30
typedef struct{
int id;
char name[SIZE];
float price;
}Product;
int readproducts(Product *p, FILE *fptr, int nr)
{
if(nr != fread(p, sizeof(Product), nr, fptr))
return -1;
return 0;
}
int main(int argc, char* argv[])
{
FILE *fptr = fopen(argv[1],"rb");
int nr = 0;
if(NULL == fptr) return -1;
// First read number of products from file
// Assuming this number is written as 4 byte integer - at the start of file
if(fread(&nr, 4, 1, fptr) != 1)
return -1;
// Now, read the products
Product *p = malloc(nr * sizeof(Product));
if(-1 == readproducts(p, fptr, nr))
return -1;
fclose(fptr);
return 0;
}
The way you had used malloc in your function was wrong, see here why.
PS. That said, binary writing/reading might not be portable across different computers.

Related

How do you assign structs into an array?

I have currently made this much of the code:
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <ctype.h>
#define STRSIZE 21
struct PInven{
int count;
struct PItem{
char name[STRSIZE];
int amount;
}Pitem;
}Pinven;//this needs to be an output file
int ReadInProduce (){
//read in file and check to see if the file exist or not.
FILE * PinFile = fopen("produce.txt","r");
if (PinFile == NULL){
printf("ERROR: WRONG FILE");
}
else{
printf("I did it!!\n");
}
//assigning the value gotten into the struct variable(but need to maybe change this since it needs to be an output)
fscanf(PinFile,"%d",&Pinven.count);
printf("%d\n", Pinven.count);
int i;
for(i =0; i <Pinven.count; i++){
fscanf(PinFile,"%20s %d",Pinven.Pitem.name, &Pinven.Pitem.amount);
printf("%s %d\n",Pinven.Pitem.name, Pinven.Pitem.amount);
}
//making an array to hold the variables
//FILE * PoutFile = fopen("produce_update.txt","w");
fclose(PinFile);
return 0;
}
From there I want to get the file that is read to the structs to be printed out into an array so that later on I can make a function that will be able to compare to the to it.
Basically a store management system. Where the file of the inventory is read in and compared to the file that is store and return a new value for the amount of produce now either left or gained.
10 //number of items that will be stored in the store
apple 19
banana 31
broccoli 9
...
In general, it's a really bad idea to include header information in the file about the number of entries in the file. You want to be able to do stream processing, and that will be more difficult if you need that meta-data. More importantly, it is important to understand how to write the code so that you don't need it. It's not really that difficult, but for some reason people avoid it. One simple approach is just to grow the array for each entry. This is horribly inefficient, but for the sake of simplicity, here's an example that expects the file not not include that first line:
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <ctype.h>
#include <limits.h>
#define STRSIZE 128
struct PItem{
char name[STRSIZE];
int amount;
};
struct PInven{
int count;
struct PItem *PItem;
};
static void
grow(struct PInven *p)
{
p->PItem = realloc(p->PItem, ++p->count * sizeof *p->PItem);
if( p->PItem == NULL ){
perror("out of memory");
exit(1);
}
}
int
ReadInProduce(struct PInven *P, const char *path)
{
FILE * PinFile = fopen(path, "r");
if( PinFile == NULL ){
perror(path);
exit(1);
}
char fmt[64];
int max_len;
max_len = snprintf(fmt, 0, "%d", INT_MAX);
snprintf(fmt, sizeof fmt, "%%%ds %%%dd", STRSIZE - 1, max_len - 1);
grow(P);
struct PItem *i = P->PItem;
while( fscanf(PinFile, fmt, i->name, &i->amount) == 2 ){
i += 1;
grow(P);
}
P->count -= 1;
fclose(PinFile); /* Should check for error here! */
return P->count;
}
int
main(int argc, char **argv)
{
struct PInven P = {0};
char *input = argc > 1 ? argv[1] : "produce.txt";
ReadInProduce(&P, input);
struct PItem *t = P.PItem;
for( int i = 0; i < P.count; i++, t++ ){
printf("%10d: %s\n", t->amount, t->name);
}
}
As an exercise for the reader, you should add some error handling. At the moment, this code simply stops reading the input file if there is bad input. Also, it would be a useful exercise to do fewer reallocations.
you should change Structure of PInven to it can save a dynamic array of Pitem with a Pitem pointer.
tested :
#include <ctype.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#define STRSIZE 21
typedef struct {
char name[STRSIZE];
int amount;
} Pitem;
struct PInven {
int count;
Pitem *pitem;
} Pinven; // this needs to be an output file
int main() {
// read in file and check to see if the file exist or not.
FILE *PinFile = fopen("produce.txt", "r");
if (PinFile == NULL) {
printf("ERROR: WRONG FILE");
} else {
printf("I did it!!\n");
}
// assigning the value gotten into the struct variable(but need to maybe
// change this since it needs to be an output)
fscanf(PinFile, "%d", &Pinven.count);
Pinven.pitem = (Pitem *)malloc(sizeof(Pitem) * Pinven.count);
printf("%d\n", Pinven.count);
int i;
for (i = 0; i < Pinven.count; i++) {
fscanf(PinFile, "%20s %d", Pinven.pitem[i].name,
&Pinven.pitem[i].amount);
// printf("%s %d\n",Pinven.pitem[i].name, Pinven.pitem[i].amount);
}
for (i = 0; i < Pinven.count; i++) {
printf("%s %d\n", Pinven.pitem[i].name, Pinven.pitem[i].amount);
}
// making an array to hold the variables
// FILE * PoutFile = fopen("produce_update.txt","w");
fclose(PinFile);
// remember free
free(Pinven.pitem);
return 0;
}

Combining two files of floats into a single file in C

My task is to read from two files both containing floats. Then I have to put them in a new file in an ascending order. The code is as follows:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int numLines(const char * fileName){
FILE *file=fopen(fileName,"r");
int lines=0;
char c;
while((c=fgetc(file))!=EOF){
if(c==10){
lines++;
}
}
fclose(file);
return lines;
}
float * fileToArray(const char * fileName){
FILE *file=fopen(fileName,"r");
int numOfLines= numLines(fileName);
float * arr= (float*)malloc(numOfLines*sizeof(float));
int i;
for(i=0;i<numOfLines;i++){
fscanf(file,"%f\n",&arr[i]);
}
fclose(file);
return arr;
}
int cmpfunc(const void *a, const void *b){
float fa= *(const float*) a;
float fb= *(const float*) b;
return (fa>fb)-(fa<fb);
}
int isSorted(FILE *fp){
float prev;
float o;
do{
fscanf(fp,"%f\n",&prev);
fscanf(fp,"%f\n",&o);
if(prev>o){
fclose(fp);
return 1;
}
}while(fscanf(fp,"%f\n",&o)==1);
fclose(fp);
return 0;
}
int main(int argc, const char * argv[]){
const char *fileName1;
const char *fileName2;
const char *fileOut;
FILE *fp1;
FILE *fp2;
FILE *fp3;
float *arr1;
float *arr2;
int size1;
int size2;
if(argc!=4){
printf("Usage: fileSort.exe FILENAME1 FILENAME2 FILEOUT");
return 0;
}
else{
fileName1= argv[1];
fileName2= argv[2];
fileOut= argv[3];
fp1= fopen(fileName1,"r");
fp2= fopen(fileName2, "r");
fp3= fopen(fileOut, "w");
if(fp1!=NULL && fp2!=NULL){
arr1=fileToArray(fileName1);
arr2=fileToArray(fileName2);
size1=sizeof(arr1)/sizeof(float);
size2=sizeof(arr2)/sizeof(float);
if(!isSorted(fp1) || !isSorted(fp2)){
printf("The files are not sorted in increasing order. Please sort them.");
}
else{
float * arr3= malloc((size1+size2)*sizeof(float));
int k;
memcpy(arr3,arr1,size1*sizeof(float));
memcpy(arr3+size1,arr2,size2*sizeof(float));
qsort(arr3,size1+size2,sizeof(float),cmpfunc);
for(k=0;k<(size1+size2);k++){
fprintf(fp3,"%f\n",arr3[k]);
}
}
fclose(fp1);
fclose(fp2);
fclose(fp3);
}
else{
printf("Files could not be opened\n");
}
return EXIT_SUCCESS;
}
Two sample files for testing this code would be:
data1.txt:
0.586399
0.769484
0.864755
6.229683
data2.txt:
0.279828
0.309235
0.591884
0.962811
1.361349
10.203892
12.158343
The result should be their ordered combination. However, I get this as a result:
0.279828
0.309235
0.586399
0.769484
No matter what I change, it always writes four numbers into the resulting file. The helper functions seem to be doing their jobs just fine. It looks like the problem comes up when I'm trying to create the third array and fill it up (or at least I assume that's the source). I have no idea where this problem is coming from though. I would really appreciate some help on this issue.
The variable arr1 is not an array, it is a pointer to a float even though you are using it as pointer to the first float element in the array.
This means that sizeof will return to you the size of the pointer which is either 4 bytes or 8 bytes depending whether you are compiler 32-bit or 64-bit.
One way to fix this is to pull out the calculation of the number of lines like this:
int size1 = numLines(fileName1);
int size2 = numLines(fileName2);
arr1=fileToArray(fileName1, size1);
arr2=fileToArray(fileName2, size2);

passing an array of structs from a function to the main program

I have a data file of about 2500 lines. Each line contains 5 parameters. I declare a strut out side of a routine.
typedef struct data
{
char date[9];
char time[3];
float price;
float NAV;
float percent_change;
} _data ;
void readfile(int *j, struct data *input);
void readfile(int *j,struct data *input)
I set aside memory to read each line of the file into an array of structs.
input = calloc(days_of_data,sizeof(*input));
for (i = 0; i< days_of_data; i++)
input[i] = *(_data*)calloc(1,sizeof(_data));
and read the file.
while(fgets(line, 75, fp) != NULL)
{
date = strtok(line,",");
strcpy(input[i].date,date);
time = strtok(NULL,",");
strcpy(input[i].time, time);
price = strtok(NULL,",");
input[i].price = atof(price);
NAV = strtok(NULL,",");
input[i].NAV = atof(NAV);
percent_change = strtok(NULL,",");
input[i].percent_change = atof(percent_change);
i--;
}
This works. Now I want to sent the structure to the main program.
int main(int argc, const char * argv[]) {
struct data *input;
readfile(&j, input);
printf("%f\n",input[0].percent_change);
}
This compiles but crashes on the print statement.
You will have to use return value
struct data * readfile(int *j) {
struct data *input;
/* read the data */
return input;
}
int main(int argc, const char * argv[]) {
struct data *input;
input = readfile(&j);
printf("%f\n",input[0].percent_change);
}
or pointer
void readfile(int *j, struct data **input_out) {
struct data *input;
/* read the data */
*input_out = input;
}
int main(int argc, const char * argv[]) {
struct data *input;
readfile(&j, &input);
printf("%f\n",input[0].percent_change);
}
to pass the data to the caller.

request for member 'x' in something not a structure or union and segmentation fault (lost)

//Goal: Load data from a file into the struct
typedef struct {
int year;
char* make;
char* model;
int miles;
} Car;
void fill_garage(Car** garage, char* cars, int* size);
int main(int argc, char** argv)
{
if(argc<3)
{
printf("Not enough arguments.\n");
return 0;
}
int size;
Car** garage;
fill_garage(garage, argv[1], &size);
return 0;
}
void fill_garage(Car** garage, char* cars, int* size)
{
int i;
FILE* inputF=fopen(cars, "r");
fscanf(inputF, "%d", size);
garage=malloc(sizeof(Car)*(*size));
for(i=0; i<(*size); i++)
{
garage[i].make=malloc(sizeof(char)*MAX_STRING_LEN);
garage[i].model=malloc(sizeof(char)*MAX_STRING_LEN);
//The line below causes a segmentation fault when there are no errors
fscanf(inputF, "%d %s %s %d", &garage[i].year, garage[i].make, garage[i].model, &garage[i].miles);
}
}
//I'm not expecting anyone to just give me the corrected code (though that would be nice) If you could point be to a resource where I could learn about pointers would be great. Here's the assignment sheet if you want to see it. http://pastebin.com/LNeCx70m
You have to pass the address of garage to fill_garage(), so this
Car** garage;
fill_garage(garage, argv[1], &size);
should be
Car *garage;
fill_garage(&garage, argv[1], &size);
/* ^ pass the address of garage */
You don't need malloc() for a fixed size string, just redefine your struct like
typedef struct {
int year;
char make[MAX_STRING_LEN];
char model[MAX_STRING_LEN];
int miles;
} Car;
Check fopen() and malloc() returned values, if they fail both return NULL, if you failed to open the file, then you will cause undefined behavior. Also check the return value of fscanf().
Include stdio.h for printf() and fscanf() and stdlib.h for malloc().
I fixed your code, so it must not have any issues in principle
#include <stdio.h>
#include <stdlib.h>
#define MAX_STRING_LEN 128
typedef struct {
int year;
char make[MAX_STRING_LEN];
char model[MAX_STRING_LEN];
int miles;
} Car;
void fill_garage(Car **garage, char *cars, int *size);
int main(int argc, char** argv)
{
if (argc < 3)
{
printf("Not enough arguments.\n");
return 0;
}
int size;
Car *garage;
fill_garage(&garage, argv[1], &size);
return 0;
}
void fill_garage(Car **output, char *cars, int *size)
{
int i;
FILE *inputF;
Car *garage;
if (output == NULL)
return;
*output = NULL;
inputF = fopen(cars, "r");
if (inputF == NULL)
return;
if (fscanf(inputF, "%d", size) != 1)
{
fclose(inputF);
return;
}
garage = malloc(sizeof(*garage) * *size);
*output = garage;
if (garage == NULL)
{
fclose(inputF);
return;
}
i = 0;
while ((i < *size) && (fscanf(inputF, "%d%127s%127s%d", &garage[i].year, garage[i].make,
garage[i].model, &garage[i].miles) == 4))
{
i += 1;
}
}

getting fscanf to store in structs in C

I have an assignment where I need to get Values of RLC circuits from a file and calculate the resonant frequency however my issue is when I use the fscanf function it reads only the first line of the file and the rest comes out as zeros .
#include <stdio.h>
data
int h;
typedef struct cct
{
int code[50];
float R[50];
float L[50];
float C[50];
} CCT;
int read(CCT cct[], int n_p, FILE* fp){
char temp;
if(fp==NULL){
printf("Error\n");
return -1;
}
fscanf(fp,"%d,%f,%e,%e\n", cct[n_p].code, cct[n_p].R,cct[n_p].L, &cct[n_p].C);
}
int main()
{
FILE* fp = fopen("U://datafile.txt", "rt");
int i = 0;
CCT cct[50];
int size;
while (!feof(fp)) {
read(cct, i, fp);
i++;
}
size = i;
for (i = 0; i < size; ++i)
printf("%d,%0.2f,%0.2f,%0.2f\n", cct[i].code[i], cct[i].R[i],
cct[i].L[i], cct[i].C[i]);
scanf("%d",&h);
fclose(fp);
}
and this is the data file
1,4.36,2.23e-2,4.65e-8
2,4.57,2.01e-2,5.00e-8
3,3.99,2.46e-2,4.82e-8
4,4.09,2.60e-2,4.70e-8
I would appreciate if someone could point put why it only gets the first line. Thanks
CCT is composed of multiple arrays (you have arrays of arrays, which is wrong for the exercise, but that's not the point) and you always write to the element zero of the arrays. For example, cct[n_p].code in fscanf() is the address of the array, which is identical to the address of cct[n_p].code[0]. Then you print code[i] in the output loop, which is blank except for i == 0.
fscanf(fp,"%d,%f,%e,%e", cct[n_p].code, cct[n_p].R,cct[n_p].L, cct[n_p].C);
...
printf("%d,%0.2f,%0.2f,%0.2f\n", cct[i].code[0], cct[i].R[0], cct[i].L[0], cct[i].C[0]);
Something like the following, perhaps
#include <stdio.h>
typedef struct cct {
int code;
float R;
float L;
float C;
} CCT;
int h;
int read(CCT cct[], int n_p, FILE* fp){
char temp;
if(fp==NULL){
printf("Error\n");
return -1;
}
fscanf(fp,"%d,%f,%e,%e\n", &cct[n_p].code, &cct[n_p].R, &cct[n_p].L, &cct[n_p].C);
}
int main(){
FILE* fp = fopen("U://datafile.txt", "rt");
int i = 0;
CCT cct[50];
int size;
while (!feof(fp)) {
read(cct, i, fp);
i++;
}
size = i;
for (i = 0; i < size; ++i)
printf("%d,%0.2f,%0.2f,%0.2f\n", cct[i].code, cct[i].R, cct[i].L, cct[i].C);
scanf("%d",&h);
fclose(fp);
}

Resources