文件操作的奇怪?
来源:互联网 发布:傲视天地宝石进阶数据 编辑:程序博客网 时间:2024/05/17 04:32
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct student
{
char *name;
char *cellphone;
char *birthday;
}stu[45];
void init(struct student *s,int n)
{
int i;
for(i=0;i<n;i++)
{
(s+i)->name=(char *)malloc(10);
(s+i)->cellphone=(char *)malloc(11);
(s+i)->birthday=(char *)malloc(5);
}
}
void found1(void)
{
int i;
char *na=(char *)malloc(20);
printf("please input the name:");
gets(na);
for(i=0;i<45;i++)
if(0!=strcmp(stu[i].name,na))
continue;
else
{
printf("%s 's cellphone is :%s",na,stu[i].name);
printf("%s 's birthday is :%s",na,stu[i].birthday);
break;
}
if(45==i)
printf("No this classmate!/n");
}
void found2(void)
{
int i,j=0;
char *birth=(char *)malloc(5);
printf("please input the birthday like 07.07:/n");
gets(birth);
for (i=0;i<45;i++)
if(strcmp(stu[i].birthday,birth)!=0)
continue;
else
{
printf("%s's birthday is :%s",stu[i].name,birth);
j++;
}
if(0==j)
printf("Nobody!/n");
}
void found3(void)
{
int i;
char *cellph=(char *)malloc(11);
printf("please input the cellphone:/n");
gets(cellph);
for (i=0;i<45;i++)
if(strcmp(stu[i].cellphone,cellph)!=0)
continue;
else
{
printf("%s's cellphone is :%s",stu[i].name,cellph);
break;
}
if(i==45)
printf("Nobady/n");
}
void found4(void)
{
int i;
FILE *p;
struct student st[45];
char *cellph=(char *)malloc(11), *na=(char *)malloc(20);
printf("please input the name:/n");
gets(na);
printf("please input the cellphone:/n");
gets(cellph);
if((p=fopen("D://yy.dat","wb"))==NULL)
{
printf("open file error!/n");
exit(0);
}
for (i=0;i<45;i++)
if(strcmp(stu[i].name,na)!=0)
continue;
else if(i<45)
{
strcpy(stu[i].cellphone,cellph);
rewind(p);
for(i=0;i<45;i++)
fwrite(&stu[i],sizeof(struct student),1,p);
break;
}
else printf(" input data error!/n");
fclose(p);
}
int choose_interface(void)
{
int j;
printf("/n************************************/n");
printf("***input 1 to find somebody ******/n");
printf("***input 2 to find birthday ******/n");
printf("***input 3 to find cellphone ******/n");
printf("***input 4 to chenge cellphone******/n");
printf("***input 0 to exit ******/n");
printf("************************************/n");
printf("please choose the number:");
scanf("%d",&j);
getchar();
return j;
}
void main(void)
{
int i,j;
FILE *p;
init(stu,45);
if((p=fopen("D://yy.dat","rb"))==NULL)
{
printf("can't open file !/n");
exit(0);
}
for(i=0;i<45;i++)
fread(&stu[i],sizeof(struct student),1,p);
fclose(p);
for(i=0;i<45;i++)
{
if(i%5==0)
putchar('/n');
printf("%10s",stu[i].name);
}
j=choose_interface();
while(j)
{
switch(j)
{
case 1: found1();
break;
case 2: found2();
break;
case 3: found3();
break;
case 4: found4();
break;
default: exit(0);
}
j=choose_interface();
}
}
其中的文件D://yy.dat是用这个程序创建的
#include <stdio.h>
struct student
{
char *name;
char *cellphone;
char *birthday;
}stu[45]={
{"*****","******","****"},*****************//处于隐私我用*号代替
};
void main(void)
{
FILE *p;
int i;
p=fopen("D://yy.dat","wb");
for(i=0;i<45;i++)
fwrite(&stu[i],sizeof(struct student),1,p);
fclose(p);
printf("well done!!/n");
}
但最后输出全市乱码!
- 文件操作的奇怪?
- 奇怪的++操作
- 奇怪的Core文件
- 奇怪的cab_xxxx_x文件
- 移位操作的奇怪问题
- 奇怪的文件读写程序?
- 一个奇怪的隐藏文件
- 一个奇怪的PE文件
- Velocity加载文件的奇怪问题
- hadoop日志文件_奇怪的时间
- .aidl文件注释的奇怪问题
- ofstream写不进txt文件的奇怪问题
- linux 删除文件名比较奇怪的文件
- 解析xml文件遇到奇怪的问题
- 一个奇怪的问题--关于文件输出
- 解析xml文件遇到奇怪的问题
- 奇怪的浮点操作错误 不支持指标操作
- 奇怪的Mongo数据库更新操作变成了删除操作
- 想到什么就去做吧
- 我的笔记
- 学习真的是一个漫长的过程
- treeview的问题?
- 日记
- 文件操作的奇怪?
- 17.文件操作
- 感悟
- 快乐和痛苦
- 我学习正则表达式的笔记
- 一些好文章 收集 慢慢看
- 理想收入
- Chain
- 文件系统与DBMS系统的区别