实训问题1:输入的电话号码与输出的不一样!
是不是格式错了!
#include <stdio.h>
#include <fcntl.h>
#include <io.h>
#define MAX 3
struct day
{
int month;
int day1;
};
struct mydata
{
char name[20];
struct day birthday;
float telnum;//电话号码在结构体中定义为FLOAT够么?11位的手机号码!
};
void creat1();
void input();
void output();
int main()
{
int i;
printf("0.creat a new file.\n");
printf("1.input the data.\n");
printf("2.output the data.\n");
printf("please choose:");
scanf("%d",&i);
getchar();
switch (i)
{
case 0:creat1();break;
case 1:input();break;
case 2:output();break;
}
return 0;
}
void creat1()
{
int f1;
f1=open("C:\\list.bin",O_CREAT);
if(f1!=-1)
{
printf("Creat a new file success!\n");
close(f1);
}
else
printf("File creat Failure!!\n");
}
void input()
{
int f1;
char ch;
struct mydata a[MAX];
f1=open("C:\\3.bin",O_APPEND|O_WRONLY|O_BINARY);//这边用2进制文档和无缓冲的文件OK不?
if((f1!=-1))
{
do
{
printf("input the name:");
gets(a[MAX].name);
printf("input the birthday(day):");
scanf(" %d",&a[MAX].birthday.day1);
printf("input the birthday(month):");
scanf(" %d",&a[MAX].birthday.month);
printf("input the telphonenumber:");
scanf(" %f",&a[MAX].telnum);//电话号码的输入!
write(f1,&a[MAX],sizeof(a[MAX]));
printf("still input (press n ou N to quie)?");
scanf(" %c",&ch);
getchar();
}while((ch!=78)&&(ch!=110));
close(f1);
printf("Data accepted!!\n");
}
else
printf("\nFile Opening Failure!!\n");
}
void output()
{
int f1;
struct mydata a[MAX];
printf("File 3.bin contens:\n");
f1=open("C:\\3.bin",O_RDONLY);
if((f1!=-1))
{
while(!eof(f1))
{
read(f1,&a[MAX],sizeof(a[MAX]));
printf("%s's birthday is %d.%d telphonenumber is %11.0f.\n",a[MAX].name,a[MAX].birthday.month,a[MAX].birthday.day1,a[MAX].telnum);//电话号码的输出!
}
close(f1);
}
else
printf("\nFile Opening Failure!!\n");
}
又做了点修改,加入0选项可以直接建立文件,可是我建立的文件没办法自己定义名称。这方面试过了,搞不过来!
还有这样的建立和插入有可能实现查找和删除的功能么?不要写了N多,最后有些功能无法实现。那就太冤啦~
为什么还没人来呢?星期1都上课了?
[此贴子已经被作者于2006-6-26 11:25:12编辑过]