怎么新建和使用旧文件?还要可以在整个程序中使用它
我的程序是这样的:#include "stdio.h"
#include "conio.h"
#include "string.h"
#include "stdlib.h"
#define N 100
struct people
{
char num[20];
char name[20];
float P1;
float P2;
float all;
}people[N];
void show_version()
{
printf("\n\n\n\n\n");
printf("\t\t\t The version is V1.00");
printf("\n\n");
printf("\t\t Please press any key to continue...");
getch();
}
printf_face()
{
printf("\n num name P1 P2 all \n");
}
printf_one(int i)
{
printf("%10s%10s%10.2f%10.2f%10.2f\n",people[i].num,people[i].name,people[i].P1,people[i].P2,people[i].all);
}
printf_back()
{
int display();
void menu();
int w;
printf("\n\n\tSuccessful.^_^\n\n");
printf("What do you want to do?\n\n 1)Display all now\t2).Back:[ ]\b\b");
scanf("%d",&w);
if(w==1)
display();
else menu();
}
save(int n)
{
FILE *fp;
int i;
if((fp=fopen("Pro.txt","wb"))==NULL)
{printf("\nCannot open file\n");
return NULL;
}
for(i=0;i<n;i++)
if(people[i].num!=NULL)
if(fwrite(&people[i],sizeof(struct people),1,fp)!=1)
printf("file write error\n");
fclose(fp);
}
no_input(int i,int n)
{
int j,k,w1;
do
{
w1=0;
for(j=0;people[i].num[j]!='\0';j++)
if(people[i].num[j]>'9')
{
puts("Input error! Only be made up of(0-9).Please reinput!\n");
w1=1;break;
}
if(w1!=1)
for(k=0;k<n;k++)
if(k!=i&&strcmp(people[k].num,people[i].num)==0)
{
puts("This record is exist.Please reinput!\n");
}
}
while(w1==1);
}
enter()
{
int input(int i);
int i,n;
printf("How many people(0-%d)?:",N-1);
scanf("%d",&n);
printf("\n Enter data now\n\n");
for(i=0;i<n;i++)
{
printf("\n Input %dth people record.\n",i+1);
input(i);
}
if(i!=0)
save(n);
printf_back();
}
load()
{
FILE *fp;
int i;
if((fp=fopen("Pro.txt","rb"))==NULL)
{printf("\nCannot open file\n");
getch();
return menu();
}
for(i=0;!feof(fp);i++)
fread(&people[i],sizeof(struct people),1,fp);
fclose(fp);
return(i-1);
}
input(int i)
{
no_input(i,i);
printf("num:");
scanf("%s",&people[i].num);
printf("name:");
scanf("%s",&people[i].name);
printf("P1:");
scanf("%f",&people[i].P1);
printf("P2:");
scanf("%f",&people[i].P2);
people[i].all=people[i].P1+people[i].P2;
return NULL;
}
modify_data(int i)
{
int c,w1;
do
{
puts("\nmodify by=>\n\n 1).num 2).name 3).P1 4).P2 ");
printf("Which you needed?:[ ]\b\b");
scanf("%d",&c);
if(c>4||c<1)
{
puts("\nChoice error! Please again!");
getch();
}
}while(c>8||c<1);
do
{
switch(c)
{
case 1:printf("num:");scanf("%s",&people[i].num);break;
case 2:printf("name:");scanf("%s",&people[i].name);break;
case 3:printf("P1:");scanf("%f",&people[i].P1);break;
case 4:printf("P2:");scanf("%f",&people[i].P2);break;
}
puts("\nNow:\n");
printf_face();
printf_one(i);
printf("\nAre you sure?\n\n\t 1).Sure 2)No and remodify 3).Back without save in this time [ ]\b\b");
scanf("%d",&w1);
}while(w1==2);
return(w1);
}
display()
{
int i,n;
n=load();
printf_face();
for(i=0;i<n;i++)
{
if((i!=0)&&(i%10==0))
{
printf("\nPass any key to continue...");
getch();
puts("\n");
}
printf_one(i);
}
printf("\tThere are %d record.\n",n);
printf("\nPass any key to back...");
getch();
return menu();
}
add()
{
int i,n,m,k;
FILE *fp;
n=load();
printf("How many people are you want to add(0-%d)?:",N-1-n);
scanf("%d",&m);
k=m+n;
for(i=n;i<k;i++)
{
printf("\nInput %dth people record.\n",i-n+1);
input(i);
}
if((fp=fopen("Pro.txt","ab"))==NULL)
{
printf("cannot open file\n");
}
for(i=n;i<k;i++)
if(fwrite(&people[i],sizeof(struct people),1,fp)!=1)
printf("file write error\n");
fclose(fp);
printf_back();
}
del()
{
int i,j,n,k,a,b='y';
struct people p;
do
{
n=load();
system("cls");
for(i=0;i<n;i++)
printf_one(i);
printf("\n\nEnter num that you want to del! num:");
scanf("%s",&p.num);
printf_face();
for(i=0;i<n;i++)
{
if(strcmp(p.num,people[i].num)==0)
{
k=i;
printf_one(k);break;
}
}
printf("Are you sure?(y/n)");
scanf("%c",&j);
getch();
if(j=='y'||'Y')
{
for(a=k;a<n;a++)
{
strcpy(people[a].num,people[a+1].num);
strcpy(people[a].name,people[a+1].name);
people[a].P1=people[a+1].P1;
people[a].P2=people[a+1].P2;
people[a].all=people[a+1].all;
}
save(n-1);
printf("\nSuccessful!^_^");
getch();
}
printf("\n\nDelete another?(y/n)");
scanf("%c",&b);
}while(b=='y');
menu();
}
search()
{
int i,n,k,w1=1,w2,w3,w4,m,a;
struct people p;
do
{
n=load();
printf("\n\nWhich way do you want to choose? \n\t1).By name 2).By num [ ]\b\b");
scanf("%d",&m);
switch(m)
{
case 1:
do
{ k=-1;
printf("\n\nEnter xeuli that you want to search! xueli.");
scanf("%s",&p.name);
printf_face();
for(i=0;i<n;i++)
if(strcmp(p.name,people[i].name)==0)
{
k=i;
printf_one(k);break;
}
if(k==-1)
{
printf("\n\nNO exist!please");
printf("\n\nAre you again?\n\t1).again 2).NO and back [ ]\b\b");
scanf("%d",&w1);
if(w1==2)
printf_back();
}
}
while(k==-1&&w1==1);break;
case 2:
do
{
k=-1;
printf("\n\nEnter num that you want to search! num.");
scanf("%s",&p.num);
printf_face();
for(i=0;i<n;i++)
if(strcmp(p.num,people[i].num)==0)
{
k=i;
printf_one(k);break;
}
if(k==-1)
{
printf("\n\nNO exist!please");
printf("\n\nAre you again?\n\t1).again 2).NO and back [ ]\b\b");
scanf("%d",&w1);
if(w1==2)
printf_back();
}
}
while(k==-1&&w1==1);break;
}
w4=0;w3=0;
if(k!=-1)
{
printf("\n\nWhat do you want to do?\n\t 1).Search 2).Modify 3).Delete 4).Back menu [ ]\b\b");
scanf("%d",&w2);
switch(w2)
{
case 2:w3=modify_data(k);break;
case 3:{printf("\nAre you sure?\n\t 1).Sure 2).No and back [ ]\b\b");
scanf("%d",&w4);
if(w4==1)
for(a=k;a<n;a++)
{
strcpy(people[a].num,people[a+1].num);
strcpy(people[a].name,people[a+1].name);
people[a].P1=people[a+1].P1;
people[a].P2=people[a+1].P2;
people[a].all=people[a+1].all;
}
save(n-1);
break;
}
}
if(w3==1)
{
save(n);
printf("\n\nSuccessful.^_^.");
printf("\n\nWhant do you want to do? \n\t 1).Search another 2).Back [ ]\b\b" );
scanf("%d",&w2);
}
}
}
while(w2==1);
menu();
}
modify()
{
struct people p;
int i,n,k,w0=1,w1,w2=0;
n=load();
do
{
k=-1;
printf_face();
for(i=0;i<n;i++)
{
if((i!=0)&&(i%10==0))
{
printf("\n\nRemember the num which needed modify.\npass any key to contiune ...");
getch();
puts("\n\n");
}
printf_one(i);
}
do
{
printf("\n\nEnter the num that you want to modify! num:");
scanf("%s",&p.num);
for(i=0;i<n;i++)
if(strcmp(p.num,people[i].num)==0)
{
k=i;
p=people[i];
}
if(k==-1)printf("\n\nNO exist!please again");
}
while(k==-1);
printf_face();
printf_one(k);
w1=modify_data(k);
if(w1==1)
{printf("\nSuccessful ^_^.\n\nAre you modify another ?\n\n\t 1).Yes 2).Back with save\t[ ]\b\b");
scanf("%d",&w0);
w2=1;
}
else
{
w0=0;
if(w2==1)
people[k]=p;
}
if(w0!=1&&w2==1)
save(n);
}
while(w0==1);
menu();
}
P1()
{
struct people p;
int i,j,n;
n=load();
for(j=0;j<n-1;j++)
for(i=0;i<n-j-1;i++)
if(people[i].P1<people[i+1].P1)
{
p=people[i];
people[i]=people[i+1];
people[i+1]=p;
}
save(n);
menu();
}
P2()
{
struct people p;
int i,j,n;
n=load();
for(j=0;j<n-1;j++)
for(i=0;i<n-j-1;i++)
if(people[i].P2<people[i+1].P2)
{
p=people[i];
people[i]=people[i+1];
people[i+1]=p;
}
save(n);
menu();
}
all()
{
struct people p;
int i,j,n;
n=load();
for(j=0;j<n-1;j++)
for(i=0;i<n-j-1;i++)
if(people[i].all<people[i+1].all)
{
p=people[i];
people[i]=people[i+1];
people[i+1]=p;
}
save(n);
menu();
}
compositor()
{
int t;
printf("Which way do you want? BY=>1).P1 2).P2 3).all 4).No and back[ ]\b\b");
scanf("%d",&t);
switch(t)
{
case 1:P1();break;
case 2:P2();break;
case 3:all();break;
case 4:printf_back();break;
}
}
help()
{
printf("NO");
getch();
menu();
}
void menu()
{
int n,w1;
system("cls");
do
{
printf("\t\t**********************************************\n");
printf("\t\t*** 1 display record ************\n");
printf("\t\t*** 2 add record ************\n");
printf("\t\t*** 3 del record ************\n");
printf("\t\t*** 4 search record ************\n");
printf("\t\t*** 5 modify record ************\n");
printf("\t\t*** 6 sort and compositor ************\n");
printf("\t\t*** 7 help ************\n");
printf("\t\t*** 8 save as... ************\n");
printf("\t\t*** 9 exit ************\n");
printf("\t\t**********************************************\n");
printf("\n");
printf("Choose your number(1-9):[ ]\b\b");
scanf("%d",&n);
if(n<1||n>9)
w1=1;
else w1=0;
}while(w1==1);
switch(n)
{
case 1:display();break;
case 2:add();break;
case 3:del();break;
case 4:search();break;
case 5:modify();break;
case 6:compositor();break;
case 7:help();break;
case 8:save(n);break;
case 9:exit(0);
}
}
main()
{
show_version();
menu();
}
现在我不要指定的Pro.txt文件,我要可以另外输入一个文件的路径来打开那个文件,如果没有的话还要可以新建一个文件来使用,不知道要搞什么.....
先谢谢各位了...