c - 在C中解密文件的功能?

标签 c arrays function pointers structure

我有一个包含大量“前景”的文件,因此每一行都有一个加密的姓氏、一个加密的名字、一个 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/

相关文章:

c - 在 Matlab 中安装 C 编译器

java - 如何在java中的方法内部使用类中的变量

javascript - 如何使用动态生成的文件名数组在 Grunt 中运行任务?

Python从类列表中选择项目

javascript - 连续运行一个javascript函数

c - 从函数发送指针

c - 获取数组中的最小值

c - 全局客户表 C : Chatroom

Java - 使用递归的深度克隆数组;未知类型和未知深度

node.js - 调用错误优先功能Node JS