链表中的C指针

链表中的C指针,c,pointers,C,Pointers,做家庭作业时,我觉得指针有问题 任务包括以下内容: 我有一个txt文件,其中每一行作为名称和密码 thisismyname:thisismypassword 我必须读取这些数据,将其处理成struct链表,运行所有列表,并将密码发送给暴力算法。此算法在找到过程后,应在结构上写入过程。最后,我应该运行列表并将数据写入txt文件 我的问题是当我找到密码时。它没有将其值存储在结构中。最后,我可以读取数据,我可以看到蛮力正在发挥作用,但最后,我只能写下名称并传递到文件。未加密的过程被写为NULL,因此

做家庭作业时,我觉得指针有问题

任务包括以下内容:

我有一个txt文件,其中每一行作为名称和密码

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

我的问题是当我找到密码时。它没有将其值存储在结构中。最后,我可以读取数据,我可以看到蛮力正在发挥作用,但最后,我只能写下名称并传递到文件。未加密的过程被写为NULL,因此我认为是指针问题

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

typedef结构p{
字符*名称;
字符*通行证;
char*pass_desenc;
结构p*下一个人;
}人;
类型定义结构{
INTA;
int b;
}数量;
int readFile(个人**个人){
文件*fp;
字符行[100];
如果((fp=fopen(学生档案,“r”)!=NULL){
while(fgets(line,sizeof(line),fp)!=NULL){
个人*p;
字符电子邮件[27]=“”;
字符密码[14]=“”;
char*change=strchr(第“\n”行);
如果(更改!=NULL)
*更改='\0';
/*收到电子邮件*/
strncpy(电子邮件,第26行);
电子邮件[27]='\0';
/*通过*/
strncpy(密码,第27行,第14行);
密码[14]='\0';
p=(人*)马洛克(人);
if(p==NULL){
返回-1;
}
p->name=(char*)malloc(strlen(email));
如果(p->name==NULL){
返回-1;
}
sprintf(p->name,“%s”,电子邮件);
p->name[strlen(email)]='\0';
p->pass=(char*)malloc(strlen(password));
如果(p->pass==NULL){
返回-1;
}
sprintf(p->pass,“%s”,密码);
p->pass[strlen(密码)]='\0';
p->next_person=(*人);
(*人)=p;
countPeople++;
}
fclose(fp);
返回0;
}
返回-1;
}
无效fmaps(整数id、数字通过范围、人*人){
/*此函数将运行所有我的列表,并尝试逐个取消加密。
在未加密的蛮力传递中,当它返回此函数时,我可以打印数据。
*/
while(people!=NULL&&j>0){
for(i=1;i name,people->pass,people->pass\u desenc);
人=人->下一个人;
j--;
}
}
无效fcontrol(整数n,人*人){
/*此函数应将数据写入文件
我可以看到所有数据都按预期写入,但people->pass\u desenc写入/打印为空
*/
如果((fp=fopen(学生日志文件,“a+”)!=NULL){
while(人!=NULL){
printf(“######1111邮件:%s通行证:%s通行证SENQ:%s\n”,人员->姓名,人员->通行证,人员->通行证);
fprintf(fp,“%d%d%d%d%d%d:grupo%d:%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);
人=人->下一个人;
}
}
fclose(fp);
}
int main(){
/*结构*/
person*people=NULL;
如果(读取文件(&P)){
printf(“读取文件时出错!\n”);
返回0;
}
/*函数将数据发送到暴力*/
FMAP(i、通过范围、人员);
/*处理完所有数据后,此函数将数据写入文件*/
F控制(程序、人员);
名单(和人员);
返回0;
}
整数暴力(整数大小,人*人,整数*总通行证){
int i;
char*pass_enc;
int*entry=(int*)malloc(sizeof(size));
字符通过[50];
焦炭温度;
通过[0]='\0';
对于(i=0;i通过)=0){
人员->通行证=(char*)malloc(strlen(pass));
如果(人员->通过\u设计==NULL){
返回-1;
}
sprintf(人员->通过设计,“%s”,通过);
人员->通行证[strlen(通行证)]='\0';
返回0;
}
通过[0]='\0';
对于(i=0;i下一个人;
免费(aux);
printf(“.”);
}
printf(“\n列表已销毁。\n”);
}
我相信在
fmap
中所做的更改是本地的,不会传递到
main

非常感谢您的帮助……

这是您编写文件读取器/解析器的方法。它避免str[n]cpy(),并使用memcpy()+偏移量+大小执行所有字符串操作。(显然,这两种情况下都需要正确)

#包括
#包括
#包括
#包括
类型定义结构p{
字符*名称;
字符*通行证;
//char*pass_desenc;
结构p*next;
}人;
#定义学生文件“STUDENTS.dat”
无符号countPeople=0;
int readFile(个人**个人){
文件*fp;
字符行[100];
尺寸透镜,位置;
fp=fopen(学生档案,“r”);
如果(!fp){
fprintf(标准,“无法打开%s:%s\n”
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");
}
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>

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

#define STUDENTS_FILE "students.dat"
unsigned countPeople = 0;

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

    fp = fopen(STUDENTS_FILE, "r");
    if (!fp)  {
        fprintf(stderr, "Could not open %s:%s\n"
                , STUDENTS_FILE, strerror(errno));
        return -1;
        }

    while ( fgets(line, sizeof line, fp) ) {
        person *p;

        len = strlen(line);

        /* remove trailng '\n', adjusting the length */
        while (len && line[len-1] == '\n') line[--len] = 0;

        /* Ignore empty lines */
        if ( !len ) continue;

        /* Library function to count the number of characters in the first argument
        ** *not* present in the second argument.
        ** This is more or less equivalent to strtok(), but
        ** 1) it doen not modify the string,
        ** 2) it returns a size_t instead of a pointer.
        */
        pos = strcspn(line, ":" );

        /* Ignore lines that don't have a colon */
        if (line[pos] != ':') continue;

        p = malloc(sizeof *p);
        if ( !p ) { fclose(fp); return -2; }
        p->next = NULL;

        p->name = malloc(1+pos);
        if ( !p->name ) { fclose(fp); return -3; } /* this could leak p ... */
        memcpy(p->name, line, pos-1);
        p->name[pos] = 0;

        p->pass = malloc(len-pos);
        if ( !p->pass ) {fclose(fp); return -4; } /* this could leak p and p->name */
        memcpy(p->pass, line+pos+1, len-pos);

        /* Instead of pushing (which would reverse the order of the LL)
        ** , we append at the tail of the LL, keeping the original order.
        */
        *people = p;
        people = &p->next ;

        countPeople++;
    }

    fclose(fp);
    return 0;
}