Repository navigation
Expand file tree
/
Copy pathsecond.c
More file actions
280 lines (218 loc) · 3.91 KB
/
Copy pathsecond.c
File metadata and controls
280 lines (218 loc) · 3.91 KB
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
//Non-Static Array Second Assignment
//Hash function was provide by: https://github.com/aappleby/smhasher/tree/master/src
#include <stdlib.h>
#include <unistd.h>
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <ctype.h>
#include <string.h>
void addData(int data);
char* readData(int file);
void printData();
void printArray();
/*void LessThanPER(int ptr, int prev, int Array[], int data);
void LessThanPNER(int ptr, int prev, int Array[], int data );
void GreaterThanPER(int ptr, int prev, int Array[], int data);
*/
void Selectsort();
char* AllData;
int size;
int* Array;
//int A[0];
int main(int argc, char*argv[]){//Read File
//int Determinant = 0;
int fileOp = open(argv[1],O_RDONLY);
if(fileOp == -1){
printf("Error");
return 0;
}else if(fileOp==0){
//EMPTY FILE
printf("Error");
close(fileOp);
return 0;
}
else {// Create Buffer Reader
//Check
readData(fileOp);
//char* AllData = (char*)malloc(sizeof(char));
//printf("AllData first: %c\n", AllData);
if(AllData==NULL){
//NO DATA COULD BE READ!!
return 0;
}
char* str = (char*)malloc(sizeof(char));
str[0] = AllData[0];
size = atoi(str);
//printf("Size of AAAAAAray: %d", size);
//Now create arrray!!
//
//int A[];
//
//size=1;
Array=malloc(size*sizeof(int));
//GOOD
size=0;
int idx = 1;
//int y = 0;
/////////////////////////////////////////////
while(AllData[idx]!='\0'){
printf("CHAR :%c",AllData[idx]);
if(isdigit(AllData[idx])){
int f = idx;
//printf("MADE IT\n");
//printf("Char: %c",AllData[idx]);
char* string = malloc(sizeof(char));
string[0]='\0';
char* app = malloc(sizeof(char));
app[0]='\0';
//app[0]=AllData[f];
//app[1]='\0';
//f=idx;
//int t = 0;
while(isdigit(AllData[f])&&AllData[f]!='\0'){
printf("\nChar 2:%c\n",AllData[f]);
app[0]=AllData[f];
strcat(string,app);
f++;
}
printf("\nThe String: %s", string);
int value = atoi(string);
printf("VALUE %d\n",value);
//ADD VALUE TO ARRAY!!!!
addData(value);
//printf("VALUER!!
idx=f;
}
idx++;
}
//free(A);
printArray();
free(Array);
//ADD MORE MEMORY!!!
//
}
//free(A);
close(fileOp);
return 0;
}
void addData(int data){
if(size==0){
//printf("NULL FIRST");
Array[0] = data;
size++;
printf("size FIRST:%d\n",size);
printArray();
return;
}
//////////////////////////////////////////////////ERROR HERRRRRRRRRR
//int r=0;
int SIZENEW = size+1;
printf("Siz NEW: %d\n", SIZENEW);
Array= (int*)realloc(Array,(SIZENEW)*sizeof(int));
if(Array==NULL){
printf("error");
exit(0);
//return;
}
*(Array+size)=data;
size++;
printf("BEFORE SORT\n");
printArray();
Selectsort();
printf("AFTER SORT\n");
printArray();
return;
}
char* readData(int file){
int determinant = 0;
char* piece = (char*)malloc(100*sizeof(char));
if(piece==NULL){
return NULL;
}
int go = 0;
while(go<100){piece[go] = '\0';
go++;
}
int Checkit = 1;
do{
Checkit = read(file,piece,99*sizeof(char));
if(Checkit==-1){
return NULL;
}
if(determinant == 0){
AllData=(char*)malloc(100*sizeof(char));
}
else{
char* ptr = realloc(AllData,100*sizeof(char));
if(ptr==NULL){
return NULL;
}
}
int startp = 0;
int t = determinant;
while(piece[startp]!='\0'){
AllData[t]=piece[startp];
t++;
startp++;
}
//Final Data
AllData[t]='\0';
determinant = determinant + Checkit;
printf("Check??%d\n",Checkit);
//WHY??
Checkit=read(file,piece,99*sizeof(char));
//Double check!!!
}while(Checkit!=0);
return AllData;
}
void printData(){
if(AllData==NULL){
return;
}
int e = 0;
while(AllData[e]!='\0'){
printf("%c",AllData[e]);
e++;
}
return;
}
void printArray(){
if(size==0){
printf("0");
return;
}
int e=0;
int SIZE = size;
while(e<SIZE){
if(Array[e]=='0'){
printf("0 ");
}
else{
printf("%d ",Array[e]);
}
e++;
}
return;
}
void Selectsort(){
int SIZE = size;
int i = 0;
int k = 0;
int place;
int data;
int temp;
for(i = 0; i<SIZE; i++){
for(k=i;k<SIZE;k++){
if(Array[i]>Array[k]){
//Switch
temp = Array[i];
Array[i]=Array[k];
Array[k]=temp;
}
}
}
return;
}