Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <ctype.h>
- #include <locale.h>
- #include <math.h>
- void print_file(FILE *);
- int main()
- {
- setlocale(0, "");
- FILE *f;
- int size = sizeof(double), offset = 8*size, end_file, flag = 0, flag2 = 0, count = 0;
- double first, last, x, temp;
- if((f = fopen("int.dat", "rb+")) == NULL){
- perror("Ошибка открытия файла");
- return 1;
- }
- printf("Исходный файл:\n");
- print_file(f);
- printf("\n");
- rewind(f);
- while(fread(&x, sizeof(double), 1, f)){
- if (x < 0){
- if(!flag2){
- first = x;
- }
- last = x;
- count++;
- }
- }
- printf("\nПервое отрицательное: %.3lf\n", first);
- printf("Последнее отрицательное: %.3lf\n", last);
- rewind(f);
- while(fread(&x, sizeof(double), 1, f )){
- if(x == first){
- fseek(f, -sizeof(double), SEEK_CUR);
- fwrite(&last, sizeof(double), 1, f);
- }
- else if (x == last){
- fseek(f, -sizeof(double), SEEK_CUR);
- fwrite(&first, sizeof(double), 1, f);
- }
- }
- printf("\nИзмененный файл:\n");
- fseek(f, sizeof(double), SEEK_END);
- fwrite(&count, sizeof(double), 1, f); //записываем кол-во отриц. компонет в конец файла
- print_file(f);
- printf("\n");
- fclose(f);
- return 0;
- }
- void print_file(FILE *f){ //печать содержимого файла
- double x, sz = sizeof(double);
- rewind(f);
- while(fread(&x, sz, 1, f)) printf("%.3lf ", x);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement