我的文件第一行如下:
AXX00.data
但是当我调用下面的函数时,它会在ll
中存储XX00.datafgets(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("%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);// 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("%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);
do
{
//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
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=%d\n",KeyCounter[max],totalCount,KeyCounter[max]*100/totalCount );
//if(KeyCounter[max]*100/totalCount>20)
return max;
//else
// return -1;
}
答案 0 :(得分:0)
至少fgets()
使用是非常错误的。
char ll[10];
...
do {
// .....v...... Wrong size
fgets(ll, 11, keysFile);
printf("ll=%s fileName=%s\n", ll, fileName);
// 8 and > here look questionable too.
if (strncmp(ll, fileName, 8) > 0)
break;
} while (fgets != '\0');
// ....^...... 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;
}
答案 1 :(得分:0)