gpt4 book ai didi

链表中的 C 指针

转载 作者:太空宇宙 更新时间:2023-11-04 03:59:58 25 4
gpt4 key购买 nike

<分区>

做家庭作业时我遇到了问题,我相信,指针。

作业包括以下内容:

我有一个 txt 文件,其中每一行都是一个名称和一个密码。

thisismyname:thisismypassword

我必须读取这些数据,将其处理成结构链表,运行所有列表并将密码发送给暴力算法。该算法在找到通行证后,应该将通行证写在结构上。最后,我应该运行列表并将数据写入 txt 文件

我的问题是当我找到密码时。它没有将其值存储在结构中。最后我可以读取数据,我可以看到蛮力正在工作,但最后,我只能设法写下名称并传递给文件。未加密的通行证被写为 NULL,所以我认为这是一个指针问题。

这是代码(删除了所有我认为不相关的东西):

typedef struct p {
char *name;
char *pass;
char *pass_desenc;
struct p *next_person;
} person;

typedef struct n {
int a;
int b;
} numbers;

int readFile(person **people) {
FILE * fp;
char line[100];

if ((fp = fopen(STUDENTS_FILE, "r")) != NULL) {
while (fgets(line, sizeof (line), fp) != NULL) {
person *p;
char email[27] = "";
char password[14] = "";
char *change = strchr(line, '\n');
if (change != NULL)
*change = '\0';

/* Gets email*/
strncpy(email, line, 26);
email[27] = '\0';

/* Gets pass*/
strncpy(password, line + 27, 14);
password[14] = '\0';

p = (person*) malloc(sizeof (person));
if (p == NULL) {
return -1;
}

p->name = (char*) malloc(strlen(email));
if (p->name == NULL) {
return -1;
}
sprintf(p->name, "%s", email);
p->name[strlen(email)] = '\0';

p->pass = (char*) malloc(strlen(password));
if (p->pass == NULL) {
return -1;
}
sprintf(p->pass, "%s", password);
p->pass[strlen(password)] = '\0';

p->next_person = (*people);
(*people) = p;

countPeople++;
}
fclose(fp);

return 0;
}
return -1;
}

void fmaps(int id, numbers pass_range, person *people) {
/*This function will run all my list and try to uncrypt pass by pass.
On the brute-force pass in unencrypted and when it return to this function, I can print the data.
*/
while (people != NULL && j > 0) {
for (i = 1; i <= PASS_SIZE && notFound == 1; i++) {
notFound = bruteForce(i, people, &total_pass);
}
notFound = 1;
count = count + total_pass;

printf("#####Email: %s Pass: %s PassDesenq: %s \n", people->name, people->pass, people->pass_desenc);

people = people->next_person;
j--;
}
}

void fcontrol(int n, person *people) {
/*This function should write the data to a file
I can see that all data is written as expected but people->pass_desenc is writing/printing NULL
*/

if ((fp = fopen(STUDENTS_LOG_FILE, "a+")) != NULL) {
while (people != NULL) {
printf("#####1111Email: %s Pass: %s PassDesenq: %s \n", people->name, people->pass, people->pass_desenc);
fprintf(fp, "%d%d%d%d%d%d:grupo%d:%s:%s\n", tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday, tm.tm_hour, tm.tm_min, tm.tm_sec, 1, people->name, people->pass_desenc);
people = people->next_person;
}
}
fclose(fp);
}

int main() {

/*Struct*/
person *people = NULL;

if (readFile(&people)) {
printf("Error reading file!\n");
return 0;
}
/*Function to send data to brute-force*/
fmaps(i, pass_range, people);
/*After all data is processed, this function writes the data to a file*/
fcontrol(NR_PROC, people);

destroyList(&people);

return 0;
}

int bruteForce(int size, person *people, int *total_pass) {
int i;
char *pass_enc;
int *entry = (int*) malloc(sizeof (size));
char pass[50];
char temp;
pass[0] = '\0';


for (i = 0; i < size; i++) {
entry[i] = 0;
}
do {
for (i = 0; i < size; i++) {
temp = (char) (letters[entry[i]]);
append(pass, temp);
}

(*total_pass)++;

/*Compare pass with test*/
pass_enc = crypt(pass, salt);


if (strcmp(pass_enc, people->pass) == 0) {

people->pass_desenc = (char*) malloc(strlen(pass));
if (people->pass_desenc == NULL) {
return -1;
}

sprintf(people->pass_desenc, "%s", pass);
people->pass_desenc[strlen(pass)] = '\0';
return 0;
}

pass[0] = '\0';
for (i = 0; i < size && ++entry[i] == nbletters; i++) {
entry[i] = 0;
}

} while (i < size);


free(entry);


return 1;
}

void append(char *s, char c) {
int len = strlen(s);
s[len] = c;
s[len + 1] = '\0';
}

void destroyList(person **people) {
person *aux;
printf("\nList is being destroyed.");
while (*people != NULL) {
aux = *people;
*people = (*people)->next_person;
free(aux);
printf(".");
}
printf("\nList destroyed.\n");
}

我相信在 fmaps 中所做的更改是本地的,不会传递给 main

感谢任何帮助...

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com