回 帖 发 新 帖 刷新版面

主题:密码存储

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <malloc.h>
#include <conio.h>




struct manage_data
{
long int manage_num;
char pin[6];
};




int main()
{
FILE* fp_manage; 
char str[6] = {'\0'};
char ch;
int num;
struct manage_data* head;
head = (struct manage_data*)malloc(sizeof(struct manage_data));
if( head == NULL )
printf( "Insufficient memory available\n" );
else
{
printf( "Memory space allocated for path name\n" );
}
head->manage_num = 0;
fp_manage = fopen("f:/data.txt","w");
if(fp_manage == NULL)
{
printf("Open file error!");
}
printf("输入账号:");
scanf("%ld",&(head->manage_num));
getchar();
    printf("输入密码:");
num = 0;
while( (ch=getch())!='\r' &&  num<6 )
{
if(ch =='\b'&&num>0)
{
num--;
putchar('\b');
putchar(' ');
putchar('\b');
}
else if(ch!='\b')
{
putchar('*');
str[num] =ch;
num++;
}
}

strcpy(head->pin,str);
    fwrite(head,sizeof(struct manage_data),1,fp_manage);


free(head);
printf( "Memory freed\n" );


fclose(fp_manage);

return 0;
}

回复列表 (共2个回复)

沙发

密码输入6位时会出错--------free(head)出错 为什么

板凳

char pin[6]; 只能存5位,最后一位用于存字符串结尾的zero

我来回复

您尚未登录,请登录后再回复。点此登录或注册