ADD file via upload

main
p6ngpykm4 8 months ago
parent 5c5c192a72
commit 016fab5d4e

@ -0,0 +1,881 @@
#include <stdio.h>
#include <windows.h>
#include <assert.h>
#include <string.h>
#include <stdlib.h>
#include <mmsystem.h>
#include <math.h>
#pragma comment(lib,"winmm.lib")
#define MAX 1000
#define NAME_MAX 20
#define SEX_MAX 5
#define ADDR_MAX 30
#define TELE_MAX 12
#define S_NUM 13
#define BLACK 0
#define BLUE 1
#define GREEN 2
#define CYAN 3
#define RED 4
#define MAGENTA 5
#define BROWN 6
#define LIGHTGRAY 7
#define DARKGRAY 8
#define LIGHTBLUE 9
#define LIGHTGREEN 10
#define LIGHTCYAN 11
#define LIGHTRED 12
#define LIGHTMAGENTA 13
#define YELLOW 14
#define WHITE 15
#define T double
#define _CRT_SECURE_NO_WARNINGS 1
#include <io.h>
#include <time.h>
#define error(message) { printf(message "\n"); fflush(stdout); system("pause"); exit(-1); }
#ifndef MIN
#define MIN(a,b) (((a) < (b)) ? (a) : (b))
#endif
#ifndef ENABLE_VIRTUAL_TERMINAL_PROCESSING
#define ENABLE_VIRTUAL_TERMINAL_PROCESSING 0x4
#endif
#define MID_FILE_PATH "001.mp3"
#define LYRIC_FILE_PATH "lyric.lrc"
#define PIC1_PATH "pic1.bmp"
#define PIC2_PATH "pic2.bmp"
#pragma pack(1)
/*
CYAN
MAGENTA:
LIGHTGRAY:
*/
T c(T x,T y,T r)
{
return sqrt(x*x+y*y)-r;
}
T u(T x,T y,T t)
{
return x*cos(t)+y*sin(t);
}
T v(T x,T y,T t)
{
return y*cos(t)-x*sin(t);
}
T fa(T x,T y)
{
return fmin(c(x,y,0.5),c(x*0.47+0.15,y+0.25,0.3));
}
T no(T x,T y)
{
return c(x*1.2+0.97,y+0.25,0.2);
}
T nh(T x,T y)
{
return fmin(c(x+0.9,y+0.25,0.03),c(x+0.75,y+0.25,0.03));
}
T ea(T x,T y)
{
return fmin(c(x*1.7+0.3,y+0.7,0.15),c(u(x,y,0.25)*1.7,v(x,y,0.25)+0.65,0.15));
}
T ey(T x,T y)
{
return fmin(c(x+0.4,y+0.35,0.1),c(x+0.15,y+0.35,0.1));
}
T pu(T x,T y)
{
return fmin(c(x+0.38,y+0.33,0.03),c(x+0.13,y+0.33,0.03));
}
T fr(T x,T y)
{
return c(x*1.1-0.3,y+0.1,0.15);
}
T mo(T x,T y)
{
return fmax(c(x+0.15,y-0.05,0.2),-c(x+0.15,y,0.25));
}
T o(T x,T y,T(*f)
(T,T),T i)
{
T r=f(x,y);return fabs(r)<0.02?(atan2(f(x,y+1e-3)-r,f(x+1e-3,y)-r)+0.3)*1.273+6.5:r<0?i:0;
}
T s(T x,T y,T(*f)(T,T),T i)
{
return f(x,y)<0?i:0;
}
T f(T x,T y)
{
return o(x,y,no,1)?fmax(o(x,y,no,1),s(x,y,nh,12)):fmax(o(x,y,fa,1),fmax(o(x,y,ey,11),fmax(o(x,y,ea,1),fmax(o(x,y,mo,1),fmax(s(x,y,fr,13),s(x,y,pu,12))))));
}
typedef union {
char user_name[20];
char user_password[20];
} User;
// 定义管理员共同体,和使用者类似,可按需扩展更多成员
typedef union {
char admin_name[20];
char admin_password[20];
} Administrator;
// 定义一个结构体来包含共同体,用于区分用户类型等更多操作(比如后续可以添加权限标识等字段)
typedef struct {
int user_type; // 0表示使用者1表示管理员可根据实际扩展更多类型
union {
User user;
Administrator admin;
} info;
} Person;
const char ADMIN_NAME[] = "a";
const char ADMIN_PASSWORD[] = "1";
// 简单模拟普通使用者用户名和密码,实际可从文件等读取验证
const char USER_NAME[] = "u";
const char USER_PASSWORD[] = "6";
//成员信息
typedef struct ContactMember
{
char Name[NAME_MAX];
char Sex[SEX_MAX];
int Age;
char Number[TELE_MAX];
char Address[ADDR_MAX];
int Snumber;
}ContactMember;
typedef struct Contact
{
ContactMember ContactMembers[MAX];
int sz;
}Contact;
//设置颜色
void SetColor(int ForgC, int BackC) {
WORD wColor;
HANDLE hStdOut;
hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
wColor = ((BackC & 0x0F) << 4) + (ForgC & 0x0F);
SetConsoleTextAttribute(hStdOut, wColor);
}
//初始化通讯录
void InitContact(Contact* con);
//将新联系人信息添加到文件末尾
void AddContactToFile(ContactMember newContact, const char* filename);
// 删除指定联系人(通过姓名匹配删除)
void DeleteContact(Contact* con);
// 将通讯录结构体中的联系人信息写回到文件中
void WriteContactToFile(Contact* con, const char* filename);
//查找指定联系人
void SearchContact(Contact* con);
// 修改联系人功能函数
void ModifyContact(Contact* con, const char* filename);
// 从文件读取联系人信息到通讯录结构体中
void ReadContactFromFile(Contact* con, const char* filename);
// 将新联系人信息添加到文件末尾
void AddContactToFile(ContactMember newContact, const char* filename);
// 展示联系人信息
void ReadContact(Contact* con);
// 清空所有联系人(在内存中清空,同时可配合重新写入文件清空文件内容)
void ClearContacts(Contact* con, const char* filename);
//按姓名升序排序所有联系人
void SortContacts1(Contact* con);
//按学号升序排序所有联系人
void SortContactsBySnumber1(Contact* con);
//按姓名降序排序所有联系人
void SortContacts2(Contact* con);
//按学号降序排序所有联系人
void SortContactsBySnumber2(Contact* con);
// 登录验证函数返回值表示用户类型0为使用者1为管理员-1表示登录失败
int login(Person *p);
//创建一个文件
void loadContacts(Contact* con);
//存入数据
void saveContacts(Contact* con);
void welcome()
{
printf("*********************************\n");
printf("********欢迎使用通讯录系统*******\n");
printf("*********************************\n");
printf("Q 1 进入!\n");
printf("1----Yes 2----No\n");
}
void menu()
{
printf("***************************************\n");
printf("****** 通讯录 **************\n");
printf("****** 1.添加 2.删除 **************\n");
printf("****** 3.查找 4.修改 **************\n");
printf("****** 5.阅览 6.清空 **************\n");
printf("****** 7.排序 8.更换字体颜色 ******\n");
printf("*****************0.退出****************\n");
printf("***************************************\n");
}
void yanse()
{
printf("0----black 1----blue\n");
printf("2----green 3----cyan\n");
printf("4----red 5----magenta\n");
printf("6----brown 7----lightgray\n");
printf("8----darkgray 9----lightblue\n");
printf("10----lightgreen 11----lightcyan\n");
printf("12----lightred 13----lightmagenta\n");
printf("14----yellow\n");
}
// 调用Windows的API播放mid音频 (ps: 这个API用来播放mp3音乐也是可以的~)
BOOLEAN playMusic(const char* filePath) {
HMODULE module = LoadLibraryA("winmm.dll");
typedef MCIERROR(WINAPI* MciSendStringT)(LPCSTR lpstrCommand, LPSTR lpstrReturnString, UINT uReturnLength, HWND hwndCallback);
MciSendStringT func_mciSendStringA = (MciSendStringT)GetProcAddress(module, "mciSendStringA");
if (func_mciSendStringA == NULL)
return FALSE;
char buff[255], command[100];
sprintf_s(command, 100, "open %s alias playsound_134", filePath);
func_mciSendStringA(command, buff, 254, NULL);
sprintf_s(command, 100, "set playsound_134 time format milliseconds");
func_mciSendStringA(command, buff, 254, NULL);
sprintf_s(command, 100, "status playsound_134 length");
func_mciSendStringA(command, buff, 254, NULL);
sprintf_s(command, 100, "play playsound_134 from 0 to %s", buff);
func_mciSendStringA(command, buff, 254, NULL);
return TRUE;
}
int main(int a,char**b)
{
for(T y=-1,s=a>1?strtod(b[1],0):1;
y<0.6;y+=0.05/s,putchar('\n'))
for(T x=-1;x<0.6;x+=0.025/s)
putchar(" .|/=\\|/=\\| @!"[(int)f(u(x,y,0.3),v(x,y,0.3))]);
printf("\n什么颜色的字好看呢?\n");
yanse();
int ys;
scanf("%d",&ys);
if(0==ys) SetColor(BLACK, WHITE);
else if(1==ys) SetColor(BLUE, WHITE);
else if(2==ys) SetColor(GREEN, WHITE);
else if(3==ys) SetColor(CYAN, WHITE);
else if(4==ys) SetColor(RED, WHITE);
else if(5==ys) SetColor(MAGENTA, WHITE);
else if(6==ys) SetColor(BROWN, WHITE);
else if(7==ys) SetColor(LIGHTGRAY, WHITE);
else if(8==ys) SetColor(DARKGRAY, WHITE);
else if(9==ys) SetColor(LIGHTBLUE, WHITE);
else if(10==ys) SetColor(LIGHTGREEN, WHITE);
else if(11==ys) SetColor(LIGHTCYAN, WHITE);
else if(12==ys) SetColor(LIGHTRED, WHITE);
else if(13==ys) SetColor(LIGHTMAGENTA, WHITE);
else if(14==ys) SetColor(YELLOW, BLACK);
else printf("输入不合法,无法更换字体颜色!\n");
// 随意设置颜色
int s,z,choice;
welcome();
scanf("%d",&z);
if(1==z){
Contact con;
const char* filename = "contacts.txt"; // 假设文件名是contacts.txt可按需修改
InitContact(&con);
Person person;
int login_result=login(&person);
if(login_result==-1){
printf("登录失败,请检查用户名和密码!\n");
return 0;
}
//播放音乐
playMusic(MID_FILE_PATH);
system("pause");
do
{
menu();
scanf("%d", &s);
switch (s)
{
case 1:
if(person.user_type==1){
ContactMember newContact;
char Name[NAME_MAX];
char Sex[SEX_MAX];
int Age;
char Number[TELE_MAX];
char Address[ADDR_MAX];
int Snumber;
printf("请输入新联系人姓名:");
scanf("%s",Name);
printf("请输入新联系人性别:");
scanf("%s",Sex);
printf("请输入新联系人年龄:");
scanf("%d",&Age);
printf("请输入新联系人电话号码:");
scanf("%s",Number);
printf("请输入新联系人地址:");
scanf("%s",Address);
printf("请输入新联系人学号: ");
scanf("%d",&Snumber);
strcpy(newContact.Name, Name);
strcpy(newContact.Sex, Sex);
newContact.Age = Age;
strcpy(newContact.Address, Address);
strcpy(newContact.Number, Number);
newContact.Snumber=Snumber;
AddContactToFile(newContact, filename);
// 再次从文件读取联系人信息(可看到新增后的情况)
ReadContactFromFile(&con, filename);
ReadContact(&con);
}
else
printf("使用者无此权限!\n");
break;
case 2:if(person.user_type==1){
DeleteContact(&con);
WriteContactToFile(&con, "contacts.txt");
}
else
printf("使用者无此权限!\n");
break;
case 3:if(person.user_type==1)
SearchContact(&con);
else
printf("使用者无此权限!\n");
break;
case 4:if(person.user_type==1){
ModifyContact(&con, "contacts.txt");
ReadContact(&con);
}
else
printf("使用者无此权限!\n");
break;
case 5:
// 从文件读取联系人信息
ReadContactFromFile(&con, filename);
// 展示联系人信息
ReadContact(&con);
break;
case 6:if(person.user_type==1)
ClearContacts(&con, "contacts.txt");
else
printf("使用者无此权限!\n");
break;
case 7:
printf("排序方式:\n1代表按学号升序排序\n2代表按姓名升序排序\n3代表按学号降序排序\n4代表按姓名降序排序\n");
printf("请选择排序方式:\n");
scanf("%d",&choice);
if(choice==1)
SortContactsBySnumber1(&con);
else if(choice==2)
SortContacts1(&con);
else if(choice==3)
SortContactsBySnumber2(&con);
else if(choice==4)
SortContacts2(&con);
else
printf("请输入正确的选项");
break;
case 8:printf("请选择喜欢的字体颜色:\n");
yanse();
scanf("%d",&ys);
if(0==ys) SetColor(BLACK, WHITE);
else if(1==ys) SetColor(BLUE, WHITE);
else if(2==ys) SetColor(GREEN, WHITE);
else if(3==ys) SetColor(CYAN, WHITE);
else if(4==ys) SetColor(RED, WHITE);
else if(5==ys) SetColor(MAGENTA, WHITE);
else if(6==ys) SetColor(BROWN, WHITE);
else if(7==ys) SetColor(LIGHTGRAY, WHITE);
else if(8==ys) SetColor(DARKGRAY, WHITE);
else if(9==ys) SetColor(LIGHTBLUE, WHITE);
else if(10==ys) SetColor(LIGHTGREEN, WHITE);
else if(11==ys) SetColor(LIGHTCYAN, WHITE);
else if(12==ys) SetColor(LIGHTRED, WHITE);
else if(13==ys) SetColor(LIGHTMAGENTA, WHITE);
else if(14==ys) SetColor(YELLOW, BLACK);
else printf("输入不合法,无法更换字体颜色!");
break;
case 0:
saveContacts(&con);
printf("已成功退出!\n");
exit(0);
break;
default:
printf("请输入正确的选项!\n");
}
} while (s);
loadContacts(&con);
}
else printf("欢迎下次使用!\n");
return 0;
}
//初始化成员信息
void InitContact(Contact* con)
{
con->sz = 0;
memset(con, 0, sizeof(con));
}
//查找目标成员位置
int FindName(Contact* con, char* name)
{
int i=0;
for (i = 0; i < con->sz; i++)
{
if (strcmp(con->ContactMembers[i].Name, name) == 0)
{
return i;
}
}
return -1;
}
//大小排序(qsort实现)
int Sort(const void* a, const void* b)
{
ContactMember* A = (ContactMember*)a;
ContactMember* B = (ContactMember*)b;
return memcmp(B, A, sizeof(ContactMember));
}
void AddContactToFile(ContactMember newContact, const char* filename)
{
FILE* pf = fopen(filename, "a");
if (pf == NULL)
{
perror("Open file for append failed");
return;
}
fprintf(pf, "%s %s %d %s %s %d\n", newContact.Name, newContact.Sex,
newContact.Age, newContact.Address, newContact.Number, newContact.Snumber);
fclose(pf);
}
void DeleteContact(Contact* con)
{
char name[NAME_MAX];
printf("请输入要删除的联系人姓名:");
scanf("%s", name);
int i, j;
for (i = 0; i < con->sz; i++)
{
if (strcmp(con->ContactMembers[i].Name, name) == 0)
{
// 找到后将后面的元素依次往前移覆盖要删除的元素
for (j = i; j < con->sz - 1; j++)
{
strcpy(con->ContactMembers[j].Name, con->ContactMembers[j + 1].Name);
strcpy(con->ContactMembers[j].Sex, con->ContactMembers[j + 1].Sex);
con->ContactMembers[j].Age = con->ContactMembers[j + 1].Age;
strcpy(con->ContactMembers[j].Address, con->ContactMembers[j + 1].Address);
strcpy(con->ContactMembers[j].Number, con->ContactMembers[j + 1].Number);
con->ContactMembers[j].Snumber=con->ContactMembers[j + 1].Snumber;
}
con->sz--;
printf("联系人删除成功!\n");
return;
}
}
printf("未找到该联系人,无法删除!\n");
}
void WriteContactToFile(Contact* con, const char* filename)
{
assert(con);
assert(filename);
int i=0;
FILE* pf = fopen(filename, "w");
if (pf == NULL)
{
perror("Open file failed");
return;
}
for (i = 0; i < con->sz; i++)
{
fprintf(pf, "%s %s %d %s %s %d\n", con->ContactMembers[i].Name,
con->ContactMembers[i].Sex,
con->ContactMembers[i].Age,
con->ContactMembers[i].Address,
con->ContactMembers[i].Number,
con->ContactMembers[i].Snumber);
}
fclose(pf);
}
void SearchContact(Contact* con)
{
assert(con);
char name[NAME_MAX];
printf("请输入要查询的人:");
scanf("%s", name);
int ret = FindName(con, name);
if (ret == -1)
{
printf("查询目标不存在!\n");
return;
}
printf("%-20s\t%-5s\t%s\t%-30s\t%-12s\t%-12s\n", "姓名", "性别", "年龄", "地址", "号码", "学号");
printf("%-20s\t%-5s\t%d\t%-30s\t%-12s\t%-12d\n", con->ContactMembers[ret].Name,
con->ContactMembers[ret].Sex,
con->ContactMembers[ret].Age,
con->ContactMembers[ret].Address,
con->ContactMembers[ret].Number,
con->ContactMembers[ret].Snumber);
}
void ModifyContact(Contact* con, const char* filename)
{
char name[NAME_MAX];
printf("请输入要修改的联系人姓名:");
scanf("%s", name);
getchar(); // 处理输入缓冲区残留字符
int i;
for (i = 0; i < con->sz; i++)
{
if (strcmp(con->ContactMembers[i].Name, name) == 0)
{
printf("请输入新的性别:");
fgets(con->ContactMembers[i].Sex, 5, stdin);
// 去除fgets读入字符串末尾的换行符
if (con->ContactMembers[i].Sex[strlen(con->ContactMembers[i].Sex) - 1] == '\n')
{
con->ContactMembers[i].Sex[strlen(con->ContactMembers[i].Sex) - 1] = '\0';
}
printf("请输入新的年龄:");
scanf("%d", &con->ContactMembers[i].Age);
getchar(); // 处理输入缓冲区残留字符
printf("请输入新的地址:");
fgets(con->ContactMembers[i].Address, ADDR_MAX, stdin);
if (con->ContactMembers[i].Address[strlen(con->ContactMembers[i].Address) - 1] == '\n')
{
con->ContactMembers[i].Address[strlen(con->ContactMembers[i].Address) - 1] = '\0';
}
printf("请输入新的号码:");
fgets(con->ContactMembers[i].Number, TELE_MAX, stdin);
if (con->ContactMembers[i].Number[strlen(con->ContactMembers[i].Number) - 1] == '\n')
{
con->ContactMembers[i].Number[strlen(con->ContactMembers[i].Number) - 1] = '\0';
}
printf("请输入新的学号:");
scanf("%d", &con->ContactMembers[i].Snumber);
getchar(); // 处理输入缓冲区残留字符
printf("联系人信息修改成功!\n");
WriteContactToFile(con, filename); // 将修改后的信息写回文件
return;
}
}
printf("未找到该联系人,无法修改!\n");
}
void ReadContactFromFile(Contact* con, const char* filename)
{
assert(con);
assert(filename);
FILE* pf = fopen(filename, "r");
if (pf == NULL)
{
perror("Open file failed");
return;
}
con->sz = 0;
while (!feof(pf))
{
if (fscanf(pf, "%s %s %d %s %s %d", con->ContactMembers[con->sz].Name,
con->ContactMembers[con->sz].Sex,
&(con->ContactMembers[con->sz].Age),
con->ContactMembers[con->sz].Address,
con->ContactMembers[con->sz].Number,
&(con->ContactMembers[con->sz].Snumber)) == 6)
{
con->sz++;
}
}
fclose(pf);
}
void ReadContact(Contact* con)
{
int i=0;
assert(con);
printf("%-20s\t%-5s\t%s\t%-30s\t%-12s\t%-12s\n", "姓名", "性别", "年龄", "地址", "号码", "学号");
for (i = 0; i < con->sz; i++)
{
printf("%-20s\t%-5s\t%d\t%-30s\t%-12s\t%-12d\n", con->ContactMembers[i].Name,
con->ContactMembers[i].Sex,
con->ContactMembers[i].Age,
con->ContactMembers[i].Address,
con->ContactMembers[i].Number,
con->ContactMembers[i].Snumber);
}
}
void ClearContacts(Contact* con, const char* filename)
{
con->sz = 0; // 将通讯录中有效联系人数量置为0
FILE* pf = fopen(filename, "w"); // 以写入方式打开文件,会清空原有内容
if (pf == NULL)
{
perror("Open file failed");
return;
}
fclose(pf);
printf("联系人已全部清空!\n");
}
void SortContacts1(Contact* con)
{
int i = 0;
int j = 0;
for (i = 0; i < con->sz - 1; i++)
{
for (j = 0; j < con->sz - i - 1; j++)
{
if (strcmp(con->ContactMembers[j].Name, con->ContactMembers[j + 1].Name) > 0)
{
// 交换两个联系人结构体的所有成员信息
ContactMember temp = con->ContactMembers[j];
con->ContactMembers[j] = con->ContactMembers[j + 1];
con->ContactMembers[j + 1] = temp;
}
}
}
// 排序后重新写入文件,保持文件中联系人信息也是排序后的状态
const char* filename = "contacts.txt";
WriteContactToFile(con, filename);
printf("排序完成!\n");
}
void SortContactsBySnumber1(Contact* con)
{
int i=0,j=0;
for ( i = 0; i < con->sz - 1; i++)
{
for (j = 0; j < con->sz - i - 1; j++)
{
if (con->ContactMembers[j].Snumber>con->ContactMembers[j + 1].Snumber)
{
// 交换两个联系人结构体的所有成员信息
ContactMember temp = con->ContactMembers[j];
con->ContactMembers[j] = con->ContactMembers[j + 1];
con->ContactMembers[j + 1] = temp;
}
}
}
// 排序后重新写入文件,保持文件中联系人信息也是排序后的状态
const char* filename = "contacts.txt";
WriteContactToFile(con, filename);
printf("排序完成!\n");
}
void SortContacts2(Contact* con)
{
int i = 0;
int j = 0;
for (i = 0; i < con->sz - 1; i++)
{
for (j = 0; j < con->sz - i - 1; j++)
{
if (strcmp(con->ContactMembers[j].Name, con->ContactMembers[j + 1].Name) < 0)
{
// 交换两个联系人结构体的所有成员信息
ContactMember temp = con->ContactMembers[j];
con->ContactMembers[j] = con->ContactMembers[j + 1];
con->ContactMembers[j + 1] = temp;
}
}
}
// 排序后重新写入文件,保持文件中联系人信息也是排序后的状态
const char* filename = "contacts.txt";
WriteContactToFile(con, filename);
printf("排序完成!\n");
}
void SortContactsBySnumber2(Contact* con)
{
int i=0,j=0;
for ( i = 0; i < con->sz - 1; i++)
{
for (j = 0; j < con->sz - i - 1; j++)
{
if (con->ContactMembers[j].Snumber<con->ContactMembers[j + 1].Snumber)
{
// 交换两个联系人结构体的所有成员信息
ContactMember temp = con->ContactMembers[j];
con->ContactMembers[j] = con->ContactMembers[j + 1];
con->ContactMembers[j + 1] = temp;
}
}
}
// 排序后重新写入文件,保持文件中联系人信息也是排序后的状态
const char* filename = "contacts.txt";
WriteContactToFile(con, filename);
printf("排序完成!\n");
}
int login(Person *p) {
char input_name[20];
char input_password[20];
printf("请输入用户名: ");
scanf("%s", input_name);
printf("请输入密码: ");
scanf("%s", input_password);
if (strcmp(input_name, ADMIN_NAME) == 0 && strcmp(input_password, ADMIN_PASSWORD) == 0) {
p->user_type = 1;
strcpy(p->info.admin.admin_name, ADMIN_NAME);
strcpy(p->info.admin.admin_password, ADMIN_PASSWORD);
return 1;
} else if (strcmp(input_name, USER_NAME) == 0 && strcmp(input_password, USER_PASSWORD) == 0) {
p->user_type = 0;
strcpy(p->info.user.user_name, USER_NAME);
strcpy(p->info.user.user_password, USER_PASSWORD);
return 0;
}
return -1;
}
void loadContacts(Contact* con)
{
FILE *file; // 声明一个FILE指针用于指向文件
// 打开文件,"r"表示以只读方式打开
file = fopen("contacts.txt", "r");
int i=0;
if (file == NULL) {
// 如果文件打开失败,打印错误信息并退出程序
perror("Error opening file");
return ;
}
for (i = 0; i < con->sz; i++)
{
fscanf(file,"%-20s\t%-5s\t%d\t%-30s\t%-12s\t%-12d\n", con->ContactMembers[i].Name,
con->ContactMembers[i].Sex,
con->ContactMembers[i].Age,
con->ContactMembers[i].Address,
con->ContactMembers[i].Number,
con->ContactMembers[i].Snumber);
}
fclose(file);
}
void saveContacts(Contact* con)
{
FILE *file; // 声明一个FILE指针用于指向文件
// 打开文件,"w"表示以只写方式打开
file = fopen("contacts.txt", "w");
if (file == NULL) {
// 如果文件打开失败,打印错误信息并退出程序
perror("Error opening file");
return ;
}
int i = 0;
for (i = 0; i < con->sz; i++)
{
fprintf(file,"%-20s\t%-5s\t%d\t%-30s\t%-12s\t%-12d\n", con->ContactMembers[i].Name,
con->ContactMembers[i].Sex,
con->ContactMembers[i].Age,
con->ContactMembers[i].Address,
con->ContactMembers[i].Number,
con->ContactMembers[i].Snumber);
}
fclose(file);
}
Loading…
Cancel
Save