运行5保存到文件中时,报错,请各位大佬帮忙看看;

ping527963401 2019-06-02 10:05:05
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#pragma warning(disable:4996)

//management 管理、人员管理系统;

#if 0
1、初始化数据库,此时的数据库是文件;(initData2File)
2、读数据库,生成内存数据模型;(createListFromFile)
3、增、查、改、删、排序;
4、更新数据库; saveList2File(保存链表文件) destroyListStu(删除链表)
#endif

typedef struct student
{
char name[30]; //姓名
char sex; //性别
int age; //年龄
int score; //成绩
}Stu;

typedef struct stuNode
{
Stu data;
struct stuNode *next;
}StuNode;

void initData2File();
StuNode *createListFromFile(char *filePash); //创建链表,把数据传进来
void traverseStulist(StuNode *head);

void addListStu(StuNode *phead); //增
StuNode *searchListStu(StuNode *head); //查
void deleteListStu(StuNode *head); //删除
void sortListStu(StuNode *head); //排序(按成绩排序)
int lenList(StuNode *head); //求链表长度
void saveList2File(StuNode *head, char *filePath);
void destroyListStu(StuNode *head);

int main()
{
initData2File();

StuNode *head = createListFromFile("student.data");

traverseStulist(head);
while (1)
{
system("cls");
traverseStulist(head);

printf("1->add\t 2->search 3->delete 4->sort 5->exit\n");
printf("请输入序号:");
int dix;
scanf("%d", &dix);
StuNode *Phead;
switch (dix)
{
case 1:
addListStu(head);
break;
case 2:
if (Phead = searchListStu(head))
{
printf("您要查找的人的信息在本数据库中:\n");
printf("name\t\t\tsex\t\tage\t\tscore\n");
printf("%-10s\t\t%c\t\t%d\t\t%d\n",
Phead->data.name, Phead->data.sex, Phead->data.age, Phead->data.score);
printf("------------------------------\n\n");
}
else
printf("查无此人!\n");
break;
case 3:
deleteListStu(head);
break;
case 4:
sortListStu(head);
break;
case 5:
saveList2File(head,"student.data");
destroyListStu(head);
return 0;
default:
printf("您输入错误!\n");
}
}

return 0;
}

void initData2File() //1、初始化数据库,此时的数据库是文件;
{
Stu s[4] =
{
{"sunwukong",'x',500,90},
{"wuneng",'x',800,59 },
{"wujing",'m',700,80},
{"tangseng",'m',24,100}
};

FILE *pf = fopen("student.data", "w+");
if (NULL == pf)
exit(-1);
fwrite((char *)s, sizeof(s), 1, pf);
fclose(pf); //关闭文件
}

StuNode *createListFromFile(char *filePash) //2、读数据库,生成内存数据模型;(createListFromFile)把文件传进来
{
FILE *pf = fopen(filePash, "r+");
if (pf == NULL)
exit(-1);
StuNode *head = (StuNode *)malloc(sizeof(StuNode)); // 创建头结点
if (NULL == head)
exit(-1);
head->next = NULL;
StuNode *cur = (StuNode *)malloc(sizeof(StuNode));
while (fread((void *)&cur->data, sizeof(Stu), 1, pf))
{
cur->next = head->next;
head->next = cur;

cur = (StuNode *)malloc(sizeof(StuNode));
}
free(pf);

return head;
}

void traverseStulist(StuNode * head)
{
head = head->next;
printf("name\t\t\tsex\t\tage\t\tscore\n");
while (head)
{
printf("%-10s\t\t%c\t\t%d\t\t%d\n", head->data.name, head->data.sex, head->data.age, head->data.score);
head = head->next;
}
}

void addListStu(StuNode * head) //增加数据
{
StuNode *cur = (StuNode*)malloc(sizeof(StuNode));
if (NULL == cur)
exit(-1);
cur->next = NULL;

system("cls");

printf("name :");
scanf("%s", cur->data.name);
getchar();

printf("sex :");
scanf("%c", &cur->data.sex);

printf("age :");
scanf("%d", &cur->data.age);

printf("score :");
scanf("%d", &cur->data.score);

cur->next = head->next;
head->next = cur;

cur = (StuNode*)malloc(sizeof(StuNode));

getchar();

}

StuNode *searchListStu(StuNode * head) //查
{
printf("请输入您要查找的人名:");
char Name[20];
scanf("%s", Name);
head = head->next;
while (head)
{
if (strcmp(Name, head->data.name) == 0)
break;
else
head = head->next;
}
return head;
}

void deleteListStu(StuNode * head)
{
StuNode *cur;
StuNode *Dhead = searchListStu(head);
int Index;
if (Dhead)
{
printf("您要查找的人的信息在本数据库中:\n");
printf("name\t\t\tsex\t\tage\t\tscore\n");
printf("%-10s\t\t%c\t\t%d\t\t%d\n",
Dhead->data.name, Dhead->data.sex, Dhead->data.age, Dhead->data.score);
printf("------------------------------\n\n");
printf("继续删除请按 1 ,取消请按 2 :");
scanf("%d", &Index);

while (1)
{
if (Index == 1)
{
//head = head->next;
while (Dhead != head->next)
{
head = head->next;
}
cur = Dhead;
head->next = Dhead->next;
free(Dhead);
return;
}
else if (Index == 2)
break;
else
{
printf("您输入有误!请重新输入!\n");
printf("继续删除请按 1 ,取消请按 2 :");
scanf("%d", &Index);
continue;
}
}
}

else
printf("查无此人!\n");
}

int lenList(StuNode * head)
{
int len = 0;
head = head->next;
while (head)
{
len++;
head = head->next;
}

return len;
}

void sortListStu(StuNode * head)
{
StuNode *cur;
StuNode *p;
StuNode *q;
//StuNode *t;

int len = lenList(head);
for (int i = 0; i < len - 1; i++)
{
cur = head;
p = cur->next;
q = p->next;
for (int j = 0; j < len - 1- i; j++)
{
if (p->data.score > q->data.score)
{
cur->next = q;
p->next = q->next;
q->next = p;

cur = q;
q = p->next;
continue;

//t = q;
//q = p;
//p = t;
}
cur = cur->next;
p = cur->next;
q = p->next;
}
}
}

void saveList2File(StuNode * head, char *filePath) //保存到文件中
{
FILE *pfw = fopen(filePath, "w+");
if (NULL == pfw)
exit(-1);
head = head->next;
while (head)
{
fwrite((void *)&head->data, sizeof(Stu), 1, pfw);
head = head->next;
}
fclose(pfw);
}

void destroyListStu(StuNode * head) //销毁链表
{
StuNode *cur;
while (head)
{
cur = head;
head = head->next;
free(cur);
}
}
...全文
121 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
ping527963401 2019-06-04
  • 打赏
  • 举报
回复
引用 10 楼 自信男孩的回复:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>

#pragma warning(disable:4996)

//management 管理、人员管理系统;

#if 0
1、初始化数据库,此时的数据库是文件;(initData2File)
2、读数据库,生成内存数据模型;(createListFromFile)
3、增、查、改、删、排序;
4、更新数据库; saveList2File(保存链表文件) destroyListStu(删除链表)
#endif

typedef struct student
{
char name[30]; //姓名
char sex; //性别
int age; //年龄
int score; //成绩
}Stu;

typedef struct stuNode
{
Stu data;
struct stuNode *next;
}StuNode;

void initData2File(const char *);
StuNode *createListFromFile(const char *filePash); //创建链表,把数据传进来
void traverseStulist(StuNode *head);

void addListStu(StuNode *phead); //增
StuNode *searchListStu(StuNode *head); //查
void deleteListStu(StuNode *head); //删除
void sortListStu(StuNode *head); //排序(按成绩排序)
int lenList(StuNode *head); //求链表长度
void saveList2File(StuNode *head, const char *filePath);
void destroyListStu(StuNode *head);

int main()
{
initData2File("student.data");

StuNode *head = createListFromFile("student.data");

traverseStulist(head);
while (1)
{
system("cls");
traverseStulist(head);

printf("1->add\t 2->search 3->delete 4->sort 5->exit\n");
printf("请输入序号:");
int dix;
scanf("%d", &dix);
StuNode *Phead;
switch (dix)
{
case 1:
addListStu(head);
break;
case 2:
if ((Phead = searchListStu(head)))
{
printf("您要查找的人的信息在本数据库中:\n");
printf("name\t\t\tsex\t\tage\t\tscore\n");
printf("%-10s\t\t%c\t\t%d\t\t%d\n",
Phead->data.name, Phead->data.sex, Phead->data.age, Phead->data.score);
printf("------------------------------\n\n");
}
else
printf("查无此人!\n");
break;
case 3:
deleteListStu(head);
break;
case 4:
sortListStu(head);
break;
case 5:
saveList2File(head,"student.data");
destroyListStu(head);
return 0;
default:
printf("您输入错误!\n");
}
}

return 0;
}

void initData2File(const char *file_path) //1、初始化数据库,此时的数据库是文件;
{
Stu s[4] =
{
{"sunwukong",'x',500,90},
{"wuneng",'x',800,59 },
{"wujing",'m',700,80},
{"tangseng",'m',24,100}
};

FILE *pf = fopen(file_path, "w+");
if (NULL == pf)
exit(-1);
fwrite((char *)s, sizeof(s), 1, pf);
fclose(pf); //关闭文件
}

StuNode *createListFromFile(const char *filePash) //2、读数据库,生成内存数据模型;(createListFromFile)把文件传进来
{
FILE *pf = fopen(filePash, "r+");
if (pf == NULL)
exit(-1);
StuNode *head = (StuNode *)malloc(sizeof(StuNode)); // 创建头结点
if (NULL == head)
exit(-1);
head->next = NULL;
StuNode *cur = (StuNode *)malloc(sizeof(StuNode));
if (!cur)
exit(-1);
while (fread((void *)&cur->data, sizeof(Stu), 1, pf))
{
cur->next = head->next;
head->next = cur;

cur = (StuNode *)malloc(sizeof(StuNode));
if (!cur)
exit(-1);
}
free(cur);
//free(pf);
fclose(pf);

return head;
}

void traverseStulist(StuNode * head)
{
StuNode *p = head;

while (p) {
printf("%-10s\t\t%c\t\t%d\t\t%d\n", p->data.name, p->data.sex, p->data.age, p->data.score);
p = p->next;
}
/*
head = head->next;
printf("name\t\t\tsex\t\tage\t\tscore\n");
while (head)
{
printf("%-10s\t\t%c\t\t%d\t\t%d\n", head->data.name, head->data.sex, head->data.age, head->data.score);
head = head->next;
}
*/
}

void addListStu(StuNode * head) //增加数据
{
StuNode *cur = (StuNode*)malloc(sizeof(StuNode));
if (NULL == cur)
exit(-1);
cur->next = NULL;

system("cls");

printf("name :");
scanf("%s", cur->data.name);
getchar();

printf("sex :");
scanf("%c", &cur->data.sex);

printf("age :");
scanf("%d", &cur->data.age);

printf("score :");
scanf("%d", &cur->data.score);
getchar();

cur->next = head->next;
head->next = cur;

cur = (StuNode*)malloc(sizeof(StuNode));
}

StuNode *searchListStu(StuNode * head) //查
{
StuNode *p = head->next;
char Name[20];
scanf("%s", Name);

while (p) {
if (strcmp(Name, p->data.name) == 0)
return p;
p = p->next;
}

return NULL;
/*
printf("请输入您要查找的人名:");
char Name[20];
scanf("%s", Name);
head = head->next;
while (head)
{
if (strcmp(Name, head->data.name) == 0)
break;
else
head = head->next;
}
*/
return head;
}

void deleteListStu(StuNode * head)
{
StuNode *pcur, *prev;
StuNode *Dhead = searchListStu(head);
int Index;

if (!Dhead) {
printf("查无此人!\n");
return;
}

//if (Dhead)
//{
printf("您要查找的人的信息在本数据库中:\n");
printf("name\t\t\tsex\t\tage\t\tscore\n");
printf("%-10s\t\t%c\t\t%d\t\t%d\n",
Dhead->data.name, Dhead->data.sex, Dhead->data.age, Dhead->data.score);
printf("------------------------------\n\n");
printf("继续删除请按 1 ,取消请按 2 :");
scanf("%d", &Index);

while (1)
{
if (Index == 1)
{
prev = head;
pcur = head->next;

while (pcur != Dhead) {
prev = prev->next;
pcur = pcur->next;
}
if (pcur == Dhead) {
prev->next = pcur->next;
free(pcur);
break;
}

/*
//head = head->next;
while (Dhead != head->next)
{
head = head->next;
}
cur = Dhead;
head->next = Dhead->next;
free(Dhead);
return;
*/
}
else if (Index == 2)
break;
else
{
printf("您输入有误!请重新输入!\n");
printf("继续删除请按 1 ,取消请按 2 :");
scanf("%d", &Index);
continue;
}
}
//}

//else
// printf("查无此人!\n");
}

int lenList(StuNode * head)
{
int len = 0;
StuNode *p = head->next;

while (p) {
len++;
p = p->next;
}
/*
head = head->next;
while (head)
{
len++;
head = head->next;
}
*/

return len;
}

void sortListStu(StuNode * head)
{
StuNode *cur;
StuNode *p;
StuNode *q;
//StuNode *t;

int len = lenList(head);
for (int i = 0; i < len - 1; i++)
{
cur = head;
p = cur->next;
q = p->next;
for (int j = 0; j < len - 1- i; j++)
{
if (p->data.score > q->data.score)
{
cur->next = q;
p->next = q->next;
q->next = p;

cur = q;
q = p->next;
continue;

//t = q;
//q = p;
//p = t;
}
cur = cur->next;
p = cur->next;
q = p->next;
}
}
}

void saveList2File(StuNode * head, const char *filePath) //保存到文件中
{
StuNode *p = head->next;
FILE *pfw = fopen(filePath, "w+");
if (NULL == pfw)
exit(-1);

while (p) {
fwrite((void *)&p->data, sizeof(Stu), 1, pfw);
p = p->next;
}
/*
while (head)
{
fwrite((void *)&head->data, sizeof(Stu), 1, pfw);
head = head->next;
}
*/
fclose(pfw);
}

void destroyListStu(StuNode * head) //销毁链表
{
StuNode *cur;
while (head)
{
cur = head;
head = head->next;
free(cur);
}
}

供参考,有问题再提出来。
vx:pq08240219
ping527963401 2019-06-04
  • 打赏
  • 举报
回复
引用 10 楼 自信男孩的回复:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>

#pragma warning(disable:4996)

//management 管理、人员管理系统;

#if 0
1、初始化数据库,此时的数据库是文件;(initData2File)
2、读数据库,生成内存数据模型;(createListFromFile)
3、增、查、改、删、排序;
4、更新数据库; saveList2File(保存链表文件) destroyListStu(删除链表)
#endif

typedef struct student
{
char name[30]; //姓名
char sex; //性别
int age; //年龄
int score; //成绩
}Stu;

typedef struct stuNode
{
Stu data;
struct stuNode *next;
}StuNode;

void initData2File(const char *);
StuNode *createListFromFile(const char *filePash); //创建链表,把数据传进来
void traverseStulist(StuNode *head);

void addListStu(StuNode *phead); //增
StuNode *searchListStu(StuNode *head); //查
void deleteListStu(StuNode *head); //删除
void sortListStu(StuNode *head); //排序(按成绩排序)
int lenList(StuNode *head); //求链表长度
void saveList2File(StuNode *head, const char *filePath);
void destroyListStu(StuNode *head);

int main()
{
initData2File("student.data");

StuNode *head = createListFromFile("student.data");

traverseStulist(head);
while (1)
{
system("cls");
traverseStulist(head);

printf("1->add\t 2->search 3->delete 4->sort 5->exit\n");
printf("请输入序号:");
int dix;
scanf("%d", &dix);
StuNode *Phead;
switch (dix)
{
case 1:
addListStu(head);
break;
case 2:
if ((Phead = searchListStu(head)))
{
printf("您要查找的人的信息在本数据库中:\n");
printf("name\t\t\tsex\t\tage\t\tscore\n");
printf("%-10s\t\t%c\t\t%d\t\t%d\n",
Phead->data.name, Phead->data.sex, Phead->data.age, Phead->data.score);
printf("------------------------------\n\n");
}
else
printf("查无此人!\n");
break;
case 3:
deleteListStu(head);
break;
case 4:
sortListStu(head);
break;
case 5:
saveList2File(head,"student.data");
destroyListStu(head);
return 0;
default:
printf("您输入错误!\n");
}
}

return 0;
}

void initData2File(const char *file_path) //1、初始化数据库,此时的数据库是文件;
{
Stu s[4] =
{
{"sunwukong",'x',500,90},
{"wuneng",'x',800,59 },
{"wujing",'m',700,80},
{"tangseng",'m',24,100}
};

FILE *pf = fopen(file_path, "w+");
if (NULL == pf)
exit(-1);
fwrite((char *)s, sizeof(s), 1, pf);
fclose(pf); //关闭文件
}

StuNode *createListFromFile(const char *filePash) //2、读数据库,生成内存数据模型;(createListFromFile)把文件传进来
{
FILE *pf = fopen(filePash, "r+");
if (pf == NULL)
exit(-1);
StuNode *head = (StuNode *)malloc(sizeof(StuNode)); // 创建头结点
if (NULL == head)
exit(-1);
head->next = NULL;
StuNode *cur = (StuNode *)malloc(sizeof(StuNode));
if (!cur)
exit(-1);
while (fread((void *)&cur->data, sizeof(Stu), 1, pf))
{
cur->next = head->next;
head->next = cur;

cur = (StuNode *)malloc(sizeof(StuNode));
if (!cur)
exit(-1);
}
free(cur);
//free(pf);
fclose(pf);

return head;
}

void traverseStulist(StuNode * head)
{
StuNode *p = head;

while (p) {
printf("%-10s\t\t%c\t\t%d\t\t%d\n", p->data.name, p->data.sex, p->data.age, p->data.score);
p = p->next;
}
/*
head = head->next;
printf("name\t\t\tsex\t\tage\t\tscore\n");
while (head)
{
printf("%-10s\t\t%c\t\t%d\t\t%d\n", head->data.name, head->data.sex, head->data.age, head->data.score);
head = head->next;
}
*/
}

void addListStu(StuNode * head) //增加数据
{
StuNode *cur = (StuNode*)malloc(sizeof(StuNode));
if (NULL == cur)
exit(-1);
cur->next = NULL;

system("cls");

printf("name :");
scanf("%s", cur->data.name);
getchar();

printf("sex :");
scanf("%c", &cur->data.sex);

printf("age :");
scanf("%d", &cur->data.age);

printf("score :");
scanf("%d", &cur->data.score);
getchar();

cur->next = head->next;
head->next = cur;

cur = (StuNode*)malloc(sizeof(StuNode));
}

StuNode *searchListStu(StuNode * head) //查
{
StuNode *p = head->next;
char Name[20];
scanf("%s", Name);

while (p) {
if (strcmp(Name, p->data.name) == 0)
return p;
p = p->next;
}

return NULL;
/*
printf("请输入您要查找的人名:");
char Name[20];
scanf("%s", Name);
head = head->next;
while (head)
{
if (strcmp(Name, head->data.name) == 0)
break;
else
head = head->next;
}
*/
return head;
}

void deleteListStu(StuNode * head)
{
StuNode *pcur, *prev;
StuNode *Dhead = searchListStu(head);
int Index;

if (!Dhead) {
printf("查无此人!\n");
return;
}

//if (Dhead)
//{
printf("您要查找的人的信息在本数据库中:\n");
printf("name\t\t\tsex\t\tage\t\tscore\n");
printf("%-10s\t\t%c\t\t%d\t\t%d\n",
Dhead->data.name, Dhead->data.sex, Dhead->data.age, Dhead->data.score);
printf("------------------------------\n\n");
printf("继续删除请按 1 ,取消请按 2 :");
scanf("%d", &Index);

while (1)
{
if (Index == 1)
{
prev = head;
pcur = head->next;

while (pcur != Dhead) {
prev = prev->next;
pcur = pcur->next;
}
if (pcur == Dhead) {
prev->next = pcur->next;
free(pcur);
break;
}

/*
//head = head->next;
while (Dhead != head->next)
{
head = head->next;
}
cur = Dhead;
head->next = Dhead->next;
free(Dhead);
return;
*/
}
else if (Index == 2)
break;
else
{
printf("您输入有误!请重新输入!\n");
printf("继续删除请按 1 ,取消请按 2 :");
scanf("%d", &Index);
continue;
}
}
//}

//else
// printf("查无此人!\n");
}

int lenList(StuNode * head)
{
int len = 0;
StuNode *p = head->next;

while (p) {
len++;
p = p->next;
}
/*
head = head->next;
while (head)
{
len++;
head = head->next;
}
*/

return len;
}

void sortListStu(StuNode * head)
{
StuNode *cur;
StuNode *p;
StuNode *q;
//StuNode *t;

int len = lenList(head);
for (int i = 0; i < len - 1; i++)
{
cur = head;
p = cur->next;
q = p->next;
for (int j = 0; j < len - 1- i; j++)
{
if (p->data.score > q->data.score)
{
cur->next = q;
p->next = q->next;
q->next = p;

cur = q;
q = p->next;
continue;

//t = q;
//q = p;
//p = t;
}
cur = cur->next;
p = cur->next;
q = p->next;
}
}
}

void saveList2File(StuNode * head, const char *filePath) //保存到文件中
{
StuNode *p = head->next;
FILE *pfw = fopen(filePath, "w+");
if (NULL == pfw)
exit(-1);

while (p) {
fwrite((void *)&p->data, sizeof(Stu), 1, pfw);
p = p->next;
}
/*
while (head)
{
fwrite((void *)&head->data, sizeof(Stu), 1, pfw);
head = head->next;
}
*/
fclose(pfw);
}

void destroyListStu(StuNode * head) //销毁链表
{
StuNode *cur;
while (head)
{
cur = head;
head = head->next;
free(cur);
}
}

供参考,有问题再提出来。
能加个好友,私聊吗?有些问题想请教一下😌
ping527963401 2019-06-04
  • 打赏
  • 举报
回复
引用 10 楼 自信男孩的回复:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>

#pragma warning(disable:4996)

//management 管理、人员管理系统;

#if 0
1、初始化数据库,此时的数据库是文件;(initData2File)
2、读数据库,生成内存数据模型;(createListFromFile)
3、增、查、改、删、排序;
4、更新数据库; saveList2File(保存链表文件) destroyListStu(删除链表)
#endif

typedef struct student
{
char name[30]; //姓名
char sex; //性别
int age; //年龄
int score; //成绩
}Stu;

typedef struct stuNode
{
Stu data;
struct stuNode *next;
}StuNode;

void initData2File(const char *);
StuNode *createListFromFile(const char *filePash); //创建链表,把数据传进来
void traverseStulist(StuNode *head);

void addListStu(StuNode *phead); //增
StuNode *searchListStu(StuNode *head); //查
void deleteListStu(StuNode *head); //删除
void sortListStu(StuNode *head); //排序(按成绩排序)
int lenList(StuNode *head); //求链表长度
void saveList2File(StuNode *head, const char *filePath);
void destroyListStu(StuNode *head);

int main()
{
initData2File("student.data");

StuNode *head = createListFromFile("student.data");

traverseStulist(head);
while (1)
{
system("cls");
traverseStulist(head);

printf("1->add\t 2->search 3->delete 4->sort 5->exit\n");
printf("请输入序号:");
int dix;
scanf("%d", &dix);
StuNode *Phead;
switch (dix)
{
case 1:
addListStu(head);
break;
case 2:
if ((Phead = searchListStu(head)))
{
printf("您要查找的人的信息在本数据库中:\n");
printf("name\t\t\tsex\t\tage\t\tscore\n");
printf("%-10s\t\t%c\t\t%d\t\t%d\n",
Phead->data.name, Phead->data.sex, Phead->data.age, Phead->data.score);
printf("------------------------------\n\n");
}
else
printf("查无此人!\n");
break;
case 3:
deleteListStu(head);
break;
case 4:
sortListStu(head);
break;
case 5:
saveList2File(head,"student.data");
destroyListStu(head);
return 0;
default:
printf("您输入错误!\n");
}
}

return 0;
}

void initData2File(const char *file_path) //1、初始化数据库,此时的数据库是文件;
{
Stu s[4] =
{
{"sunwukong",'x',500,90},
{"wuneng",'x',800,59 },
{"wujing",'m',700,80},
{"tangseng",'m',24,100}
};

FILE *pf = fopen(file_path, "w+");
if (NULL == pf)
exit(-1);
fwrite((char *)s, sizeof(s), 1, pf);
fclose(pf); //关闭文件
}

StuNode *createListFromFile(const char *filePash) //2、读数据库,生成内存数据模型;(createListFromFile)把文件传进来
{
FILE *pf = fopen(filePash, "r+");
if (pf == NULL)
exit(-1);
StuNode *head = (StuNode *)malloc(sizeof(StuNode)); // 创建头结点
if (NULL == head)
exit(-1);
head->next = NULL;
StuNode *cur = (StuNode *)malloc(sizeof(StuNode));
if (!cur)
exit(-1);
while (fread((void *)&cur->data, sizeof(Stu), 1, pf))
{
cur->next = head->next;
head->next = cur;

cur = (StuNode *)malloc(sizeof(StuNode));
if (!cur)
exit(-1);
}
free(cur);
//free(pf);
fclose(pf);

return head;
}

void traverseStulist(StuNode * head)
{
StuNode *p = head;

while (p) {
printf("%-10s\t\t%c\t\t%d\t\t%d\n", p->data.name, p->data.sex, p->data.age, p->data.score);
p = p->next;
}
/*
head = head->next;
printf("name\t\t\tsex\t\tage\t\tscore\n");
while (head)
{
printf("%-10s\t\t%c\t\t%d\t\t%d\n", head->data.name, head->data.sex, head->data.age, head->data.score);
head = head->next;
}
*/
}

void addListStu(StuNode * head) //增加数据
{
StuNode *cur = (StuNode*)malloc(sizeof(StuNode));
if (NULL == cur)
exit(-1);
cur->next = NULL;

system("cls");

printf("name :");
scanf("%s", cur->data.name);
getchar();

printf("sex :");
scanf("%c", &cur->data.sex);

printf("age :");
scanf("%d", &cur->data.age);

printf("score :");
scanf("%d", &cur->data.score);
getchar();

cur->next = head->next;
head->next = cur;

cur = (StuNode*)malloc(sizeof(StuNode));
}

StuNode *searchListStu(StuNode * head) //查
{
StuNode *p = head->next;
char Name[20];
scanf("%s", Name);

while (p) {
if (strcmp(Name, p->data.name) == 0)
return p;
p = p->next;
}

return NULL;
/*
printf("请输入您要查找的人名:");
char Name[20];
scanf("%s", Name);
head = head->next;
while (head)
{
if (strcmp(Name, head->data.name) == 0)
break;
else
head = head->next;
}
*/
return head;
}

void deleteListStu(StuNode * head)
{
StuNode *pcur, *prev;
StuNode *Dhead = searchListStu(head);
int Index;

if (!Dhead) {
printf("查无此人!\n");
return;
}

//if (Dhead)
//{
printf("您要查找的人的信息在本数据库中:\n");
printf("name\t\t\tsex\t\tage\t\tscore\n");
printf("%-10s\t\t%c\t\t%d\t\t%d\n",
Dhead->data.name, Dhead->data.sex, Dhead->data.age, Dhead->data.score);
printf("------------------------------\n\n");
printf("继续删除请按 1 ,取消请按 2 :");
scanf("%d", &Index);

while (1)
{
if (Index == 1)
{
prev = head;
pcur = head->next;

while (pcur != Dhead) {
prev = prev->next;
pcur = pcur->next;
}
if (pcur == Dhead) {
prev->next = pcur->next;
free(pcur);
break;
}

/*
//head = head->next;
while (Dhead != head->next)
{
head = head->next;
}
cur = Dhead;
head->next = Dhead->next;
free(Dhead);
return;
*/
}
else if (Index == 2)
break;
else
{
printf("您输入有误!请重新输入!\n");
printf("继续删除请按 1 ,取消请按 2 :");
scanf("%d", &Index);
continue;
}
}
//}

//else
// printf("查无此人!\n");
}

int lenList(StuNode * head)
{
int len = 0;
StuNode *p = head->next;

while (p) {
len++;
p = p->next;
}
/*
head = head->next;
while (head)
{
len++;
head = head->next;
}
*/

return len;
}

void sortListStu(StuNode * head)
{
StuNode *cur;
StuNode *p;
StuNode *q;
//StuNode *t;

int len = lenList(head);
for (int i = 0; i < len - 1; i++)
{
cur = head;
p = cur->next;
q = p->next;
for (int j = 0; j < len - 1- i; j++)
{
if (p->data.score > q->data.score)
{
cur->next = q;
p->next = q->next;
q->next = p;

cur = q;
q = p->next;
continue;

//t = q;
//q = p;
//p = t;
}
cur = cur->next;
p = cur->next;
q = p->next;
}
}
}

void saveList2File(StuNode * head, const char *filePath) //保存到文件中
{
StuNode *p = head->next;
FILE *pfw = fopen(filePath, "w+");
if (NULL == pfw)
exit(-1);

while (p) {
fwrite((void *)&p->data, sizeof(Stu), 1, pfw);
p = p->next;
}
/*
while (head)
{
fwrite((void *)&head->data, sizeof(Stu), 1, pfw);
head = head->next;
}
*/
fclose(pfw);
}

void destroyListStu(StuNode * head) //销毁链表
{
StuNode *cur;
while (head)
{
cur = head;
head = head->next;
free(cur);
}
}

供参考,有问题再提出来。
感谢!困扰多天地问题得已解决,非常感谢!😃
jiht594 2019-06-03
  • 打赏
  • 举报
回复
引用 6 楼 ping527963401 的回复:
[quote=引用 4 楼 赵4老师的回复:]崩溃的时候在弹出的对话框按相应按钮进入调试,按Alt+7键查看Call Stack即“调用堆栈”里面从上到下列出的对应从里层到外层的函数调用历史。双击某一行可将光标定位到此次调用的源代码或汇编指令处,看不懂时双击下一行,直到能看懂为止

弄了好长时间了,没查出来哪错了,感觉心里总不对劲[/quote]
程序启动、直接按5,没有触发断点
赵4老师 2019-06-03
  • 打赏
  • 举报
回复
数据结构对单链表进行数据排序 http://bbs.csdn.net/topics/392201633
#include <time.h>
#include <stdlib.h>
#include <windows.h>
int main() {
    int a,b[11];//本来是b[10],为判断哪句越界,故意声明为b[11]

    srand((unsigned int)time(NULL));//按两次F11,等黄色右箭头指向本行时,调试、新建断点、新建数据断点,地址:&b[10],字节计数:4,确定。
    while (1) {//按F5,会停在下面某句,此时a的值为10,b[10]已经被修改为对应0..4之一。
        b[(a=rand()%11)]=0;
        Sleep(100);
        b[(a=rand()%11)]=1;
        Sleep(100);
        b[(a=rand()%11)]=2;
        Sleep(100);
        b[(a=rand()%11)]=3;
        Sleep(100);
        b[(a=rand()%11)]=4;
        Sleep(100);
    }
    return 0;
}
自信男孩 2019-06-03
  • 打赏
  • 举报
回复
#include<stdio.h>
#include<stdlib.h>
#include<string.h>

#pragma warning(disable:4996)

//management 管理、人员管理系统;

#if 0
1、初始化数据库,此时的数据库是文件;(initData2File)
2、读数据库,生成内存数据模型;(createListFromFile)
3、增、查、改、删、排序;
4、更新数据库; saveList2File(保存链表文件) destroyListStu(删除链表)
#endif

