Advertisement
Guest User

Untitled

a guest
Nov 17th, 2017
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.24 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <math.h>
  3. #define EPS 0.000001
  4. #include <stdlib.h>
  5. #include <string.h>
  6. int search_number(FILE *f, double x);
  7.  
  8. int search_number(FILE *f,double x)
  9. {
  10.     double a;
  11.     while (fscanf(f, "%lf", &a) == 1) {
  12.         if (fabs(a -x) < EPS) {
  13.  
  14.             return 1;
  15.         }
  16.     }
  17.  
  18.     return 0;
  19. }
  20.  
  21. int main(void)
  22. {
  23.     int found ;
  24.     char *res = (char*)calloc(100,sizeof(char));
  25.  
  26.     FILE *f;
  27.     double x;
  28.     printf("Please write down filename of input file to read:\n");
  29.     fgets (res, 100,stdin);
  30.     res[strlen(res)-1]=0;
  31.     if (strlen(res)==0) {
  32.         printf("You haven't entered file name. The programme will be terminated!\n");
  33.         free(res);
  34.         return 1;
  35.     }
  36.     printf("Your file:[%s]\n",res);
  37.     f = fopen(res, "r");
  38.     if (f == NULL) {
  39.         perror("Error occurred while opening file");
  40.         free(res);
  41.         return 1;
  42.     }
  43.     printf("Please enter number to find in file:\n");
  44.     if(scanf("%lf",&x)==1)
  45.     {
  46.         found=search_number(f, x);
  47.  
  48.     }
  49.     else
  50.     {
  51.         printf("ERROR\n");
  52.     }
  53.  
  54.     fclose(f);
  55.  
  56.     if (found == 1) {
  57.         printf("Number is found!\n");
  58.     } else {
  59.         printf("Number isn't found!\n");
  60.     }
  61.     return 0;
  62. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement