Dump file Generate file sort.h 1. (50 points) Binary Record Sorting in Ascending
ID: 3890034 • Letter: D
Question
Dump file Generate filesort.h
1. (50 points) Binary Record Sorting in Ascending Order This program should be called fastsort.c. It should be placed inside the "Problem I" directory. You will have to write a sorting program. Your program should read in a file which has the following format You will have to build a sorting program called fastsort that takes in one of these generated files and sorts it based on the 4-byte key (the remainder of the record should of course be kept with the same key). The output is written to the specified output file, Generating Input Files To generate test files we have provided a simple program called generate. This program will gonerate input files to test your program. Using generate is simple. First you should compile it with the following command: she11% gcc-o generate generate. c -Wall -Werror
Explanation / Answer
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <assert.h>
#include <ctype.h>
#include <string.h>
//#include "sort.h"
#include <sys/types.h>
#include <sys/stat.h>
#include <string.h>
#ifndef __sort_h__
#define __sort_h__
// DO NOT EDIT THIS FILE
#define NUMRECS (24)
typedef struct __rec_t {
unsigned int key;
unsigned int record[NUMRECS];
} rec_t;
#endif // __sort_h__
//comp func for qsort
static int comprec_t(const void *p1, const void *p2)
{
int aa,bb;
aa=(*(rec_t *)p1).key;
bb=(*(rec_t *)p2).key;
return aa-bb;
}
void
usage(char *prog)
{
//fprintf(stderr, "usage: %s <-i file> ", prog);
fprintf(stderr, "Usage: fastsort -i inputfile -o outputfile ");
exit(1);
}
int
main(int argc, char *argv[])
{
// arguments
char *inFile = "/no/such/file";
char *outFile = "/no/such/file";
struct stat sb;//GW: per the stat eg.
// input params
int c;
opterr = 0;//??? => resolved by reading man opterr
c = getopt(argc, argv, "i:o:") ;
if(c==-1)
usage(argv[0]);
// printf("c=%d",c);
while (c != -1) {//This works
switch (c) {
case 'i':
inFile = strdup(optarg);
break;
case 'o'://This works
outFile = strdup(optarg);
break;
default:
usage(argv[0]);
}
c = getopt(argc, argv, "i:o:") ;
}
// printf("optstring=%s");
// open and create input file
int fd = open(inFile, O_RDONLY);
if (fd < 0) {
fprintf(stderr,"Error: Cannot open file %s ",inFile);
exit(1);
}
//Read the lines into an array or rec_t:r[];
int lc=0; //line counter
fstat(fd,&sb);//get the total line number in file
int numline;
numline=sb.st_size/sizeof(rec_t); // total line number in file
rec_t r[numline];
int rc;
while (1) {
rc = read(fd,&(r[lc]), sizeof(rec_t));//it seems when I use pointer arithmetic (&r+lc) wont work here
if (rc == 0) // 0 indicates EOF
break;
if (rc < 0) {
perror("read");
exit(1);
}
++lc; //increase line counter
}
(void) close(fd);
//sort the array r[]
qsort(r, numline, sizeof(rec_t), comprec_t);
//write the sorted array r[] into output file
// creat o/p file if non-exist; if exist, clear exiting content
fd = open(outFile, O_WRONLY|O_CREAT|O_TRUNC, S_IRWXU);//int open(const char *pathname, int flags, mode_t mode);
if (fd < 0) {
fprintf(stderr,"Error: Cannot open file %s ",outFile);
exit(1);
}
rc=write(fd,&(r),sizeof(r));
(void) close(fd);
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.