关于C语言中fprintf乱码的问题

ypf_1227 2009-04-21 04:56:35

#include "stdlib.h"
#include "stdio.h"
#include "string.h"

void selecettype(FILE *file)
{

char type;
printf("please selecet type,i or c.");
scanf("%s",&type);


if(type=='i')
{
int num;
scanf("%d",&num);
fprintf(file,"%d",&num);
//fwrite(&num,4,1,file);
//不论用fprintf还是fwrite输入数字之后文件里显示的总是乱码
}
else if(type=='c')
{
char string;
scanf("%s",&string);
fprintf(file,"%s",&string); //fprintf输入后文件内容显示正确
//fwrite(&string,1,1,file); //fwrite输入后文件内容显示为乱码

}


}


void createtable(FILE *file)
{
int i,SIZE;
printf("please printf the size:");
scanf("%d",&SIZE);
for(i=0;i <SIZE;i++)
{
selecettype(file);
}


}


void insertdata(FILE *file)
{
char decide;
printf("whether keep writing?y or n");
scanf("%s",&decide);

if(decide=='y')
{

createtable(file);
insertdata(file);
}
else if (decide=='n')
{
printf("end printf.");
}


}


void main()
{
FILE *file;
file=fopen("E:\\createtable\\createtable\\data.txt","a");
insertdata(file);
fclose(file);

FILE *file1;
char chs;
file1=fopen("E:\\createtable\\createtable\\shu_ju_zi_dian.txt","a");
if(file1==NULL)
{
printf("cannot open this file\n");
}
printf("\nplease writing in shu_ju_zi_dian.txt.");
chs=getchar();
while(chs!='#')
{
fputc(chs,file1);
putchar(chs);
chs=getchar();
}
fclose(file1);
}
...全文
1440 8 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
changyuan551 2011-06-20
  • 打赏
  • 举报
回复
typedef struct node
{
char n[20];char ccnum[20];
char time[10];
struct node *next;
}NODE;
NODE *start;

class employee
{
private:
NODE *p;
NODE *q;
NODE *t;
}

void employee::save() //0:文件保存及退出!
{
FILE *file;
char name[50],n1[50];
cout<<"请输入文件名,此文件会保存在程序所在文件夹例如文件名:车次"<<endl;
cin>>n1;
sprintf(name,"%s.txt",n1);
file=fopen(name, "w");
p=q->next;
fprintf(file,"%s\t%s\t%s\n",p->n,p->ccnum,p->time);
p=p->next;
fclose(file);
}


各位大侠能帮忙看看这个么,出来的也是乱码啊=。=
子晞 2009-04-21
  • 打赏
  • 举报
回复

#include "stdlib.h"
#include "stdio.h"
#include "string.h"

void selecettype(FILE *file)
{

char type;
printf("please selecet type,i or c.");
scanf("%c",&type);
getchar();


if(type=='i')
{
int num;
scanf("%d",&num);
getchar();
fprintf(file,"%d",num);
//fwrite(&num,4,1,file);
//不论用fprintf还是fwrite输入数字之后文件里显示的总是乱码
}
else if(type=='c')
{
char string;
scanf("%c",&string);
getchar();
fprintf(file,"%c",string); //fprintf输入后文件内容显示正确
//fwrite(&string,1,1,file); //fwrite输入后文件内容显示为乱码

}


}


void createtable(FILE *file)
{
int i,SIZE;
printf("please printf the size:");
scanf("%d",&SIZE);
getchar();
for(i=0;i <SIZE;i++)
{
selecettype(file);
}


}


void insertdata(FILE *file)
{
char decide;
printf("whether keep writing?y or n");
scanf("%c",&decide);
getchar();

if(decide=='y')
{

createtable(file);
insertdata(file);
}
else if (decide=='n')
{
printf("end printf.");
}


}


int main()
{
FILE *file;
file=fopen("E:\\data.txt","a");
insertdata(file);
fclose(file);

FILE *file1;
char chs;
file1=fopen("E:\\dian.txt","a");
if(file1==NULL)
{
printf("cannot open this file\n");
}
printf("\nplease writing in dian.txt.");
chs=getchar();
while(chs!='#')
{
fputc(chs,file1);
putchar(chs);
chs=getchar();
}
fclose(file1);
return 0;
}

子晞 2009-04-21
  • 打赏
  • 举报
回复
怎么会出现段错误,这段程序
ypf_1227 2009-04-21
  • 打赏
  • 举报
回复
问题解决了 非常感谢hai040
ch_tei_hyou 2009-04-21
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 hai040 的回复:]
fprintf(file,"%d",&num); //多了个&

char string;
scanf("%s",&string); //%s char 会越界的
fprintf(file,"%s",&string);
[/Quote]

同意。fprintf(file,"%d",&num);//你输出了&num以后的直到\0的内容

hai040 2009-04-21
  • 打赏
  • 举报
回复
fprintf(file,"%d",&num); //多了个&

char string;
scanf("%s",&string); //%s char 会越界的
fprintf(file,"%s",&string);
ypf_1227 2009-04-21
  • 打赏
  • 举报
回复
没有啊
int的我输入的是阿拉伯数字
char的我输入的是英文字母
  • 打赏
  • 举报
回复
你有输入中文?

70,023

社区成员

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

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