簡體   English   中英

返回值 3221225477?

[英]Return Value 3221225477?

我對編碼很陌生,所以請放輕松

這是我的代碼

/* Open File*/
FILE *openFile(char filename[], char filetype[]);
/* Read Number Of Nurses */
int readNurses(void);
/* Title */
void title(void);
/* Login Screen*/
int loginScreen();
/* Checks if memory allocation was correct */
void mallocCheck(char **malloc1, char **malloc2);
/* Asks for login details */
int askLogin(void);
/* Retrieve Login Details */
void getDetails(char **properID, char **properPASS, int nurses);
/* Test Details Input To Details */
int testDetails(void);
/* Encryption Of Password */
void encryptPass(void);
/* Main Menu */
void mainMenu(void);
/* Encrypt Patient's Details */
void encrtpyPatient(void);
/* Enter Patients Details */
void enterPatients(void);
/* Create Patients File */
void createPatient(void);
/* Clear Screen Smooth */
void clearScreen(void); 

int main(void)
{
    int nurses;
    int correct;
    char enterID[ID_LENGTH];
    nurses = readNurses();
    correct = loginScreen(enterID, nurses);
    title();
    return 0;
}

void title(void)
{
    printf("\n\n\n            =================================");
    Sleep(SLEEP_TIME);
    printf("\n            =                               =");
    Sleep(SLEEP_TIME);
    printf("\n            =  Welcome to Action On Weight  =");
    Sleep(SLEEP_TIME);
    printf("\n            =                               =");
    Sleep(SLEEP_TIME);
    printf("\n            =================================");
    Sleep(SLEEP_TIME);
    printf("\n                    *****************");
    printf("\n               ******               ******");
    printf("\n           ****                           ****");
    printf("\n        ****                                 ***");
    printf("\n      ***                                       ***");
    printf("\n     **           ***               ***           **");
    printf("\n   **           *******           *******           **");
    printf("\n   **           *******           *******           **");
    Sleep(SLEEP_TIME);
    printf("\n   **           *******           *******           **");
    printf("\n   **             ***               ***             **");
    printf("\n   **                                               **");
    printf("\n   **      *                                 *      **");
    printf("\n   **     **                                 **     **");
    printf("\n   **  ***                                     ***  **");
    printf("\n   **     *                                  *      **");
    printf("\n   **      ***                           ***        **");
    Sleep(SLEEP_TIME);
    printf("\n   ***      ****                       ****       ***");
    printf("\n     **         ******             ******         **");
    printf("\n      ***            ***************            ***");
    printf("\n        ****                                 ****");
    printf("\n           ****                           ****");
    Sleep(SLEEP_TIME); 
    printf("\n               ******               ******");
    printf("\n                    *****************");
    Sleep(SLEEP_TIME);
    Sleep(SLEEP_TIME);
}


int readNurses(void)
{
    FILE *fin;
    int nurses;
    fin = openFile("Nurses.txt", "r");
    while(!feof(fin))
    {
        fscanf(fin, "%*s");
        nurses++;
    }
    fclose(fin);
    printf("%d", nurses);
    return nurses;
}

FILE *openFile(char filename[], char filetype[])
{
    FILE *ptr = fopen(filename, filetype);
    if(!ptr)
    {
        printf("\n\n\n        Error 1. File couldn't be opened.");
              (" Please contact us at email@example.com");
              exit(1);
    }
    return ptr;
}

int loginScreen(char enterID[], int nurses)
{
    int correctDetails;
    int loop;
    char *properID;
    char *properPASS;
    properID = (char*) malloc(ID_LENGTH * nurses * 60);
    properPASS = (char*) malloc(PASS_LENGTH * nurses *60);
    correctDetails = 0;
    mallocCheck(&properID, &properPASS);
    getDetails(&properID, &properPASS, nurses);
    loop = nurses - 1;
    printf("das");
    while(nurses > 0)
    {
        printf("%s : %s", &properID[nurses], &properPASS[nurses]);
        nurses--;
    }
    /* do
    {
        printf("\n\n\n              ================================");
        printf("\n              =                              =");
        printf("\n              =         Login Screen         =");
        printf("\n              =                              =");
        printf("\n              ================================");
    }while(correctDetails = 0); */
    while(loop > 0)
    {
        free(properID - loop);
        free(properPASS - loop);        
        loop--;
    }
    return correctDetails;
}

void mallocCheck(char **malloc1, char **malloc2)
{
    if(malloc1 == NULL ||malloc2 == NULL)
    {
        printf("\n\n\n        Error 2. Assignment of ");
        printf(" memory wasn't succcesful.");
        printf(" Please contact us at email@example.com");
        Sleep(SLEEP_TIME);
        Sleep(SLEEP_TIME);
        exit(0);
    }
}

void getDetails(char **properID, char **properPASS, int nurses)
{
    FILE *ptr;
    ptr = openFile("Nurses.txt", "r");
    while(!feof(ptr))
    {
        fscanf(ptr, "%[^.]%*c%s\n", 
        &properID[nurses], &properPASS[nurses]);
        nurses--;
    }
    fclose(ptr);
}

問題出在函數 loginScreen() 中,函數 getDetails() 會運行,但是一旦它完成 3221225477,誰能指出我為什么會這樣? 看起來我正在正確分配內存和一切。 我試圖找出為什么會發生這種情況,但我找不到任何東西。

3221225477 是十六進制的 0xC0000005,即NTSTATUS代碼 STATUS_ACCESS_VIOLATION。 您的程序以某種方式損壞了其工作內存。 改為在 Linux 上編譯您的程序; 然后你可以在valgrind下運行它,並被告知問題出在哪里。

(valgrind 是一個非常有價值的調試工具,我建議所有 C 程序員保留一個 Linux 安裝,即使他們不需要它用於其他任何事情,只是為了他們可以使用 valgrind。它從未被移植到 Windows,對你來說很不幸。如果您現在不想花幾個小時重新配置您的計算機以實現永久雙啟動,您可以通過從Ubuntu liveCD啟動來臨時訪問 Linux。)

沒有理由期望您將 3221225477 識別為 Windows NT 低級錯誤代碼。 在您的 IDE 上提交錯誤報告; 它應該為你解碼這些。

如果您查看fscanf()函數簽名,它會說

scanf() 系列函數根據如下所述的格式掃描輸入。 此格式可能包含轉換說明符; 此類轉換的結果(如果有)通過指針參數存儲

並且您的getDetails()函數需要三個參數; 第一個是指向字符指針的指針,又名**char*char[] ,最后一個是int

void getDetails(char **properID, char **properPASS, int nurses)

在您的fscanf()調用中,您傳遞的是properIDproperPASS參數的地址,即使這些參數本身就是指針。 如果您按如下所示更新fscanf()調用,您的代碼應該可以正常工作。

fscanf(ptr, "%[^.]%*c%s\n", properID[nurses], properPASS[nurses]);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM