c - fgets function returning wrong value -


my file has first line below:

axx00.data 

but when call below function, stores xx00.data in ll

fgets(ll,10,keysfile); 

why weird thing happening? kindly check code below. whole code confuse that's why didn't keep before, remove confusion editing question code.

.... putting complete code on request.....

    #include <stdio.h>     #include <string.h>     #include <stdlib.h>     #include <unistd.h>      int n=192;     int l=8;     int s[192];     int i,j;     int k[8];     int x,y;     int keycounter[192];         int fmsattack(int,int,int,int,int, int);     int fms(char[], int);     int max(int[]);     /*void ksa(int *, int);     ksa(int *s,int n)     {         i,j;         for(i=0;i<b+3;i++)         {          }     }*/     int main()     {         int i,j,b=0,nextkey;         for(b=0; b<5;b++)//(l-3);b++)         {             /*int scurr[n];             (int = 0; < n; i++)             {                 scurr[i]=s[i];              }*/             for(i = 0;i<= 4; i++)             {                 (j = 0; j <= 9; j++)                 {                     if(i==0 && j==9)                         continue;                      char filename[10]="axx";                     char append[10];                      sprintf(append, "%d", i);                     strcat(filename,append);                       sprintf(append, "%d", j);                     strcat(filename,append);                      strcat(filename,".data");                      //printf("%s\n", filename);                     nextkey=fms(filename,b);                 }             }             k[b+3]=nextkey;              //ksa(scurr[],n);         }     }      int fms(char filename[], int b)     {         int f;          file *outfile;         file *infile;         file *keysfile;         char outfilename[]="out";         char line[20];;         char firstline[20];         char *firstlineargs;         char ll[10];         int tuples;         int count=0;         int nextkey;         int max;          strcat(outfilename,filename);          infile=fopen(filename,"r");          //printf("%fopen\n",fopen("keysfile.data","r") );         if(access( "keysfile.data",f_ok ) != -1 )         {             printf("a\n");             keysfile=fopen("keysfile.data","a");         }         else         {             printf("w\n");             keysfile=fopen("keysfile.data","w");         }             fprintf(keysfile, "%s ",filename);          fgets(firstline,20,infile);         //printf("firstline=%s\n",firstline);          l=atoi(strtok(firstline," "));         tuples=atoi(strtok(null," "));          //printf("%d",l);         //printf("%d",tuples);          fgets(firstline,20,infile);// skip unnecessary line         for(f=0;f<n;f++)             keycounter[f]=0;             (f=0;f<tuples;f++)                                                       {              int k0,k1,k2,r;             char *lineargs;                                                                                                                                                                                                                                                                                                          fgets(line,20,infile);             //printf("%s\n",line);             //printf("%s\n",strtok(line," "));             k0=atoi(strtok(line," "));             k1=atoi(strtok(null," "));             k2=atoi(strtok(null," "));             r=atoi(strtok(null," "));              if(fmsattack(k0,k1,k2,r,l,b)==1)                 {                     count++;                     nextkey=r-j-s[b+3];                     if(nextkey<0)                         nextkey=nextkey+n;                     else if(nextkey>(n-1))                         nextkey=nextkey-n;                     keycounter[nextkey]++;                     //fprintf(outfile, "%d %d %d %d k[b+3]=%d\n", k0,k1,k2,r,nextkey);      //              printf("%d nextkey=%d\n",count,nextkey);                  }          }         max=max(keycounter);                 {                 //printf("ll=%s\n",ll );             fgets(ll,11,keysfile);             printf("ll=%s filename=%s\n",ll,filename );             if(strncmp(ll,filename,8)>0)                 break;         }         while(fgets != '\0');         printf("pratik\n");         fprintf(keysfile, " %d\n",max);         printf("pratik\n");         fclose(infile);         fclose(outfile);          fclose(keysfile);         printf("pratik\n");         return max;      }     int fmsattack(int k0,int k1,int k2,int r,int l, int b)         {             //ksa                  // assigning initial values                 k[0]=k0;                 k[1]=k1;                 k[2]=k2;                  //initialization                 (i = 0; < n; i++)                 {                     s[i]=i;                 }                  j=0;                  //scrambling                 (i=0; i<b+3; i++)                 {                     int temp;                      j=(j+s[i]+k[i % l]) % n;                      //swap s[i] & s[j]                     temp=s[i];                     s[i]=s[j];                     s[j]=temp;                  }                 x=s[1];                 y=s[x];                 if(x+y==b+3)                     return 1;                 else                     return 0;     /*             //prga                  //initilization                 i=0;                 j=0;                  //generation loop                  for(i=0,j=0;i<3;i++)                 {                     i=i+1;                     j=j+s[i];                     x=s[i];                     y=s[x];                  if(x+y==b+3)                     return 1;                 }                 return 0;*/         }     int max(int keycounter[])     {         int i,max=0,totalcount=0;         for(i=1;i<n;i++)         {             totalcount=totalcount+keycounter[i];             if(keycounter[i]>keycounter[max])                 max=i;         }         printf("max=%d total=%d per=%d\n",keycounter[max],totalcount,keycounter[max]*100/totalcount );         //if(keycounter[max]*100/totalcount>20)             return max;         //else         //  return -1;     } 

at least fgets() usage wrong.

char ll[10]; ... {   //   .....v......  wrong size   fgets(ll, 11, keysfile);   printf("ll=%s filename=%s\n", ll, filename);   // 8 , > here questionable too.           if (strncmp(ll, filename, 8) > 0)     break; } while (fgets != '\0'); //   ....^...... not call fgets 

suggest following

char ll[12]; ... while (fgets(ll, sizeof ll, keysfile) != null) {   printf("ll='%s' filename='%s'\n", ll, filename);   if (strncmp(ll, filename, 8) > 0)     break; } 

Comments

Popular posts from this blog

PHPMotion implementation - URL based videos (Hosted on separate location) -

c# - Unity IoC Lifetime per HttpRequest for UserStore -

I am trying to solve the error message 'incompatible ranks 0 and 1 in assignment' in a fortran 95 program. -