typedef struct student
{
char name[30]; //姓名
char sex; //性别
int age; //年龄
int score; //成绩
}Stu;

typedef struct stuNode
{
Stu data;
struct stuNode *next;
}StuNode;

void initData2File(const char *);
StuNode *createListFromFile(const char *filePash); //创建链表,把数据传进来
void traverseStulist(StuNode *head);

void addListStu(StuNode *phead); //增
StuNode *searchListStu(StuNode *head); //查
void deleteListStu(StuNode *head); //删除
void sortListStu(StuNode *head); //排序(按成绩排序)
int lenList(StuNode *head); //求链表长度
void saveList2File(StuNode *head, const char *filePath);
void destroyListStu(StuNode *head);

int main()
{
initData2File("student.data");

StuNode *head = createListFromFile("student.data");

traverseStulist(head);
while (1)
{
system("cls");
traverseStulist(head);

printf("1->add\t 2->search 3->delete 4->sort 5->exit\n");
printf("请输入序号:");
int dix;
scanf("%d", &dix);
StuNode *Phead;
switch (dix)
{
case 1:
addListStu(head);
break;
case 2:
if ((Phead = searchListStu(head)))
{
printf("您要查找的人的信息在本数据库中:\n");
printf("name\t\t\tsex\t\tage\t\tscore\n");
printf("%-10s\t\t%c\t\t%d\t\t%d\n",
Phead->data.name, Phead->data.sex, Phead->data.age, Phead->data.score);
printf("------------------------------\n\n");
}
else
printf("查无此人!\n");
break;
case 3:
deleteListStu(head);
break;
case 4:
sortListStu(head);
break;
case 5:
saveList2File(head,"student.data");
destroyListStu(head);
return 0;
default:
printf("您输入错误!\n");
}
}

return 0;
}

void initData2File(const char *file_path) //1、初始化数据库,此时的数据库是文件;
{
Stu s[4] =
{
{"sunwukong",'x',500,90},
{"wuneng",'x',800,59 },
{"wujing",'m',700,80},
{"tangseng",'m',24,100}
};

FILE *pf = fopen(file_path, "w+");
if (NULL == pf)
exit(-1);
fwrite((char *)s, sizeof(s), 1, pf);
fclose(pf); //关闭文件
}

StuNode *createListFromFile(const char *filePash) //2、读数据库,生成内存数据模型;(createListFromFile)把文件传进来
{
FILE *pf = fopen(filePash, "r+");
if (pf == NULL)
exit(-1);
StuNode *head = (StuNode *)malloc(sizeof(StuNode)); // 创建头结点
if (NULL == head)
exit(-1);
head->next = NULL;
StuNode *cur = (StuNode *)malloc(sizeof(StuNode));
if (!cur)
exit(-1);
while (fread((void *)&cur->data, sizeof(Stu), 1, pf))
{
cur->next = head->next;
head->next = cur;

cur = (StuNode *)malloc(sizeof(StuNode));
if (!cur)
exit(-1);
}
free(cur);
//free(pf);
fclose(pf);

return head;
}

void traverseStulist(StuNode * head)
{
StuNode *p = head;

while (p) {
printf("%-10s\t\t%c\t\t%d\t\t%d\n", p->data.name, p->data.sex, p->data.age, p->data.score);
p = p->next;
}
/*
head = head->next;
printf("name\t\t\tsex\t\tage\t\tscore\n");
while (head)
{
printf("%-10s\t\t%c\t\t%d\t\t%d\n", head->data.name, head->data.sex, head->data.age, head->data.score);
head = head->next;
}
*/
}

void addListStu(StuNode * head) //增加数据
{
StuNode *cur = (StuNode*)malloc(sizeof(StuNode));
if (NULL == cur)
exit(-1);
cur->next = NULL;

system("cls");

printf("name :");
scanf("%s", cur->data.name);
getchar();

printf("sex :");
scanf("%c", &cur->data.sex);

printf("age :");
scanf("%d", &cur->data.age);

printf("score :");
scanf("%d", &cur->data.score);
getchar();

cur->next = head->next;
head->next = cur;

cur = (StuNode*)malloc(sizeof(StuNode));
}

StuNode *searchListStu(StuNode * head) //查
{
StuNode *p = head->next;
char Name[20];
scanf("%s", Name);

while (p) {
if (strcmp(Name, p->data.name) == 0)
return p;
p = p->next;
}

return NULL;
/*
printf("请输入您要查找的人名:");
char Name[20];
scanf("%s", Name);
head = head->next;
while (head)
{
if (strcmp(Name, head->data.name) == 0)
break;
else
head = head->next;
}
*/
return head;
}

void deleteListStu(StuNode * head)
{
StuNode *pcur, *prev;
StuNode *Dhead = searchListStu(head);
int Index;

if (!Dhead) {
printf("查无此人!\n");
return;
}

//if (Dhead)
//{
printf("您要查找的人的信息在本数据库中:\n");
printf("name\t\t\tsex\t\tage\t\tscore\n");
printf("%-10s\t\t%c\t\t%d\t\t%d\n",
Dhead->data.name, Dhead->data.sex, Dhead->data.age, Dhead->data.score);
printf("------------------------------\n\n");
printf("继续删除请按 1 ,取消请按 2 :");
scanf("%d", &Index);

while (1)
{
if (Index == 1)
{
prev = head;
pcur = head->next;

while (pcur != Dhead) {
prev = prev->next;
pcur = pcur->next;
}
if (pcur == Dhead) {
prev->next = pcur->next;
free(pcur);
break;
}

/*
//head = head->next;
while (Dhead != head->next)
{
head = head->next;
}
cur = Dhead;
head->next = Dhead->next;
free(Dhead);
return;
*/
}
else if (Index == 2)
break;
else
{
printf("您输入有误!请重新输入!\n");
printf("继续删除请按 1 ,取消请按 2 :");
scanf("%d", &Index);
continue;
}
}
//}

//else
// printf("查无此人!\n");
}

