Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- int z(const char*, double*);
- int z(const char *name, double *n)
- {
- FILE *fp;
- double j=0,x;
- if (!(fp = fopen(name,"r")))
- return -1;
- while (fscanf(fp,"%lf",&x)==1)
- {
- j++;
- if(n==x)
- break;
- }
- if (!feof(fp))
- {
- fclose(fp);
- return -2;
- }
- fclose(fp);
- return j;
- }
- int main(void)
- {
- const char *fname = "1.txt";
- double d; int ret;
- ret = z(fname,&d);
- if (ret<0)
- {
- switch (ret)
- {
- case -1:
- printf("Cannot open %s\n",fname);
- break;
- case -2:
- printf("Cannot read %s\n",fname);
- break;
- default:
- printf("Error %d in file %s",ret,fname);
- }
- return 1;
- }
- printf("answer = %f\n",j);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement