Скачиваний:
23
Добавлен:
02.05.2014
Размер:
6.77 Кб
Скачать
void dobav()
{
char buf[100];
int y=121,kol;
FILE *fp;
FILE *fout;
if((fout=fopen("Baza.txt","r"))==NULL)
{ puts("Error");
exit(1);
}
while (y==121)
{
if((fp=fopen("Baza.txt","a"))==NULL)
{ puts("Error");
exit(1);
}
kol=0;
while (fgetc(fout)!=EOF) kol++;
fseek(fout,0,SEEK_SET);
clrscr();
printf("\nEnter City ");
scanf("%s",&buf);
if (kol!=0) fprintf(fp,"\n%s\n",&buf);
else fprintf(fp,"%s\n",&buf);
stpcpy(buf,"\0");
printf("Enter Area ");
scanf("%s",&buf);
fprintf(fp,"%s\n",&buf);
stpcpy(buf,"\0");
printf("Enter The Population ");
scanf("%s",&buf);
fprintf(fp,"%s\n",&buf);
stpcpy(buf,"\0");
printf("Enter Year ");
scanf("%s",&buf);
fprintf(fp,"%s",&buf);
stpcpy(buf,"\0");
printf("To continue (Y|N)");
y=getch();
fclose(fp);
}
fclose(fout);
clrscr();

}
void vivod()
{ char str[200],str2[200],str3[200],str4[200];
int i=1,n=1,kol=0;
FILE *fp2;
if((fp2=fopen("Baza.txt","r"))==NULL)
{ puts("Error");
exit(1);
}
clrscr();
printf("--------------------------------------------------------------------\n");
printf("|# | City | Area | Population | Year |\n");
printf("--------------------------------------------------------------------\n");
while (!feof(fp2))
{
switch(i)
{ case 1: fscanf(fp2,"%s",&str); break;
case 2: fscanf(fp2,"%s",&str2);break;
case 3: fscanf(fp2,"%s",&str3);break;
case 4: fscanf(fp2,"%s",&str4);break;
}
if (i==4)
{
kol++;
printf("|#%4d | %10s | %15s| %10s | %10s|\n",n/4,str,str2,str3,str4);
i=0;
strcat(str,"\0");
strcat(str2,"\0");
strcat(str3,"\0");
strcat(str4,"\0");
}
if ((kol==20)&&(!feof(fp2)))
{
kol=0;
printf("Press a key for continuation...");getch();
clrscr();
printf("--------------------------------------------------------------------\n");
printf("|# | City | Area | Population | Year |\n");
printf("--------------------------------------------------------------------\n");
}
i++;
n++;
}
fclose(fp2);
}
void poisk()
{ char str[200],str2[200],str3[200],str4[200],poisk[100];
int i=1;
FILE *fp3;
if((fp3=fopen("Baza.txt","r"))==NULL)
{ puts("Error");
exit(1);
}
vivod();
printf("\nEnter required city ");
scanf("%s",&poisk);
while(!feof(fp3))
{
switch(i)
{ case 1: fscanf(fp3,"%s",&str); break;
case 2: fscanf(fp3,"%s",&str2);break;
case 3: fscanf(fp3,"%s",&str3);break;
case 4: fscanf(fp3,"%s",&str4);break;
}
if (i==4)
{
if (strstr(str,poisk)!=NULL) printf("%10s %10s %10s %10s\n",str,str2,str3,str4);
i=0;
strcpy(str,"\0");
strcpy(str2,"\0");
strcpy(str3,"\0");
strcpy(str4,"\0");
}
i++;
}
fclose(fp3);
}
void del()
{
struct s
{
char gorod[200];
char obl[200];
char nas[200];
char god[200];
};
char str[200],str2[200],str3[200],str4[200];
int n,i,j;
FILE *fp6;
if((fp6=fopen("Baza.txt","r"))==NULL)
{ puts("Error");
exit(1);
}
clrscr();
vivod();
int kol=1,_kol=4;
while (!feof(fp6)) if(getc(fp6)==10) kol++;
s *ms = new s[kol/4];
fseek(fp6, 0, SEEK_SET);
i=0;
while(!feof(fp6))
{
fscanf(fp6,"%s",&str);
fscanf(fp6,"%s",&str2);
fscanf(fp6,"%s",&str3);
fscanf(fp6,"%s",&str4);
strcpy(ms[i].gorod,str);
strcpy(ms[i].obl,str2);
strcpy(ms[i].nas,str3);
strcpy(ms[i].god,str4);
i++;
}
printf("\nEnter numder of a line ");
scanf("%d",&n);
fclose(fp6);
if((fp6=fopen("Baza.txt","w"))==NULL)
{ puts("Error");
exit(1);
}
for(j=0;j<kol/4;j++,_kol+=4)
if (j+1!=n) fprintf(fp6,"%s\n%s\n%s\n%s\n",ms[j].gorod,ms[j].obl,ms[j].nas,ms[j].god);
fclose(fp6);
delete [] ms;
}
void dell_enter()
{
int kol_s=0,_kol_s=0;
char buf[100];
FILE *fp;
FILE *fp1;
if((fp=fopen("Baza.txt","r"))==NULL)
{ puts("Error");
exit(1);
}
if((fp1=fopen("Baza_buf.txt","w"))==NULL)
{ puts("Error");
exit(1);
}
while (!feof(fp)) { strcpy(buf,"\0");fscanf(fp,"%s",&buf); kol_s++;}
fseek(fp, 0, SEEK_SET);
if (atoi(buf)==0)
{
while (!feof(fp))
{
fscanf(fp,"%s",&buf);
if(_kol_s==kol_s-2) fprintf(fp1,"%s",buf);
if(_kol_s<kol_s-2) fprintf(fp1,"%s\n",buf);
_kol_s++;
}
fclose(fp);
fclose(fp1);
remove("Baza.txt");
rename("Baza_buf.txt","Baza.txt");
}
}
void sort()
{ struct s
{
char gorod[200];
char obl[200];
char nas[200];
char god[200];
};
char str[200],str2[200],str3[200],str4[200];
int n,i,j;
FILE *fp6;
if((fp6=fopen("Baza.txt","r"))==NULL)
{ puts("Error");
exit(1);
}
clrscr();
int kol=0,_kol=4;
while (!feof(fp6)) if(getc(fp6)==10) kol++;
s *ms = new s[1+kol/4];
fseek(fp6, 0, SEEK_SET);
i=0;
while(!feof(fp6))
{
fscanf(fp6,"%s",&str);
fscanf(fp6,"%s",&str2);
fscanf(fp6,"%s",&str3);
fscanf(fp6,"%s",&str4);
strcpy(ms[i].gorod,str);
strcpy(ms[i].obl,str2);
strcpy(ms[i].nas,str3);
strcpy(ms[i].god,str4);
i++;
}
s temp;
for(j=0;j<1+kol/4;j++)
for (i=0;i<1+kol/4;i++)
if (atoi(ms[i].god)<atoi(ms[j].god))
{
strcpy(temp.gorod,ms[i].gorod);
strcpy(temp.obl,ms[i].obl);
strcpy(temp.nas,ms[i].nas);
strcpy(temp.god,ms[i].god);

strcpy(ms[i].gorod,ms[j].gorod);
strcpy(ms[i].obl,ms[j].obl);
strcpy(ms[i].nas,ms[j].nas);
strcpy(ms[i].god,ms[j].god);

strcpy(ms[j].gorod,temp.gorod);
strcpy(ms[j].obl,temp.obl);
strcpy(ms[j].nas,temp.nas);
strcpy(ms[j].god,temp.god);
}
fclose(fp6);
if((fp6=fopen("Baza.txt","w"))==NULL)
{ puts("Error");
exit(1);
}
for(j=0;j<1+kol/4;j++,_kol+=4)
{
if (_kol==kol+1) fprintf(fp6,"%s\n%s\n%s\n%s",ms[j].gorod,ms[j].obl,ms[j].nas,ms[j].god);
if (_kol!=kol+1) fprintf(fp6,"%s\n%s\n%s\n%s\n",ms[j].gorod,ms[j].obl,ms[j].nas,ms[j].god);
}
fclose(fp6);
vivod();
delete [] ms;
}