-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsort.c
110 lines (89 loc) · 1.93 KB
/
sort.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
// I pledge my honor that I have abided by the Stevens Honor System. - Aparajita Rana
#include "sort.h"
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
//comparator method needed for qsort
int comparator(const void *x, const void *y)
{
int l = *((int*) x);
int r = *((int*) y);
if (l==r)
return 0;
else if (l<r)
return -1;
else
return 1;
}
void mysort(char *inputfile, char *outputfile)
{
FILE *file;
FILE *file2;
file=fopen(inputfile,"r");
if(file==NULL)
{
printf("Cannot open file");
exit(1);
}
//in order to store integers in int array, we need the size
//each line is new number, so # of lines=int array size
char val;
int size=0;
for (val=getc(file); val!=EOF; val=getc(file))
{
if (val=='\n')
{
size++;
}
}
rewind(file);
//here we create and start storing the vals
int *numbers;
//finally, we have the correct (hopefully) amt of memory
numbers=(int *) malloc(sizeof(int)*size);
//store each val into numbers
int loc=0;
//int val_temp;
while(!feof(file))
{
fscanf(file, "%d",&numbers[loc]);
//numbers[loc]=val_temp;
loc++;
}
//utilize quicksort algorithm & comparator
qsort(numbers, size, sizeof(int), comparator);
fclose(file);
file2 = fopen(outputfile, "w"); //open writefile
if(file2==NULL)
{
printf("Cannot open file");
exit(1);
}
//write the numbers to the file
int x;
char temp[size];
//convert int array to char array
for(x = 0; x < size; x++)
{
snprintf(&temp[x], sizeof(temp), "%d",numbers[x]);
}
for(x = 0; x < size; x++)
{
fprintf(file2, "%d", numbers[x]);
fputc('\n', file2);
}
fclose(file2);
//we are told to also use free -> frees the memory
free(numbers);
}
int main(int argc, char **argv)
{
//report an error if fewer than or more than two arguments are provided
if(argc!=3)
{
printf("Error in number of arguments");
exit(1);
}
mysort(argv[1], argv[2]);
return(0);
}