2012-10-22 61 views
1

我有一個包含大量'潛在客戶'的文件,所以每行都有一個加密的姓氏,一個加密的名字,一個12位數的id代碼,然後是4個評級(3個整數,1個浮點數)。加密是通過文件中最後一個數字的值(發現爲310)來移動名稱的每個字符。C函數解密文件?

試圖創建一個函數來解密1個字符,然後使用此函數解密字符串(名稱)的另一個函數,但是出現錯誤和分段錯誤,請幫忙!

#include <stdio.h> 
#include <stdlib.h> 

#define MSTRLEN 20 
#define MAX_SIZE 1000 

/* structure prototype */ 
typedef struct { 
    char lastname[MSTRLEN]; 
    char firstname[MSTRLEN]; 
    char secretcode[13]; 
    int rank1; 
    int rank2; 
    float rank3; 
    int rank4; 
} prospect_t; 

int main (void) 
{ 

    FILE *ifile; 
    prospect_t *prospects; 
    char last[MSTRLEN],first[MSTRLEN],code[13],last_name,first_name; 
    int r1,r2,r4,num_prospects,shift,i,j; 
    float r3; 

    char unencrypt_letter(char *letter, int shift); 
    char unencrypt_name(char name[MSTRLEN], int shift); 


    /*finding how many prospects and last integer*/ 
    ifile = fopen("prospects.txt","r"); 
    num_prospects = 0; 

    if (ifile == NULL){ 
     printf("File not found!\n"); 
     return (-1); 
    } 
    while (fscanf(ifile,"%s %s %s %d %d %f %d",last,first,code,&r1,&r2,&r3,&r4)!=EOF){ 
     num_prospects++; 
    } 
    shift = r4%26; 
    fclose(ifile); 
    /*--------------------------------------*/ 

    /* dynamic memory allocation */ 
    prospects = (prospect_t*)malloc(num_prospects*sizeof(prospect_t)); 

    ifile = fopen("prospects.txt","r"); 

    if (ifile == NULL){ 
     printf("File not found!\n"); 
     return (-1); 
    } 

    for(i=0;i<num_prospects;i++){ 
     fscanf(ifile,"%s %s %s %d %d %f %d", prospects[i].lastname,prospects[i].firstname,prospects[i].secretcode,&prospects[i].rank1,&prospects[i].rank2,&prospects[i].rank3,&prospects[i].rank4); 
    } 
    /* to be used once get working 
    for(j=0;j<num_prospects;j++){ 
     prospects[j].lastname = unencrypt_name(prospects[j].lastname,shift); 
     prospects[j].firstname = unencrypt_name(prospects[j].firstname,shift); 
    } 
    */ 
    /* to be taken out once working */ 
    last_name = unencrypt_name(prospects[0].lastname,shift); 
    first_name = unencrypt_name(prospects[0].firstname,shift); 

    printf("%s %s\n",last_name,first_name); 



    fclose(ifile);  

    free(prospects); 
    return(0); 
} 

/* function to unencrypt one letter */ 
char unencrypt_letter(char *letter, int shift) 
{ 
char *new_letter; 

if ((*letter - shift) < 'a') 
    *new_letter = (char)((*letter - shift) + 26); 
else 
    *new_letter = (char)(*letter - shift); 

return(*new_letter); 
} 
/* function to unencrypt a name */ 
char unencrypt_name(char name[MSTRLEN],int shift) 
{ 
char new_name[MSTRLEN]; 
int k; 

k = 0; 

while (name[k] != '\0'){ 
    new_name[k] = unencrypt_letter(name[k],shift); 
    k++; 
} 
return(*new_name); 
} 

從終端,我得到以下幾點:

 
la2.c: In function ‘main’: 
la2.c:68:2: warning: format ‘%s’ expects argument of type ‘char *’, but argument 2 has type ‘int’ [-Wformat] 
la2.c:68:2: warning: format ‘%s’ expects argument of type ‘char *’, but argument 3 has type ‘int’ [-Wformat] 
la2.c: In function ‘unencrypt_name’: 
la2.c:99:3: warning: passing argument 1 of ‘unencrypt_letter’ makes pointer from integer without a cast [enabled by default] 
la2.c:79:6: note: expected ‘char *’ but argument is of type ‘char’ 

**鏈接階段 的gcc -o LA2 la2.o

編譯和鏈接成功 完成您的二進制文件可以運行通過鍵入: la2 engs20-1:〜/ engs20/workspace $ la2 分段錯誤

回答

1

再次閱讀警告,他們是有相當清楚的:

 
la2.c:68:2: warning: format ‘%s’ expects argument of type ‘char *’, but argument 2 has type ‘int’ 

它告訴你的printf呼叫線路68的第二個參數預期爲一個字符串(char *),但你傳遞一個整數(實際上是單一的char,但編譯器將其轉換爲int)作爲該參數。

稍後當您運行程序時printf將該整數用作指向字符串的指針,並且由於該整數不是正確的整數,所以程序崩潰。

0

假設第二fscanf是線68

fscanf(ifile,"%s %s %s %d %d %f %d", prospects[i].lastname,prospects[i].firstname,prospects[i].secretcode,&prospects[i].rank1,&prospects[i].rank2,&prospects[i].rank3,&prospects[i].rank4); 

我會堅持在一些(),以確保你得到的,你認爲是什麼地址。

&(prospects[i].lastname), ... 

你不預先申報char unencrypt_letter(char *letter, int shift)你打電話之前,所以編譯器假定它返回一個int。

main()之前添加它的預先聲明。