南方小莫 2020-04-05 13:45 采纳率: 33.3%
浏览 1419
已采纳

c操作txt文件,写入数据时,在txt中变成了乱码

#include
#include
#include
struct friends_list
{
char name[10];
char age[11];
char telep[13];
char qq[12];
struct friends_list *next;
};

struct friends_list create_f_list(struct friends_list *p){
if(p!=NULL) { printf("通讯录已经存在\n
********\n\n"); }
else {
p=(struct friends_list )malloc(sizeof(struct friends_list));
if(p==NULL) { printf("创建通讯录失败\n
*******************\n");}
p->next=NULL;
printf(" 成功新建通讯录\n*****************\n");
}
return p;
}
void insert_friends_list(struct friends_list p){
struct friends_list *q;
char c;
if(p==NULL) {printf("通讯录不存,请先创建\n\n\n");}
else {
q=(struct friends_list
)malloc(sizeof(struct friends_list));

    printf("请输入姓名 \n");
    scanf("%s",q->name);
    printf("请输入年龄 \n");
    scanf("%s",&q->age);
    printf("请输入电话号码 \n");
    scanf("%s",q->telep);
    printf("请输入qq号码 \n");
    scanf("%s",q->qq);
    q->next=NULL;
    q->next=p->next;
    p->next=q;
    printf("*************************\n\n");

}

}
void display_frd_list(struct friends_list p,char name[10],int i){
struct friends_list *q;
int j;
q=p->next;
if(i==1){
j=1;
while(q!=NULL){
if(strcmp(name,q->name)==0){
printf("------姓名为%s的第%d个联系人----\n",q->name,j);
printf("name:%s\n",q->name);
printf("age:%s\n",q->age);
printf("telephone:%s\n",q->telep);
printf("qq:%s\n",q->qq);
j++;
printf("
*******\n\n");
}
q=q->next;
}
if(j==1) { printf("没有姓名为%s的联系人。\n",name);}
}else{
j=1;
while(q!=NULL){
printf("-----第%d个联系人-----\n",j);
printf("name:%s\n",q->name);
printf("age:%s\n",q->age);
printf("telephone:%s\n",q->telep);
printf("qq:%s\n",q->qq);
q=q->next;
j++;
printf("********************\n\n\n");
}
if(j==1) printf("通讯录中没有记录\n");
}

}

void query_frd_list(struct friends_list *p)
{
int choice;
char name[10];
if(p==NULL){

}
while(1){
    printf("查询选项:输1.按姓名查询联系人     输入2.查询所有联系人    0:退出\n");
    scanf("%d",&choice);
    if(choice==1){
        printf("请输入联系人的名字:");
        scanf("%s",name);
        display_frd_list(p,name,1); 
    }   
    else if(choice==2) display_frd_list(p,name,2);
    else if(choice==0) break;
    else printf("选项错误\n");
}

}
int del_frd_list(struct friends_list p){
char name[10];
struct friends_list *q,*r;
int j;
j=1;
if(p==NULL){
printf("请先创建通讯录\n
***********\n\n");
return 1;
}

printf("请输入要删除的联系人的姓名:");
scanf("%s",name);
r=p;
q=p->next;
while(q!=NULL){
if(strcmp(name,q->name)==0){
r->next=q->next;
free(q);
q=r->next;
//j++;
}
else{
r=q; q=q->next;
j++;
}
}
if(j==0) printf("通讯录中没有姓名为%s的联系人。\n",name);
else printf("通讯录共删除了姓名为%s的 第%d 条记录\n******************\n\n",name,j);
return 0;
}

void writes(struct friends_list *p){
FILE *fp;
printf("successfully");
fp=fopen("/home/h/code/aaa.txt","wb");
if(fp==NULL) {printf("fail to open");}
fprintf(fp,"%s",p->name);
fwrite(p,sizeof(struct friends_list),1,fp);
fclose(fp);

}
int main(void){
int choice;
struct friends_list *p;
do{
printf("输入--1:新建 2:插入 3:查询 4:删除 5:数据输入到文件 0:退出\n");
printf("请选择功能:");
scanf("%d",&choice);
switch(choice){
case 1: p=create_f_list(p);break;
case 2: insert_friends_list(p);break;
case 3: query_frd_list(p); break;
case 4: del_frd_list(p); break;
case 5: writes(p); break;

case 0: break;
default: printf("选项错误,重新选择!");
}

}while(choice!=0);
printf("谢谢使用!\n");
return 0;
}

  • 写回答

4条回答 默认 最新

  • 守得云开终见月明 2020-04-05 14:24
    关注

    你的头文件为什么不复制一下?还是漏了?

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

问题事件

  • 已采纳回答 10月28日

悬赏问题

  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效