#include <stdio.h>
#include <string.h>
#include <conio.h>
#include <Windows.h>
#include <locale.h>
#define MAX_PASSWORD_LENGTH 50 //长度:50
#define MAX_USERNAME_LENGTH 50
#define TRUE 0
#define FALSE -1
/*
Defination of ROLES
*/
#define SUPER_USER 0
#define ADMIN 1
#define COMMON_USER 2
#define GUEST 3
#define SYSTEM_NAME "蒙蒙一号"
#define COMMON_CONSOLE_WIDTH 1200
#define COMMON_CONSOLE_HEIGHT 800
/*用户结构体*/
struct USER
{
unsigned short id;
wchar_t username[MAX_USERNAME_LENGTH];
wchar_t password[MAX_PASSWORD_LENGTH];
int role:4;
int isActive:1;
};
/*用户列表*/
struct USERLIST //2014-3-18 18:23 END
{
unsigned short id;
wchar_t username[MAX_USERNAME_LENGTH];
wchar_t password[MAX_PASSWORD_LENGTH];
int role:4;
int isActive:1;
struct USERLIST *next;
};
int GetTotalUserCount() //Error Code: 1025
{
FILE *userCount;
int count;
userCount=fopen("C:\\testSource\\user.count","rb+");
if (!userCount)
{
printf("GetTotalUserCount failed! Error Code: 1025\n");
return FALSE;
}
fread(&count, sizeof(int), 1, userCount);
fclose(userCount);
return count;
}
/*
Write total user count to user.count
*/
int WriteTotalUserCount(int num) //Error Code: 1024
{
FILE *userCount;
int count=GetTotalUserCount();
userCount=fopen("C:\\testSource\\user.count","rb+");
if (!userCount)
{
printf("WriteTotalUserCount failed! Error Code: 1024\n");
return FALSE;
}
count=count+num;
fwrite(&count, sizeof(int), 1, userCount);
fclose(userCount);
return TRUE;
}
int CreateUser(unsigned short id,wchar_t *username,wchar_t *password,int role,int isActive)
{
FILE *ptr_User;
struct USER user;
ptr_User=fopen("C:\\testSource\\user","ab");
if (!ptr_User)
{
return FALSE;
}
user.id=id;
wcscpy(user.username,username);
wcscpy(user.password,password);
user.role=role;
user.isActive=isActive;
fwrite(&user, sizeof(struct USER), 1, ptr_User);
fclose(ptr_User);
WriteTotalUserCount(1);
return TRUE;
}
int PrintUser(struct USERLIST *Head)
{
printf("\nID\t 用户名\t\t 密码\t已激活\t角色\n");
printf("--------------------------------------------------------------------------------\n");
while(Head!=NULL)
{
printf("%d\t%ls\t%ls\t %d\t %d\n",Head->id,Head->username,Head->password,Head->isActive,Head->role);
Head=Head->next;
}
printf("--------------------------------------------------------------------------------\n");
return TRUE;
}
struct USERLIST *GetUsers()
{
struct USERLIST *Head,*New,*Tail;
int userCount;
int round;
FILE *ptr_User;
struct USER user;
userCount=GetTotalUserCount();
Head=New=Tail=(struct USERLIST *)malloc(sizeof(struct USERLIST));
ptr_User=fopen("C:\\testSource\\user","rb");
if (!ptr_User)
{
return NULL;
}
fread(&user, sizeof(struct USER), 1, ptr_User);
New->id=user.id;
wcscpy(New->username,user.username);
wcscpy(New->password,user.password);
New->isActive=user.isActive;
New->role=user.role;
for(round=1;round<userCount;round++)
{
New=(struct USERLIST *)malloc(sizeof(struct USERLIST));
fseek(ptr_User,sizeof(struct USER)*round,SEEK_SET);
fread(&user, sizeof(struct USER), 1, ptr_User);
New->id=user.id;
wcscpy(New->username,user.username);
wcscpy(New->password,user.password);
New->isActive=user.isActive;
New->role=user.role;
Tail->next=New;
Tail=New;
}
Tail->next=NULL;
fclose(ptr_User);
return Head;
}
void FreeUserList(struct USERLIST *Head)
{
struct USERLIST *temp;
while (Head!=NULL)
{
temp=Head;
Head=Head->next;
free(temp);
}
}
/*
Create the necessary files
*/
int InitialNecessaryFiles() //Error Code: 1026
{
FILE *user;
FILE *userCount;
int intialCount=0;
user=fopen("C:\\testSource\\user","wb");
if (!user)
{
printf("Create file user failed! Error Code: 1026\n");
return FALSE;
}
fclose(user);
userCount=fopen("C:\\testSource\\user.count","wb");
if (!userCount)
{
printf("Create file userCount failed! Error Code: 1026\n");
return FALSE;
}
fwrite(&intialCount, sizeof(int), 1, userCount);
fclose(userCount);
printf("Initial necessary files success!\n");
printf("C:\\testSource\\user\n");
printf("C:\\testSource\\user.count\n\n");
return TRUE;
}
void InitialConsoleFrame()
{
HWND h;
h=GetConsoleWindow();
if(!MoveWindow(h,0,0,COMMON_CONSOLE_WIDTH,COMMON_CONSOLE_HEIGHT,FALSE))
{
printf("初始化窗口大小以及位置错误!");
}
if(!SetConsoleTitle(SYSTEM_NAME))
{
printf("初始化窗口标题错误!");
}
}
int main(int argv,char* argc[])
{
int count;
struct USERLIST *head;
unsigned short id[6]={1,2,3,4,5,6};
wchar_t *username[6]={L"蒙蒙一号",L"蒙蒙二号",L"蒙蒙三号",L"蒙蒙四号",L"蒙蒙五号",L"蒙蒙六号"};
wchar_t *password[6]={L"123456",L"654321",L"asdqwe",L"qweasd",L"147258",L"852741"};
int role[6]={SUPER_USER,SUPER_USER,ADMIN,COMMON_USER,GUEST,GUEST};
int isActive[6]={TRUE,FALSE,TRUE,TRUE,TRUE,FALSE};
setlocale(LC_ALL,"chinese");
InitialConsoleFrame();
InitialNecessaryFiles();
for(count=0;count<6;count++)
{
if(CreateUser(id[count],username[count],password[count],role[count],isActive[count])==TRUE)
{
printf("Create user success!\n");
}
else
{
printf("Create user failed!\n");
}
}
head=GetUsers();
PrintUser(head);
FreeUserList(head);
getch();
}
转载于:https://my.oschina.net/u/1538037/blog/213697