C - 读取文件并打印到文件后获取无效字符,可能是缓冲区溢出

C - Getting Invalid Characters After Reading a File and Printing to a File, -maybe- Buffer Overflow

我有一个包含名字、姓氏、ID 和电子邮件的文件,这些文件的顺序是随机的。我必须组织这些数据,写入结构和组织的输出文件。名字和姓氏可能不止一个。这是 disordered.txt:

的示例
abc@gmail.com Andee Kenny SMITH 1234
ADAM ADAM abc@gmail.com Andeee 21654
Anderea abc@gmail.com SAMMY 3524654
abc@gmail.com Andi BROWN 1245
Andie abc@gmail.com KNOWY 2485
Andra abc@gmail.com BRUCE 52445
Andrea abc@gmail.com 246574 DENNIS
2154 Andreana abc@gmail.com CHASE
Andree 21524 SIERRRA abc@gmail.com
Andrei 154 MONDY abc@gmail.com
4564765 Andria LE BARC abc@gmail.com
78 Andriana abc@gmail.com WALLS

我的代码适用于这 12 个人,但如果我复制粘贴很多或添加新人,在 33 个人之后,它会以重复的方式在名字和姓氏前面打印无效字符。

以下是截图:organized.txt

我更喜欢在我的结构中使用字符指针。

这是我的代码:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define TRUE 1
#define WORD_SIZE 30
#define NAME 1
#define SURNAME 2
#define EMAIL 3
#define ID 4

typedef struct ppl {
    char* name;
    char* surname;
    char* eMail;
    int id;
} PEOPLE;

int whichDataType (char* buffer);
void writeData (PEOPLE* person, char* buffer, int whichData, int* nameTimes, int* surnameTimes, int personNumber);  
void printData (PEOPLE* person, FILE* sptr, int personNumber);

int main (void) {
    FILE* fptr = NULL;

    fptr = fopen("disorganized.txt", "r");
    if (fptr == NULL) {
        printf ("Disorganized file couldn't open\n");
        printf ("Exiting the program\n");
        exit(TRUE);
    }

    FILE* sptr = NULL;

    sptr = fopen("organized.txt", "w");
    if (sptr == NULL) {
        printf ("Organized file couldn't open\n");
        printf ("Exiting the program\n");
        exit(TRUE);
    }

    int whichData;
    int personNumber = 0;
    int* nameTimes;
    int* surnameTimes;
    int forOnce = 0;
    char* buffer;
    int* buffer2;
    PEOPLE* person;

    person = (PEOPLE*) malloc (sizeof(PEOPLE));     
    buffer = (char*) malloc ( WORD_SIZE * sizeof(char));
    nameTimes = (int*) malloc ( sizeof(int));
    surnameTimes = (int*) malloc (sizeof(int));

    *nameTimes = 0;
    *surnameTimes = 0;

    //gets word 'till EOF
    while ((fscanf(fptr, "%s", buffer)) == 1) {
        if (personNumber != 0) {
            //creates new structure
            person = (PEOPLE*) realloc (person, personNumber * sizeof(PEOPLE));
        }
        //looks what type of data
        whichData = whichDataType(buffer);
        //allocates inside of structures and writes
        writeData(person, buffer, whichData, nameTimes, surnameTimes, personNumber);

        buffer2 = (int*) malloc (sizeof(int));
        *buffer2 = fgetc(fptr); //checks what's coming next

        if (*buffer2 == '\n') {
            if (forOnce == 0) {
                //to open a place for next person in my structure pointer, since personNumber = 0; increasing it with 1 and reallocating it with 1*sizeof(PEOPLE) would be the allocating memory for person 1 twice.
                personNumber = personNumber + 2;
                free(buffer2);
                free(buffer);
                buffer = (char*) malloc ( WORD_SIZE * sizeof(char));
                *nameTimes = 0;
                *surnameTimes = 0;
                ++forOnce;
            }
            else {
                ++personNumber;
                free(buffer2);
                free(buffer);
                buffer = (char*) malloc ( WORD_SIZE * sizeof(char));
                *nameTimes = 0;
                *surnameTimes = 0;
            }
        }
        else if (*buffer2 == ' ' || *buffer2 == '\t') {
            free(buffer2);
            free(buffer);
            buffer = (char*) malloc ( WORD_SIZE * sizeof(char));
        }
    }

    --personNumber; //my algorithm increases it 1 more time which is redundant

    printData (person, sptr, personNumber);
    int i;

    for (i = 0; i<personNumber; ++i) {
        free((person+i)->name);
        free((person+i)->surname);
        free((person+i)->eMail);
    }

    free(person);
    free(buffer);
    free(buffer2);
    free(nameTimes);
    free(surnameTimes);

    fclose(fptr);
    fclose(sptr);

    return 0;
}

