行。。。
反正都是为了进步。。。
你帮我看看我现在的2个错误好不。一个是静态变量会变为0。
还有个就是那句代码。错误信息。本来全部整合好了。可是就那边又动不起来了!
我用CF的构成工程做的。有几个版块列。
首先是这个。错误在这块里~可是这块单独运行是正常的(除了静态外部变量会变之外)
#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;
char telnum[13];
}a[MAX];
static int g=0;
void input(struct mydata a[],int g);
void output(struct mydata a[],int g);
void search(struct mydata a[],int g);
void array(struct mydata a[],int g);
int main2(void)
{
int i;
printf("1.input the data.\n");
printf("2.output the data.\n");
printf("3.search the data.\n");
printf("4.array the data.\n");
printf("Please selete:");
scanf("%d",&i);
getchar();
switch (i)
{
case 1:input(a,g);break;
case 2:output(a,g);break;
case 3:search(a,g);break;
case 4:array(a,g);break;
}
return 0;
}
void input(struct mydata a[],int g) //输入data
{ //CF中的错误就在这一行。。。
char ch;
printf("input the data:");
do
{
printf("\ninput the name:");
gets(a[g].name);
printf("\ninput the birthday(mm-dd):");
scanf("%d-%d",&a[g].birthday.month,&a[g].birthday.day1);
getchar();
printf("\ninput the telphone number:");
gets(a[g].telnum);
printf("countuie?(press n or N to quie)");
scanf(" %c",&ch);
getchar();
}while((ch!=78)&&(ch!=110)&&(++g<MAX));
g--;
}
void array(struct mydata a[],int g) //排列
{
int i,j;
struct mydata t;
for(i=0;i<g-1;i++) //让人名从a-z排列
{
for(j=1;j<g;j++)
{
if(strcmp(a[i].name,a[j].name)>0)
{t=a[i];a[i]=a[j];a[j]=t;}
}
}
}
void output(struct mydata a[],int g) //输出
{
int i;
for(i=0;i<=g-1;i++) //输出所有人物!
{
printf("%s's birthday is %d.%d telphone number is %s.\n",
a[i].name,a[i].birthday.month,a[i].birthday.day1,a[i].telnum);
}
}
void search(struct mydata a[],int g) //查找
{
char keyname[20];int i,flag=1;
struct mydata t;
printf("input the search name:"); //查找人物!
gets(keyname);
for(i=0;(i<=g-1)&&flag;i++)
{
if(strcmp(keyname,a[i].name)!=0);
else
{
printf("Find the data:%s's birthday is %d.%d telphone number is %s.\n",
a[i].name,a[i].birthday.month,a[i].birthday.day1,a[i].telnum);
flag=0;
}
}
if(flag)printf("Can't find the data!!\n");
}
void delete(struct mydata a[],int g) //删除
{
char keyname[20];int i,flag=1;
printf("input the search name:");
gets(keyname);
for(i=0;(i<=g-1)&&flag;i++)
{
if(strcmp(keyname,a[i].name)!=0);
else
{
a[MAX]=a[i];g-=1;
printf("Find the data:%s's birthday is %d.%d telphone number is %s.\n",
a[i].name,a[i].birthday.month,a[i].birthday.day1,a[i].telnum);
flag=0;
}
}
if(flag)printf("Can't find the data!!\n");
}
这个是MAIN
#include <stdio.h>
#define MAX 3
struct day
{
int month;
int day1;
};
struct mydata
{
char name[20];
struct day birthday;
char telnum[20];
}a[MAX];
static int g=0;
void creat1();
void load(struct mydata a[],int g);
void save(struct mydata a[],int g);
int main2(void);
int main(void)
{
int i;
printf("0.creat a new file.\n");
printf("1.load the file.\n");
printf("2.save the file.\n");
printf("please select:");
scanf("%d",&i);
getchar();
switch (i)
{
case 0:creat1();break;
case 1:load(a,g);break;
case 2:save(a,g);break;
}
return 0;
}
这个是文件读取:
#include <stdio.h>
#define MAX 3
struct day
{
int month;
int day1;
};
struct mydata
{
char name[20];
struct day birthday;
char telnum[20];
}a[MAX];
void load(struct mydata a[],int g)
{
FILE *fp1;
int i;
if((fp1=fopen("tongxun.bin","rb"))==NULL)
{
printf("The file can not open!!!\n");
return;
}
for (i=0;i<g;i++)
if(fread(&a[i],sizeof(struct mydata),1,fp1)!=1)
{printf("file load error!\n");
return;
}
fclose(fp1);
}
这个是文件记录:
#include <stdio.h>
#define MAX 3
struct day
{
int month;
int day1;
};
struct mydata
{
char name[20];
struct day birthday;
char telnum[20];
}a[MAX];
void save(struct mydata a[],int g)
{
FILE *fp1;
int i,j;
struct mydata t;
for(i=0;i<g-1;i++) //让人名从a-z排列
{
for(j=1;j<g;j++)
{
if(strcmp(a[i].name,a[j].name)>0)
{t=a[i];a[i]=a[j];a[j]=t;}
}
}
if((fp1=fopen("tongxun.bin","wb"))==NULL)
{
printf("The file can not open!!!\n");
return;
}
for(i=0;i<g;i++)
if(fwrite(&a[i],sizeof(struct mydata),1,fp1)!=1)
{printf("file save error!\n");
return;
}
fclose(fp1);
}
建立文件的:
#include <stdio.h>
#include<stdlib.h>
#define MAX 3
void creat1()
{
FILE *fp1;
fp1=fopen("tongxun.txt","w+");
if((fp1!=NULL))
printf("File creat success!!!\n");
else
printf("File creat failure!!!\n");
}
可恶。CF出错。掉了点东西!