I need help with this task. Hints and suggestions appreciated. do as much as pos
ID: 3745362 • Letter: I
Question
I need help with this task. Hints and suggestions appreciated. do as much as possible and give me the exact code.
Stage 2 code : ( including structure description and fread commands)
#include <stdio.h> //for printf() function /i
#include <string.h> // for strcpy() function /
#include <stdbool.h> // for bool datatype /
#include<stdint.h>
#include<stdlib.h>
/* Structure definition as in filestruct-description.txt */
struct record {
unsigned char tin; //8 bit unsigned integer/
char love[10]; //fixed length string/
int curve; //32 bit boolean/
unsigned long history; //64 bit unsigned integer/
unsigned int ground; //32 bit unsigned integer/
float week; //32 bit floating point/
int whip; //32 bit boolean/
char bag; //8 bit integer/
bool kiss; //8 bit bool/
short riddle; //16 bit boolean/
short playground; //16 bit boolean/
unsigned char trains; //8 bit unsigned integer/
float quilt; //32 bit floating point/
char line; //8 bit char/
short discovery; //16 bit integer/
double step; //64 bit floating point/
};
int main(int argc, char **argv)xxxx
{
size_t buff;
int read=1;
FILE *f1;
struct record rec1;
//Specifies the filename
if (argc < 2)
{
fprintf(stderr,"Usage: %s inputfile ", argv[0]);
exit(1);
}
f1 = fopen(argv[1], "rb");
if (!f1)
{
fprintf(stderr, "%s doesn't exist! ", argv[1]);
exit(1);
}
printf("tin,love,curve,history,ground,week,whip,bag,kiss,riddle,playground,trains,quilt,line,discovery,step ");
while(read==1){
buff= buff + fread(&rec1.tin, sizeof(rec1.tin), 1, f1);
// printf("%hhu, ",rec1.tin);
buff= buff + fread(&rec1.love,sizeof(rec1.love),1,f1);
// printf("%s, ",rec1.love);
buff= buff + fread(&rec1.curve,sizeof(rec1.curve),1,f1);
buff= buff + fread(&rec1.history,sizeof(rec1.history),1,f1);
buff= buff + fread(&rec1.ground,sizeof(rec1.ground),1,f1);
buff= buff + fread(&rec1.week,sizeof(rec1.week),1,f1);
buff= buff + fread(&rec1.whip,sizeof(rec1.whip),1,f1);
buff= buff + fread(&rec1.bag,sizeof(rec1.bag),1,f1);
buff= buff + fread(&rec1.kiss,sizeof(rec1.kiss),1,f1);
buff= buff + fread(&rec1.riddle,sizeof(rec1.riddle),1,f1);
buff= buff + fread(&rec1.playground,sizeof(rec1.playground),1,f1);
buff= buff + fread(&rec1.trains,sizeof(rec1.trains),1,f1);
buff= buff + fread(&rec1.quilt,sizeof(rec1.quilt),1,f1);
buff= buff + fread(&rec1.line,sizeof(rec1.line),1,f1);
buff= buff + fread(&rec1.discovery,sizeof(rec1.discovery),1,f1);
buff= buff + fread(&rec1.step,sizeof(rec1.step),1,f1);
if(buff ==0){
read=0;
}
else{
printf( "%hhu, %s , %d, %lu , %u , %f, %d , %d, %d,%hi ,%hi , %u, %f, %c, %hx, %lf ",
rec1.tin,rec1.love,rec1.curve,rec1.history, rec1.ground,rec1.week, rec1.whip, rec1.bag,
rec1.kiss ,rec1.riddle,rec1.playground,rec1.trains,rec1.quilt,rec1.line,
rec1.discovery,rec1.step);
buff =0;
}
}
fclose(f1);
return 0;
}
Explanation / Answer
i have tried to go in details within the stipulated time allocated to me. Hope you will find it useful
1)here is how we can use qsort:--
int cmpfunc (const void * a, const void * b) {
return ( *(int*)a - *(int*)b );
values is array, num is size of array, cmpfunc:- is my function to compare records
2)below i am providing details of how structure will be created. First file will be read and data will be stored in structure . After that i will perform sorting on the array of records.
3)final step will be to output sorted records into a file.
/***********************************************************************************/
FILE *outfile;
File *readfile;
Struct record
{
int horse ;
char cat[100];
float paper;
bool train;
}
Typedef struct record record1; //structure to store record read by file
Record1 val1[100]// array of 100 records
int cmpfunc (const void * a, const void * b) {
return ( *(int*)a - *(int*)b );
// open file for writing
outfile = fopen ("file1.txt", "w");
if (outfile == NULL)
{
fprintf(stderr, " Error opend file ");
exit (1);
}
readfile1=fopen(“file2.txt”,”r”);
fread(val1[0],sizeof(input1),1,readfile);// create a loop to read 100 records
/* val1=realloc(val1,100*szeof(record1) if memory is issue*/
//now input1 structure has first record.
// we need to do sorting on all records
qsort(val1, 100, sizeof(record1), cmpfunc);
// write struct to file
fwrite (&val1[0], sizeof(struct person), 1, outfile);
fwrite (&val1[1], sizeof(struct person), 1, outfile);
if(fwrite != 0)
printf("contents to file written successfully ! ");
else
printf("error writing file ! ");
// close file
fclose (outfile);
return 0;