int whichDataType (char* buffer) {
    if (buffer[0] >= 'A' && buffer[0] <= 'Z') {
        if (buffer[1] >= 'a' && buffer[1] <= 'z') {
            return NAME;
        }
        else if (buffer[1] >= 'A' && buffer[1] <= 'Z') {
            return SURNAME;
        }
    }
    else if (buffer[0] >= 'a' && buffer[0] <= 'z') {
        return EMAIL;
    }
    else if (buffer[0] >= '0' && buffer[0] <= '9') {
        return ID;
    }
} 

void writeData (PEOPLE* person, char* buffer, int whichData, int* nameTimes, int* surnameTimes, int personNumber) {
    if (personNumber != 0) {
        --personNumber;
    }

    switch (whichData) {
    case NAME:
        if (*nameTimes == 0) {
            (person + personNumber)->name = (char*) malloc ( WORD_SIZE * sizeof(char));
            ++(*nameTimes);
        }
        break;

    case SURNAME:
        if (*surnameTimes == 0) {
            (person+personNumber)->surname = (char*) malloc ( WORD_SIZE * sizeof(char));
            ++(*surnameTimes);
        }
        break;

    case EMAIL:
        (person + personNumber)->eMail = (char*) malloc ( WORD_SIZE * sizeof(char));
        break;
    }

    char space[2];
    strcpy(space, " ");

    switch (whichData) {
    case NAME:
        if (*nameTimes == 0) {
            strcpy( (person+personNumber)->name, buffer);
        }
        else {
            strcat ( (person+personNumber)->name, space);
            strcat( (person+personNumber)->name, buffer);
        }
        break;

    case SURNAME:
        if (*surnameTimes == 0) {
            strcpy ( (person+personNumber)->surname, buffer);
        }
        else {
            strcat( (person + personNumber)->surname, space);
            strcat( (person + personNumber)->surname, buffer);
        }
        break;

    case EMAIL:
        strcpy( (person + personNumber)->eMail, buffer);
        break;

    case ID:
        (person+personNumber)->id = atoi(buffer);
        break;
    }

}

void printData (PEOPLE* person, FILE* sptr, int personNumber) {
    fprintf(sptr, "-------------------------------------------------------------------------------------------------------------------------------------------------------------------");
    fprintf(sptr, "\n|%30s\t\t", "***NAME***");
    fprintf(sptr, "|%30s\t\t", "***SURNAME***");
    fprintf(sptr, "|%30s\t\t", "***E-MAIL***");
    fprintf(sptr, "|%30s\t|\n", "***ID NUMBER***");
    fprintf(sptr, "-------------------------------------------------------------------------------------------------------------------------------------------------------------------");

    int i;

    for (i = 0; i<personNumber; ++i) {
        fprintf(sptr, "\n|%d%30s\t\t", i, (person+i)->name);
        fprintf(sptr, "|%30s\t\t", (person+i)->surname);
        fprintf(sptr, "|%30s\t\t", (person+i)->eMail);
        fprintf(sptr, "|%30d\t|\n", (person+i)->id);
        fprintf(sptr, "-------------------------------------------------------------------------------------------------------------------------------------------------------------------");
    }
}

我尝试 malloc 每个新结构及其内部,在我完成对它们的工作后释放我的缓冲区,并为下一个再次分配它们。如果一个人有多个名字或姓氏,我将其名字或姓氏分配一次,并将 strcpy 我的缓冲区分配到适当的位置。如果我再次为同一个人转到我的 writeData 函数获取姓名或姓氏,我将传递分配的内存,因为我已经这样做了。然后我基本上将我的新缓冲区(名字或姓氏)连接到旧缓冲区旁边。

我的问题是,为什么我会收到这些无效字符,我在哪里犯了错误,我该如何预防?

