啊哈磊_编程从这里起步

标题: 问题 [打印本页]

作者: 981013    时间: 2013-3-24 14:43
标题: 问题
最近编了个背单词用的程序
添加词库功能一切正常
[code=Css width=740px]#include <stdio.h>
#include <stdlib.h>
main()
{ struct word
{ char spell[36];
char pos[40];
char mean[100];
int put;
int check;
}word[100];
int n;
for(n=0;n<=1000;n++)
{
FILE *fp1;
fp1=fopen("词库.txt","r");
fread(&word[n],sizeof(struct word),1,fp1);
if(word[n].put)
continue;
else
break;
}
for(n=0;n<=1000;n++)
{
printf("请输入单词:\n");
scanf("%s",word[n].spell);
printf("请输入音标:\n");
scanf("%s",word[n].pos);
printf("请输入词义:\n");
scanf("%s",word[n].mean);
word[n].put=1;
word[n].check=0;

FILE *fp;
fp=fopen("词库.txt","a+");
fwrite(&word[n],sizeof(struct word),1,fp);
int a;
printf("是否结束?(1——是,2——否)\n");
scanf("%d",&a);
if(a-2)
exit(0);
}
system("pause");
return 0;
}
[/code]
但是输出时有问题。
[code=Css width=740px]#include <stdio.h>
#include <stdlib.h>
main()
{ struct word
{ char spell[36];
char pos[40];
char mean[100];
int put;
int check;
}word[100];
int n;
for(n=0;n<=1000;n++)
{
FILE *fp1;
fp1=fopen("词库.txt","r");
fread(&word[n],sizeof(struct word),1,fp1);
if(word[n].put==1)
continue;
else
break;
}
for(n=0;n<=1000;n++)
{
printf("%s",word[n].spell);
printf("%s",word[n].pos);
printf("%s",word[n].mean);
}
system("pause");
return 0;
}
[/code]
编译通过了,但输出时显示乱码
作者: 981013    时间: 2013-3-24 14:47
for(n=0;n<=1000;n++)
是1000

for(n=0;n<=1000;n++)
是100的原因是电脑太破,内存8够
作者: zynpc    时间: 2013-3-27 19:46
存单词的那个文件,你试试存几个,然后用写字板打开看看不是乱码,我觉得应该是存入单词的时候就有问题。
作者: 981013    时间: 2013-3-31 12:49
zynpc 发表于 2013-3-27 19:46
存单词的那个文件,你试试存几个,然后用写字板打开看看不是乱码,我觉得应该是存入单词的时候就有问题。

存入单词的时没有问题
作者: 981013    时间: 2013-3-31 12:52
用记事本看本来就该是乱码,fwrite和fread读写文件时是以2进制方式进行的
作者: 呵呵呵呵    时间: 2015-8-17 15:40
呵呵
{:soso_e176:}




欢迎光临 啊哈磊_编程从这里起步 (https://bbs.codeaha.com/) Powered by Discuz! X3.2