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
|
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_CHAR 256
int comp(const void *elem1, const void *elem2){
return (*(int *)(elem1) - *(int *)(elem2));
}
int main(){
FILE *file = NULL;
char ifilename[MAX_CHAR];
char ofilename[MAX_CHAR];
int *list;
int count;
printf("Input file name:\n>");
scanf("%s", ifilename);
file = fopen(ifilename, "r");
if(!file){
printf("ERROR: file %s can not be opened!\n", ifilename);
return 1;
}
for(count = 0; !feof(file); ++count){
int dummy;
fscanf(file, "%i", &dummy);
}
list = new int[--count];
fseek(file, 0, SEEK_SET);
for(int i = 0; i < count; ++i){
fscanf(file, "%i", list + i);
printf("%i\n", *(list + i));
}
fclose(file);
qsort(list, count, sizeof(int), comp);
strncpy(ofilename, ifilename, strlen(ifilename) - 4);
ofilename[strlen(ifilename) - 4] = '\0';
strcat(ofilename, "_out.txt");
file = fopen(ofilename, "w");
if(!file){
printf("ERROR: file %s can not be opened!\n", ofilename);
return 1;
}
for(int i = 0; i < count; ++i)
fprintf(file, "%i\n", *(list + i));
fclose(file);
return 0;
}
|