我看到了问题并解决了。有一个算法问题。当程序去为名字分配内存时,当涉及到第二个名字时不再为人名字符串分配内存,不要丢失第一个,我增加了一次 nameTimes。然后在第二次切换时,因为它是名字,它应该输入 if (*nameTimes == 0) 部分到 strcpy。但是因为我在分配时已经增加了它,所以它永远不会进入那个部分并且它总是使用 strcat 来复制字符串。但是没有要连接的字符串,因此导致了问题。我将该部分的条件更改为 if (*nameTimes == 1)。然后出现第二个问题,它只是打印姓氏,因为我没有再次增加它,所以它卡在 strcpy 部分。所以我在 strcpy 之后再次增加了它。姓也一样。感谢@Barmar 和@user3629249,我还改进了代码。

|新代码|

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define WORD_SIZE 30
#define NAME 1
#define SURNAME 2
#define EMAIL 3
#define ID 4


typedef struct ppl {

    char* name;
    char* surname;
    char* eMail;
    int id;

} PEOPLE;

int whichDataType (char* buffer);
void writeData (PEOPLE* person, char* buffer, int whichData, int* nameTimes, int* surnameTimes, int personNumber);  
void printData (PEOPLE* person, FILE* sptr, int personNumber);

int main (void) {

    FILE* fptr = NULL;

    fptr = fopen("disorganized.txt", "r");
    if (fptr == NULL) {

        perror("Error: ");
        exit( EXIT_FAILURE );
    }

    FILE* sptr = NULL;

    sptr = fopen("organized.txt", "w");
    if (sptr == NULL) {

        perror("Error: ");
        exit( EXIT_FAILURE );
    }



    int whichData;
    int personNumber = 0;
    int nameTimes = 0;
    int surnameTimes = 0;
    int forOnce = 0;
    char* buffer = NULL;
    int* buffer2 = NULL;
    PEOPLE* person = NULL;
    PEOPLE* realloctemp = NULL;



    person = malloc (sizeof(PEOPLE));
    if ( person == NULL ) {

        perror("Error, malloc failed for person. ");
        exit ( EXIT_FAILURE );
    }

    buffer = malloc ( WORD_SIZE * sizeof(char));
    if ( buffer == NULL ) {

        perror("Error, malloc failed for buffer. ");
        exit ( EXIT_FAILURE );
    }


    while ((fscanf(fptr, "%29s", buffer)) == 1) {

        if (personNumber != 0) {

            realloctemp = realloc (person, personNumber * sizeof(PEOPLE));
            if ( realloctemp == NULL ) {

                perror("Error, reallocating. ");
                exit ( EXIT_FAILURE );
            }

            else {

                person = realloctemp;
            }

        }

        whichData = whichDataType(buffer);

        writeData(person, buffer, whichData, &nameTimes, &surnameTimes, personNumber);

        buffer2 = malloc (sizeof(int));
        if ( buffer2 == NULL ) {

            perror("Error, malloc failed for buffer2. ");
            exit ( EXIT_FAILURE );
        }

        else {

            *buffer2 = fgetc(fptr);
        }

        if (*buffer2 == '\n') {

            if (forOnce == 0) {

                personNumber = personNumber + 2;
                free(buffer2);
                free(buffer);

                buffer = malloc ( WORD_SIZE * sizeof(char));
                if ( buffer == NULL ) {

                    perror("Error*, malloc failed for buffer. ");
                    exit ( EXIT_FAILURE );
                }

                nameTimes = 0;
                surnameTimes = 0;

                ++forOnce;

            }

            else {

                ++personNumber;
                free(buffer2);
                free(buffer);

                buffer = malloc ( WORD_SIZE * sizeof(char));
                if ( buffer == NULL ) {

                    perror("Error**, malloc failed for buffer. ");
                    exit ( EXIT_FAILURE );
                }

                nameTimes = 0;
                surnameTimes = 0;

            }

        }

        else if (*buffer2 == ' ' || *buffer2 == '\t') {

            free(buffer2);
            free(buffer);
            buffer = malloc ( WORD_SIZE * sizeof(char));
            if ( buffer == NULL ) {

                perror("Error***, malloc failed for buffer. ");
                exit ( EXIT_FAILURE );
            }

        }
    }

    --personNumber;

    printData (person, sptr, personNumber);

    int i;

    for (i = 0; i<personNumber; ++i) {

        free((person+i)->name);
        free((person+i)->surname);
        free((person+i)->eMail);

    }

    free(person);
    free(buffer);
    free(buffer2);

    fclose(fptr);
    fclose(sptr);

    return EXIT_SUCCESS;
}

