fprintf乱码问题!

z_lllllll 2017-12-03 06:21:40
#include<iostream>
using namespace std;
typedef struct w
{
int no;
char name[100];
int depno;
int salary;
struct w* next;
} worker;

void input(worker*&L)
{
worker*p;
p=(worker*)malloc(sizeof(worker));
cout<<"请按顺序分别输入职工编号,姓名,部门,工资(两信息间用空格分离)"<<endl;
cin>>p->no>>p->name>>p->depno>>p->salary;
p->next=L->next;
L->next=p;
}

void show(worker*L)
{
worker*p=L->next;
cout<<"工号 姓名 部门 工资"<<endl;
for(p=L->next;p!=NULL;p=p->next)
cout<<p->no<<" "<<p->name<<" "<<p->depno<<" "<<p->salary<<endl;
}

void num_sort(worker*&pno)
{
worker*p,*q,*s;
if(pno->next==NULL)
{
cout<<"链表为空"<<endl;
return;
}
q=pno->next->next;
pno->next->next=NULL;
while(q!=NULL)
{
p=pno;
while(p->next!=NULL && q->no >= p->next->no)
p=p->next;
s=q->next;
q->next=p->next;
p->next=q;
q=s;
}
cout<<"已排序完毕"<<endl;
}
void show_pno(worker*&pno)
{
worker*p=pno->next;
cout<<"工号 姓名 部门 工资"<<endl;
for(p=pno->next;p!=NULL;p=p->next)
cout<<p->no<<" "<<p->name<<" "<<p->depno<<" "<<p->salary<<endl;
}

void dep_sort(worker*&depno)
{
worker*p,*q,*s;
if(depno->next==NULL)
{
cout<<"链表为空"<<endl;
return;
}
q=depno->next->next;
depno->next->next=NULL;
while(q!=NULL)
{
p=depno;
while(p->next!=NULL && q->depno >= p->next->depno)
p=p->next;
s=q->next;
q->next=p->next;
p->next=q;
q=s;
}
cout<<"已排序完毕"<<endl;
}
void show_depno(worker*depno)
{
worker*p=depno->next;
cout<<"工号 姓名 部门 工资"<<endl;
for(p=depno->next;p!=NULL;p=p->next)
cout<<p->no<<" "<<p->name<<" "<<p->depno<<" "<<p->salary<<endl;
}
void sal_sort(worker*&psalary)
{
worker*p,*q,*s;
if(psalary->next==NULL)
{
cout<<"链表为空"<<endl;
return;
}
q=psalary->next->next;
psalary->next->next=NULL;
while(q!=NULL)
{
p=psalary;
while(p->next!=NULL && q->salary >= p->next->salary)
p=p->next;
s=q->next;
q->next=p->next;
p->next=q;
q=s;
}
cout<<"链表为空"<<endl;
}
void show_psalary(worker*psalary)
{
worker*p=psalary->next;
cout<<"工号 姓名 部门 工资"<<endl;
for(p=psalary->next;p!=NULL;p=p->next)
cout<<p->no<<" "<<p->name<<" "<<p->depno<<" "<<p->salary<<endl;
}
void save(worker*&L)
{
FILE *fp;
worker*p;p=L;
if((fp=fopen("C:\\11\\worker.txt","w"))==NULL)
cout<<"error"<<endl;
for(p;p!=NULL;p=p->next)
{
fprintf(fp,"%x,%x,%x,%x",p->no,p->name,p->depno,p->salary);
}
fclose(fp);
exit(0);
}



void destroy(worker*&L)
{
worker*p=L->next,*q;
while(p!=NULL)
{
q=p;
p=p->next;
free(q);
}
L->next=NULL;
cout<<"已删除全部职工记录"<<endl;
}
void menu()
{


cout<<" 菜单 "<<endl;
cout<<" 1:输入一个职工记录 "<<endl;
cout<<" 2:显示所有职工记录 "<<endl;
cout<<" 3:按工号升序排序 "<<endl;
cout<<" 4:按工号升序排序输出 "<<endl;
cout<<" 5:按部门升序排序 "<<endl;
cout<<" 6:按部门升序排序输出 "<<endl;
cout<<" 7:按工资升序排序 "<<endl;
cout<<" 8:按工资升序排序输出 "<<endl;
cout<<" 9:删除全部职工记录 "<<endl;
cout<<" 10:将单链表中的所有节点存储到职工文件中并退出 "<<endl;
}

void main()
{
int option,T=0;
menu();
worker*L=(worker*)malloc(sizeof(worker));
worker*pno=(worker*)malloc(sizeof(worker));
pno=L;
worker *depno=(worker*)malloc(sizeof(worker));
depno=L;
worker*psalary=(worker*)malloc(sizeof(worker));
psalary=L;
L->next=NULL;
for(;;)
{
cout<<"请输入选项序号: ";
cin>>option;
switch(option)
{
case 1:
input(L);
break;
case 2:
show(L);
break;
case 3:
num_sort(pno);
break;
case 4:
show_pno(pno);
case 5:
dep_sort(depno);
break;
case 6:
show_depno(depno);
break;
case 7:
sal_sort(psalary);
break;
case 8:
show_psalary(psalary);
break;
case 9:
destroy(L);
break;
case 10:
save(L);
break;
default:
cout<<"选项序号输入错误,请重新输入!"<<endl;
}
if(T)
break;
}
cout<<"程序运行结束!"<<endl;
}

save函数里面的fprintf,打开文本后是乱码
...全文
251 4 点赞 打赏 收藏 举报
写回复
4 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
赵4老师 2017-12-05
不要把 fopen("...","...");fscanf,fprintf,fgets,fgetc,fclose //读时把\r\n替换成\n,写时把\n替换成\r\n;读到\x1a就设置EOF;读写的内容当字符看待 和 fopen("...","...b");fseek,ftell,fread,fwrite,fgetc,fclose //不作以上替换,遇到\x1a仍继续读;读写的内容当字节看待 弄混了
  • 打赏
  • 举报
回复
fprintf(fp,"%d,%s,%d,%d",p->no,p->name,p->depno,p->salary);
  • 打赏
  • 举报
回复
destory27 2017-12-04
你输入到文件中, 一种是二进制文件,一种是文本文件 只有字符能看
  • 打赏
  • 举报
回复
自信男孩 2017-12-04
for(;p!=NULL;p=p->next)
    {
        fprintf(fp,"%x,%s,%x,%x",p->no, p->name,p->depno,p->salary);
    }
name是字符数组,应该是用%s输出。用%x输出自然会有问题;
  • 打赏
  • 举报
回复
相关推荐
发帖
新手乐园
加入

3.3w+

社区成员

C/C++ 新手乐园
申请成为版主
帖子事件
创建了帖子
2017-12-03 06:21
社区公告
暂无公告