最近写一个简单地物流信息管理系统,类似于学生信息管理系统但是出现了文件读写出现乱码的问题,和其他人出现的乱码问题不同。困扰了好几天了,还是没找到代码的问题之处,希望各位能帮忙看看,非常感谢!!!
代码不长,而且有的功能还没有做出来,但是文件读写,链表搭建这种最主要的做出来了的。
不说那么多了,直接上代码:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
struct transport
{
char ID[20];
char name[20];
char phoneNum[20];
char myAddress[20];
char yourAddress[20];
};
struct sample
{
struct transport data;
struct sample*next;
};
//声明一下函数
struct sample*createList();
struct sample*createNode(struct transport data);
void insertNode(struct sample*headNode,struct transport data);
void deleteNode1(struct sample*headNode,char*name);
void deleteNode2(struct sample*headNode,char*ID);
void printList(struct sample*headNode);
void ReadInfFromFile(struct sample*headNode);
void WriteInfToFile(struct sample*headNode);
//界面
void menu()
{
printf("-------------------------【物流信息管理系统】-------------------------\n");
printf("\t\t0.退出系统\n");
printf("\t\t1.录入信息\n");
printf("\t\t2.浏览信息\n");
printf("\t\t3.修改信息\n");
printf("\t\t4.删除信息\n");
printf("\t\t5.查找信息\n");
}
void keyDown(struct sample*list)//选择功能
{
int choice;
struct transport data;
scanf("%d",&choice);
switch(choice)
{
case 0:
printf("正常退出!\n");
system("pause");
exit(0);
break;
case 1:
printf("--------------------------【录入信息】--------------------------\n");
printf("请输入物流信息:姓名,单号,电话,发件地址,收件地址:");
scanf("%s%s%s%s%s",data.name,data.ID,data.phoneNum,data.myAddress,data.yourAddress);
printf("成功啦");
insertNode(list,data);
fflush(stdin);//清空缓冲区
break;
case 2:
printf("--------------------------【浏览信息】--------------------------\n");
printList(list);
break;
case 3:
printf("--------------------------【修改信息】--------------------------\n");
break;
case 4:
printf("--------------------------【删除信息】--------------------------\n");
break;
case 5:
printf("--------------------------【查找信息】--------------------------\n");
printf("您想要用单号查询还是用姓名查询?\n");
printf("1.通过姓名查询\t\t2.通过单号查询");
break;
default:
printf("选择错误,重新输入\n");
system("pause");
break;
}
WriteInfToFile(list);
}
//功能
struct sample*createList()//建表
{
struct sample*headNode=(struct sample*)malloc(sizeof(struct sample));
headNode->next=NULL;
return headNode;
}
struct sample*createNode(struct transport data)//建节点
{
struct sample*newNode=(struct sample*)malloc(sizeof(struct sample));
newNode->data=data;
newNode->next=NULL;
return newNode;
}
void insertNode(struct sample*headNode,struct transport data)//插入节点
{
struct sample*newNode=createNode(data);
newNode->next=headNode;
headNode=newNode;
}
void deleteNode1(struct sample*headNode,char*name)//通过姓名,删除节点
{
struct sample*posNode=headNode->next;
struct sample*posFrontNode=headNode;
if(posNode==NULL)
{
printf("数据为空,无法删除!\t");
return;
}
while(strcmp(posNode->data.name,name))
{
posFrontNode=posNode;
posNode=posNode->next;
if(posNode=NULL)
{
printf("未找到该节点,无法删除!");
return;
}
}
posFrontNode->next=posNode->next;
free(posNode);
}
void deleteNode2(struct sample*headNode,char*ID)//通过单号,删除节点
{
struct sample*posNode=headNode->next;
struct sample*posFrontNode=headNode;
if(posNode==NULL)
{
printf("数据为空,无法删除!");
return;
}
while(strcmp(posNode->data.ID,ID))
{
posFrontNode=posNode;
posNode=posNode->next;
if(posNode==NULL)
{
printf("数据为空,无法删除!");
return;
}
}
posFrontNode->next=posNode->next;
free(posNode);
}
struct sample*searchNode1(struct sample*headNode,char *ID)//通过单号查询
{
struct sample*pMove=headNode->next;
if(pMove==NULL)
{
return NULL;
}
while(strcmp(pMove->data.ID,ID))
{
pMove=pMove->next;
}
return pMove;
}
struct sample*searchNode2(struct sample*headNode,char *name)//通过姓名查询
{
struct sample*pMove=headNode->next;
if(pMove==NULL)
{
return NULL;
}
while(strcmp(pMove->data.name,name))
{
pMove=pMove->next;
}
return pMove;
}
void printList(struct sample*headNode)//浏览信息(打印链表)
{
struct sample*pMove=headNode;
printf("寄件人姓名\t快递单号\t电话号码\t发件地址\t收件地址\n");
while(pMove)
{
printf("%s\t%s\t%s\t%s\t%s\t\n",pMove->data.name,pMove->data.ID,pMove->data.phoneNum,pMove->data.myAddress,pMove->data.yourAddress);
pMove=pMove->next;
}
printf("\n");
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//读文件 //
void ReadInfFromFile(struct sample*headNode)
{
FILE *fp;
struct transport data;
if((fp=fopen("xiaozuwuliuxitong.txt","r")) ==NULL)
{printf("没找到,建立了文件");
fopen("xiaozuwuliuxitong.txt","w+");
}
while(fscanf(fp,"%s %s %s %s %s",data.ID,data.name,data.phoneNum,data.myAddress,data.yourAddress)!=EOF)
{
insertNode(headNode,data);
}
fclose(fp);
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//写文件 //
void WriteInfToFile(struct sample*headNode)
{
FILE *fp;
fp=fopen("xiaozuwuliuxitong.txt","w");
struct sample*pMove = headNode;//创建用来移动的指针
while(pMove!=NULL)//只要指针不为空(还有信息),就继续打印
{
fprintf(fp,"%s %s %s %s %s",pMove->data.ID,pMove->data.name,pMove->data.phoneNum,pMove->data.myAddress,pMove->data.yourAddress);
pMove = pMove->next;//指针后移
}
fclose(fp);
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//主函数
int main()
{
struct sample*list=createList();
ReadInfFromFile(list); //读文件
while(1)
{
menu();
keyDown(list);
system("pause");
system("cls");//清屏
}
system("pause");
return 0;
}