我有一个包含大量“潜在客户”的文件,因此每行都有一个加密的姓氏,一个加密的名字,一个12位数的id代码,然后是4个评级(3个整数,1个浮点数)。加密正在将名称的每个字符移动到文件中最后一个数字的值(发现为310)。
尝试创建一个解密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 分段错误
答案 0 :(得分:1)
再次阅读警告,它们非常清楚:
la2.c:68:2: warning: format ‘%s’ expects argument of type ‘char *’, but argument 2 has type ‘int’
它告诉你第68行printf
调用的第二个参数应该是一个字符串(char *
),但你传递一个整数(实际上是一个char
,但是编译器将其转换为int
)作为该参数。
稍后当你运行程序时printf
使用该整数作为指向字符串的指针,并且由于该整数不是一个正确的整数,程序崩溃。
答案 1 :(得分: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()
之前添加预先声明。