Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <math.h>
- #define EPS 0.000001
- #include <stdlib.h>
- #include <string.h>
- int search_number(FILE *f, double x);
- int search_number(FILE *f,double x)
- {
- double a;
- while (fscanf(f, "%lf", &a) == 1) {
- if (fabs(a -x) < EPS) {
- return 1;
- }
- }
- return 0;
- }
- int main(void)
- {
- int found ;
- char *res = (char*)calloc(100,sizeof(char));
- FILE *f;
- double x;
- printf("Please write down filename of input file to read:\n");
- fgets (res, 100,stdin);
- res[strlen(res)-1]=0;
- if (strlen(res)==0) {
- printf("You haven't entered file name. The programme will be terminated!\n");
- free(res);
- return 1;
- }
- printf("Your file:[%s]\n",res);
- f = fopen(res, "r");
- if (f == NULL) {
- perror("Error occurred while opening file");
- free(res);
- return 1;
- }
- printf("Please enter number to find in file:\n");
- if(scanf("%lf",&x)==1)
- {
- found=search_number(f, x);
- }
- else
- {
- printf("ERROR\n");
- }
- fclose(f);
- if (found == 1) {
- printf("Number is found!\n");
- } else {
- printf("Number isn't found!\n");
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement