Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- int
- main(void)
- {
- FILE *f1, *f2;
- int tmp, i, n, max, buf, j, fr, fw;
- if ((f1 = fopen("input.txt", "rt")) == NULL) {
- return 0;
- }
- if ((f2 = fopen("output.bin", "wb+")) == NULL) {
- return 0;
- }
- if (fscanf(f1, "%d", &n) != 1) {
- return 0;
- }
- fseek(f2, 0L, SEEK_SET);
- for (i = 0; i < n; i++) {
- if (fscanf(f1, "%d", &tmp)!= 1) {
- return 0;
- }
- if (fwrite(&tmp, sizeof(int), 1, f2)!= 1) {
- return 0;
- }
- }
- for (i = n; i > 1; i--) {
- fseek(f2, 0L, SEEK_SET);
- for(j = 1; j < i; j++) {
- fr = fread(&max, sizeof(int), 1, f2);
- fr = fread(&buf, sizeof(int), 1, f2);
- if (max > buf) {
- fseek(f2, -2*sizeof(int), SEEK_CUR);
- if (fwrite(&buf, sizeof(int), 1, f2)!= 1) {
- return 0;
- }
- if (fwrite(&max, sizeof(int), 1, f2)!= 1) {
- return 0;
- }
- }
- fseek(f2, -sizeof(int), SEEK_CUR);
- }
- }
- fclose(f1);
- fclose(f2);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement