我有一个包含大量“前景”的文件,因此每一行都有一个加密的姓氏、一个加密的名字、一个 12 位数字 ID 代码,然后是 4 个评级(3 个整数,1 个 float )。加密是将名称的每个字符移动文件中最后一个数字的值(发现是 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
编译链接成功 您的二进制文件可以通过键入以下内容来运行: 拉2 engs20-1:~/engs20/workspace$ la2 段错误
最佳答案
再次阅读警告,它们很清楚:
la2.c:68:2: warning: format ‘%s’ expects argument of type ‘char *’, but argument 2 has type ‘int’
它告诉您第 68 行 printf
调用的第二个参数应该是一个字符串 (char *
) 但您传递了一个整数(实际上是单个char
,但编译器将其转换为 int
) 作为该参数。
稍后当您运行程序时,printf
使用该整数作为指向字符串的指针,并且由于该整数不是正确的整数,程序崩溃了。
关于c - 在C中解密文件的功能?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13005745/