C - 返回错误值的 fgets 函数



我的文件的第一行如下:

AXX00.data

但是当我调用下面的函数时,它将 XX00.data 存储在 ll

fgets(ll,10,keysFile);

为什么会发生这种奇怪的事情?请检查下面的代码。但实际上整个代码可能会让你感到困惑,这就是为什么我以前没有保留它,但为了消除一些混乱,我正在用代码编辑我的问题。

....根据请求放置完整的代码.....

    #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)
    {
        it 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];
            for (int i = 0; i < N; i++)
            {
                SCurr[i]=S[i];
            }*/
            for(i = 0;i<= 4; i++)
            {
                for (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("%sn", 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("%fopenn",fopen("keysFile.data","r") );
        if(access( "keysFile.data",F_OK ) != -1 )
        {
            printf("an");
            keysFile=fopen("keysFile.data","a");
        }
        else
        {
            printf("wn");
            keysFile=fopen("keysFile.data","w");
        }   
        fprintf(keysFile, "%s ",fileName);
        fgets(firstLine,20,infile);
        //printf("firstLine=%sn",firstLine);
        L=atoi(strtok(firstLine," "));
        tuples=atoi(strtok(NULL," "));
        //printf("%d",L);
        //printf("%d",tuples);
        fgets(firstLine,20,infile);// To skip unnecessary line
        for(f=0;f<N;f++)
            KeyCounter[f]=0;    
        for (f=0;f<tuples;f++)                                              
        { 
            int k0,k1,k2,R;
            char *lineArgs;                                                                                                                                                                                                                                                                                             
            fgets(line,20,infile);
            //printf("%sn",line);
            //printf("%sn",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]=%dn", k0,k1,k2,R,NextKey);
    //              printf("%d NextKey=%dn",count,NextKey);
                }
        }
        max=Max(KeyCounter);
        do
        {
                //printf("ll=%sn",ll );
            fgets(ll,11,keysFile);
            printf("ll=%s fileName=%sn",ll,fileName );
            if(strncmp(ll,fileName,8)>0)
                break;
        }
        while(fgets != '');
        printf("PRATIKn");
        fprintf(keysFile, " %dn",max);
        printf("PRATIKn");
        fclose(infile);
        fclose(outfile); 
        fclose(keysFile);
        printf("PRATIKn");
        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
                for (i = 0; i < N; i++)
                {
                    S[i]=i;
                }
                j=0;
                //Scrambling
                for (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=%dn",KeyCounter[max],totalCount,KeyCounter[max]*100/totalCount );
        //if(KeyCounter[max]*100/totalCount>20)
            return max;
        //else
        //  return -1;
    }

至少fgets()用法是非常错误的。

char ll[10];
...
do {
  //   .....v......  Wrong size
  fgets(ll, 11, keysFile);
  printf("ll=%s fileName=%sn", ll, fileName);
  // 8 and > here look questionable too.        
  if (strncmp(ll, fileName, 8) > 0)
    break;
} while (fgets != '');
//   ....^...... this is not a call to fgets

建议如下

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;
}

我想错了。Append(a) 不支持 Read(r) 模式。所以不得不用读取+追加(r+)模式进行更改。非常感谢您抽出宝贵的时间。

相关内容

  • 没有找到相关文章

最新更新