QQ网名大全

c语言共用体实现设计个人名片,包含姓名,职业,电话

展开全部/共用体实例 你看看行不
include
include
include
共用体
union businessCard
{
char name[20];存储姓名
char job[20];存储职业
char phoneNum[13];存储电话号码
};void main(void)
{
businessCard card;FILE*fp;if!(fp=fopen("businessCard.txt","a+")))
{
printf("文件打开失败\n");exit(0);}
else
{
printf(">输入姓名:");scanf("%s",card.name);fprintf(fp,"%s\n",card.name);printf(">输入职业:");scanf("%s",card.job);fprintf(fp,"%s\n",card.job);printf(">输入电话:");scanf("%s",card.phoneNum);fprintf(fp,"%s\n",card.phoneNum);}
fclose(fp);从已存储文件中读取名片
if!(fp=fopen("businessCard.txt","r")))
{
printf("文件打开失败…\n");exit(0);}
int i,j;i=1,j=1;char buf[20]=";while!feof(fp))
{
if(i=1|i-j*3=1)
{
fgets(card.name,20,fp);if(strcmp(card.name,buf)!0)
printf(">姓名:%s",card.name);}
if(i=2|i-j*3=2)
{
fgets(card.job,20,fp);printf(">职业:%s",card.job);}
if(i=3|i-j*3=3)
{
fgets(card.phoneNum,20,fp);printf(">电话:%s\n",card.phoneNum);strcpy(buf,card.phoneNum);j+;}
i+;}
fclose(fp);}
佚名
2024-12-22 23:52:08
最佳回答
类似问题(10)