语法错误解决完了,就是运行就错误,找不出来了。
这个程序是为了放在电话本前面做一个多用户的电话本登陆,可以创建每个人自己的电话本,可能有个BUG就是,只要用户名和密码正确,就可以进任何一个电话本,虽然考虑了,但是水平太菜,不会做了。各位给看看啊,写的太乱,不好意思啊,新手!
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<malloc.h>
/*步骤:1.密码验证2.读取/创建文件3.电话本链表4.保存到文件
*思路:1.输出菜单:1.创建新电话本2.打开已有电话本
* 2.选择创建:要求输入电话本名字(文件名),用define带参宏定义,创建一个电话本文件,
* 然后要求输入设置登陆用户名和密码。然后登入.
* 选择打开已有电话本:直接要求输入用户名和密码,然后通过strcmp对比验证,正确通过,
* 错误返回重新输入,错误三次退出。
*操作方法:*/
//#define changjian(x) “x.txt”
//#define liebiao1(x) “x.txt”
typedef struct tel
{
char name[10];
char num[12];
struct tel *next;
}TEL;
typedef struct pwd
{
char name[10];
char mima[10];
struct pwd *next;
}MIMA;
//开始菜单
void denglu()
{
FILE *fp1,*fp2,*fp3,*fp4;
MIMA *p1,*p2,*head=NULL;
char a,telname[10],b,str[30]=”e:\\telbook\\”,str1[30]=”e:\\telbook\\”;
char liebiao[100][10],liebiao1[10];
int i;
if((fp3=fopen(“e:\\telbook\\liebiao.txt”,”r+”))==NULL)
{
printf(“文件打开错误,请与管理员联系!\n”);
exit(0);
}
while(feof(fp3)==1)
{
for(i=0;i<100;i++)
{
fread(liebiao[i],10,1,fp3);
}
}
if((fp1=fopen(“e:\\telbook\\mima.txt”,”r+”))==NULL)
{
printf(“文件打开错误,请与管理员联系!\n”);
exit(0);
}
while(feof(fp1)==1)
{
p2=(MIMA *)malloc(sizeof(MIMA));
fread(p2->name,10,1,fp1);
fread(p2->mima,10,1,fp1);
if(head==NULL)
{
head=p2;
}
else
{
p1->next=p2;
}
p1=p2;
}
p1->next=NULL;
printf(“欢迎使用电话本程序\1.创建新的电话本\n2.打开已有的电话本\n”);
scanf(“%c”,&a);
switch(a)
{
case ’1′:
a:printf(“请输入电话本的名字:”);
scanf(“%s”,telname);
strcat(str,telname);
strcat(str,”.txt”);
if((fp2=fopen(str,”a”))==NULL)
{
printf(“创建失败!再见!\n”);
exit(0);
}
p1=(MIMA *)malloc(sizeof(MIMA));
printf(“请设置登陆名:”);
scanf(“%s”,p1->name);
printf(“请设置登陆密码:”);
scanf(“%s”,p1->mima);
fwrite(telname,10,1,fp3);
fwrite(p1->name,10,1,fp1);
fwrite(p1->mima,10,1,fp1);
fclose(fp1);
case ’2′:b:printf(“电话本列表:\n”);
for(i=0;i<100;i++)
{
printf(“%s”,liebiao[i]);
}
p1=(MIMA *)malloc(sizeof(MIMA));
printf(“请输入电话本名字:”);
scanf(“%s”,liebiao1);
for(i=0;i<100;i++)
{
if(strcmp(liebiao[i],liebiao1)!=0)
{
printf(“没有这个电话本,是否创建?(y/n)”);
fflush(stdin);
scanf(“%c”,&b);
switch(b)
{
case ‘y’:goto a;
case ‘n’:goto b;
}
}
else
{
printf(“请输入登陆名:”);
scanf(“%s”,p1->name);
printf(“请输入登陆密码:”);
scanf(“%s”,p1->mima);
while(strcmp(p1->name,p2->name)!=0)
{
p2->next=p2;
}
if(strcmp(p1->mima,p2->mima)==0)
{
strcat(str1,telname);
strcat(str1,”.txt”);
if((fp4=fopen(str1,”r+”))==NULL)
{
printf(“打开错误!\n”);
}
}
}
}
}
}
void main()
{
denglu();
}
>> 本文固定链接: http://www.vcgood.com/archives/1639
演示了用户名,密码和文件的对应关系.其他的自己补全.
文件名判断的时候注意大小写,windows下是不分大小写的.
[code]
#include <stdlib.h>
#include <stdio.h>
#define MAXCHAR 512
typedef struct tagBOOKLIST {
char username[ 64 ];
char password[ 64 ];
char filename[ 256 ];
struct tagBOOKLIST *pNextBook;
} BOOK, *PTRBOOK;
void ShowMenu()
{
system( "cls" );
printf( "Tell Books By XStar.WXB\n" );
printf( "Please Choice Operation:\n" );
printf( "1: Create A Tell Book;\n" );
printf( "2: Open A Tell Book;\n" );
}
PTRBOOK ReadBookInfo( const char *filename )
{
FILE *stream;
char buff[ 512 ];
PTRBOOK pBookList;
PTRBOOK pItem;
int cs;
stream = fopen( filename, "r" );
if( stream == NULL ) {
return NULL;
}
pBookList = NULL;
while( fgets( buff, 512, stream ) != NULL ) {
pItem = (PTRBOOK)malloc( sizeof( BOOK ) );
if( pItem == NULL )
{
break;
}
pItem->pNextBook = NULL;
cs = sscanf( buff, "%64s %64s %256s", pItem->username, pItem->password, pItem->filename );
if( cs != 3 ) {
free( pItem );
continue;
}
pItem->pNextBook = pBookList;
pBookList = pItem;
}
fclose( stream );
return pBookList;
}
void FreeBookInfo( PTRBOOK pBookList )
{
PTRBOOK pList;
while( pBookList != NULL ) {
pList = pBookList->pNextBook;
free( pBookList );
pBookList = pList;
}
}
int ShowList( PTRBOOK pBookList )
{
PTRBOOK pList;
int nCnt;
nCnt = 0;
pList = pBookList;
while( pList != NULL ) {
printf( "%10s %10s [%10s]\n", pList->username, pList->password, pList->filename );
pList = pList->pNextBook;
nCnt++;
}
return nCnt;
}
int main()
{
PTRBOOK pBookList;
pBookList = NULL;
ShowMenu();
pBookList = ReadBookInfo( "E:\\TelBook\\booklist.txt" );
ShowList( pBookList );
FreeBookInfo( pBookList );
return 0;
}
[/code]
booklist.txt
[code]
root root E:\telbook\phone.txt
guest guest E:\telbook\phoneguest.txt
guest1 guest1 E:\telbook\phoneguest.txt
guest2 guest2 E:\telbook\phoneguest.txt
[/code]