int lenList(StuNode * head)
{
int len = 0;
StuNode *p = head->next;

while (p) {
len++;
p = p->next;
}
/*
head = head->next;
while (head)
{
len++;
head = head->next;
}
*/

return len;
}

void sortListStu(StuNode * head)
{
StuNode *cur;
StuNode *p;
StuNode *q;
//StuNode *t;

int len = lenList(head);
for (int i = 0; i < len - 1; i++)
{
cur = head;
p = cur->next;
q = p->next;
for (int j = 0; j < len - 1- i; j++)
{
if (p->data.score > q->data.score)
{
cur->next = q;
p->next = q->next;
q->next = p;

cur = q;
q = p->next;
continue;

//t = q;
//q = p;
//p = t;
}
cur = cur->next;
p = cur->next;
q = p->next;
}
}
}

void saveList2File(StuNode * head, const char *filePath) //保存到文件中
{
StuNode *p = head->next;
FILE *pfw = fopen(filePath, "w+");
if (NULL == pfw)
exit(-1);

while (p) {
fwrite((void *)&p->data, sizeof(Stu), 1, pfw);
p = p->next;
}
/*
while (head)
{
fwrite((void *)&head->data, sizeof(Stu), 1, pfw);
head = head->next;
}
*/
fclose(pfw);
}

void destroyListStu(StuNode * head) //销毁链表
{
StuNode *cur;
while (head)
{
cur = head;
head = head->next;
free(cur);
}
}

供参考,有问题再提出来。
ping527963401 2019-06-03
  • 打赏
  • 举报
回复
引用 7 楼 赵4老师的回复:
数据结构对单链表进行数据排序 http://bbs.csdn.net/topics/392201633
#include <time.h>
#include <stdlib.h>
#include <windows.h>
int main() {
    int a,b[11];//本来是b[10],为判断哪句越界,故意声明为b[11]

    srand((unsigned int)time(NULL));//按两次F11,等黄色右箭头指向本行时,调试、新建断点、新建数据断点,地址:&b[10],字节计数:4,确定。
    while (1) {//按F5,会停在下面某句,此时a的值为10,b[10]已经被修改为对应0..4之一。
        b[(a=rand()%11)]=0;
        Sleep(100);
        b[(a=rand()%11)]=1;
        Sleep(100);
        b[(a=rand()%11)]=2;
        Sleep(100);
        b[(a=rand()%11)]=3;
        Sleep(100);
        b[(a=rand()%11)]=4;
        Sleep(100);
    }
    return 0;
}
调试了好多次,总是在void saveList2File(StuNode * head, char *filePath)       //保存到文件中 里面报错,但就是不知道具体是哪一个,这里面就那几句,都一一对应好多次了,实在不知道错哪里了😂 能加个扣扣吗?527963401
ping527963401 2019-06-02
  • 打赏
  • 举报
回复
我也怀疑是内存越界,但查不出来是哪?都好几个小时了,能帮我看看具体是哪出问题了吗?感谢
636f6c696e 2019-06-02
  • 打赏
  • 举报
回复
断点说明你的链表操作有内存越界
ping527963401 2019-06-02
  • 打赏
  • 举报
回复
增,查,删,排序,都没问题,运行5,保存到文件中就出错,说是引发了一个断点,到文件中查看,也什么都没有,各位大佬帮我看看哪儿出错了,感谢!
ping527963401 2019-06-02
  • 打赏
  • 举报
回复
引用 4 楼 赵4老师的回复:
崩溃的时候在弹出的对话框按相应按钮进入调试,按Alt+7键查看Call Stack即“调用堆栈”里面从上到下列出的对应从里层到外层的函数调用历史。双击某一行可将光标定位到此次调用的源代码或汇编指令处,看不懂时双击下一行,直到能看懂为止
弄了好长时间了,没查出来哪错了,感觉心里总不对劲
ping527963401 2019-06-02
  • 打赏
  • 举报
回复
引用 4 楼 赵4老师的回复:
崩溃的时候在弹出的对话框按相应按钮进入调试,按Alt+7键查看Call Stack即“调用堆栈”里面从上到下列出的对应从里层到外层的函数调用历史。双击某一行可将光标定位到此次调用的源代码或汇编指令处,看不懂时双击下一行,直到能看懂为止
我用的是VS2017,现在自学阶段,没有系统的学过这个软件,能告诉我具体怎么操作吗?感谢
赵4老师 2019-06-02
  • 打赏
  • 举报
回复
崩溃的时候在弹出的对话框按相应按钮进入调试,按Alt+7键查看Call Stack即“调用堆栈”里面从上到下列出的对应从里层到外层的函数调用历史。双击某一行可将光标定位到此次调用的源代码或汇编指令处,看不懂时双击下一行,直到能看懂为止

69,371

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