int whichDataType (char* buffer) {

    if (buffer[0] >= 'A' && buffer[0] <= 'Z') {

        if (buffer[1] >= 'a' && buffer[1] <= 'z') {

            return NAME;
        }

        else if (buffer[1] >= 'A' && buffer[1] <= 'Z') {

            return SURNAME;
        }
    }

    else if (buffer[0] >= 'a' && buffer[0] <= 'z') {

        return EMAIL;
    }

    else if (buffer[0] >= '0' && buffer[0] <= '9') {

        return ID;
    }

    else {

        perror("Invalid data type. ");
        exit( EXIT_FAILURE );

    }

} 

void writeData (PEOPLE* person, char* buffer, int whichData, int* nameTimes, int* surnameTimes, int personNumber) {

    if (personNumber != 0) {

        --personNumber;
    }

    switch (whichData) {

        case NAME:

            if (*nameTimes == 0) {

                (person + personNumber)->name = malloc ( WORD_SIZE * sizeof(char));
                if ( (person+personNumber)->name == NULL ) {

                    perror("Error. malloc failed for (person+personNumber)->name");
                    exit ( EXIT_FAILURE );
                }

                ++(*nameTimes);
            }

            break;

        case SURNAME:

            if (*surnameTimes == 0) {

                (person+personNumber)->surname = malloc ( WORD_SIZE * sizeof(char));
                if ( (person+personNumber)->surname == NULL ) {

                    perror("Error. malloc failed for (person+personNumber)->surname");
                    exit ( EXIT_FAILURE );
                }

                ++(*surnameTimes);
            }

            break;

        case EMAIL:

            (person + personNumber)->eMail = malloc ( WORD_SIZE * sizeof(char));
            if ( (person+personNumber)->eMail == NULL ) {

                perror("Error. malloc failed for (person+personNumber)->eMail");
                exit ( EXIT_FAILURE );
            }

            break;
    }


    char space[2];
    strcpy(space, " ");


    switch (whichData) {

        case NAME:

            if (*nameTimes == 1) {

                strcpy( (person+personNumber)->name, buffer);
                ++(*nameTimes);
            }

            else if (*nameTimes>1) {

                strcat ( (person+personNumber)->name, space);
                strcat( (person+personNumber)->name, buffer);
            }

            else {

                perror("Error, invalid nameTimes value. ");
                exit ( EXIT_FAILURE );
            }

            break;

        case SURNAME:

            if (*surnameTimes == 1) {

                strcpy ( (person+personNumber)->surname, buffer);
                ++(*surnameTimes);
            }

            else if (*surnameTimes>1) {


                strcat( (person + personNumber)->surname, space);
                strcat( (person + personNumber)->surname, buffer);

            }

            else {

                perror("Error, invalid surnameTimes value. ");
                exit ( EXIT_FAILURE );
            }

            break;

        case EMAIL:

            strcpy( (person + personNumber)->eMail, buffer);

            break;

        case ID:

            (person+personNumber)->id = atoi(buffer);

            break;

    }


}

void printData (PEOPLE* person, FILE* sptr, int personNumber) {

    fprintf(sptr, "-------------------------------------------------------------------------------------------------------------------------------------------------------------------");
    fprintf(sptr, "\n|%30s\t\t", "***NAME***");
    fprintf(sptr, "|%30s\t\t", "***SURNAME***");
    fprintf(sptr, "|%30s\t\t", "***E-MAIL***");
    fprintf(sptr, "|%30s\t|\n", "***ID NUMBER***");
    fprintf(sptr, "-------------------------------------------------------------------------------------------------------------------------------------------------------------------");

    int i;

    for (i = 0; i<personNumber; ++i) {

        fprintf(sptr, "\n|%d%30s\t\t", i+1, (person+i)->name);
        fprintf(sptr, "|%30s\t\t", (person+i)->surname);
        fprintf(sptr, "|%30s\t\t", (person+i)->eMail);
        fprintf(sptr, "|%30d\t|\n", (person+i)->id);
        fprintf(sptr, "-------------------------------------------------------------------------------------------------------------------------------------------------------------------");
    }

}