The world’s Largest Sharp Brain Virtual Experts Marketplace Just a click Away
Levels Tought:
Elementary,Middle School,High School,College,University,PHD
| Teaching Since: | Apr 2017 |
| Last Sign in: | 103 Weeks Ago, 3 Days Ago |
| Questions Answered: | 4870 |
| Tutorials Posted: | 4863 |
MBA IT, Mater in Science and Technology
Devry
Jul-1996 - Jul-2000
Professor
Devry University
Mar-2010 - Oct-2016
hi, can anyone help me to write the main function for t1.c (attached file)?
Â
Â
#include "intarr.h"
#include <stdlib.h>
#include <stdio.h>
/**********************************************/
/*
Save the entire array ia into a file called 'filename' in a binary
file format that can be loaded by intarr_load_binary(). Returns
zero on success, or a non-zero error code on failure. Arrays of
length 0 should produce an output file containing an empty array.
*/
int intarr_save_binary( intarr_t* ia, const char* filename )
{
int *arr=ia->data;
int n=ia->len;
int i;
FILE *p;
p=fopen(filename,"wb");
if(p!=NULL)
{
for(i=0;i<n;i++)
{
fwrite(&arr[i],sizeof(int), 1, p);
}
fclose(p);
return 0;
}
return 1;
}
/*
Load a new array from the file called 'filename', that was
previously saved using intarr_save_binary(). Returns a pointer to a
newly-allocated intarr_t on success, or NULL on failure.
*/
intarr_t* intarr_load_binary( const char* filename )
{
FILE *p;
p=fopen(filename,"rb");
intarr_t *q;
if(p!=NULL)
{
int init=(int)ftell(p);
fseek(p,sizeof(int),SEEK_END);
int k=(int)ftell(p);
int n=(k-init-1)/sizeof(int);
int x;
q=intarr_create(n);
int count=0;
rewind(p);
while(fread(&x,sizeof(x),1,p)>0)
{
q->data[count++]=x;
}
}
return q;
}