【发布时间】:2019-12-16 19:43:07
【问题描述】:
我对编码很陌生,所以请放轻松
这是我的代码
/* 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,谁能指出为什么会发生这种情况?看起来我正在正确分配内存和一切。我试图找出为什么会发生这种情况,但我找不到任何东西。
【问题讨论】:
-
也许用 0 初始化护士。另外,你要数什么?
-
这很奇怪,因为该函数中的拼写错误
while(correctDetails = 0)。 -
您的指针使用在某些地方不正确。
getDetails(&properID, &properPASS, nurses);您需要传递包含已分配内存地址的指针。相反,您将指针传递给指针。也就是应该是getDetails(properID, properPASS, nurses); -
Oh mybad:你评论了那部分,但我也注意到
int loginScreen()是一个缺少完整声明的原型。请发布显示问题的Minimal Reproducible Example。这还应该显示您正在使用的#include文件,以及诸如ID_LENGTH之类的缺失值。我还建议您使用tour 并阅读How do I ask a good question?
标签: c memory memory-management c99