Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <math.h>
- /*FILE*;
- fopen(const char *, ...);
- fclose(FILE*);
- fscanf(FILE*, const char *, ...);
- fprintf(FILE*, const char *, ...);
- feof(FILE*);*/
- double myabs(double n);
- int number (const char *, int *,double);
- double myabs(double n)
- {
- return n>0?n:-n;
- }
- int number (const char *name, int *res,double x)
- {
- FILE *fp;
- double y,max;
- int n=1,num=1;
- if (!(fp=fopen(name,"r"))) return -1;
- fscanf(fp,"%lf",&x);
- max=x;
- while (fscanf(fp,"%lf",&y)==1)
- {
- if (y>x)
- {
- max=y;
- num=n++;
- }
- n++;
- x=y;
- }
- if (!feof(fp))
- {
- fclose(fp);
- return -2;
- }
- fclose(fp);
- *res=max;
- return num;
- }
- int main(void)
- {
- const char *fname="a.txt";
- double x;
- int ret,answer;
- ret=number(fname,&answer,x);
- 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;
- }else{
- printf("Number=%d, position=%d\n",ret,answer);
- return 0;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement