Код:
void adding(application* app)
{
read(app);
struct telecast *base;
base=app->base_app;
FILE *fp;
fp=app->fp_app;
int number_str=app->number_str_app;
char hour_start[3],hour_finish[3],minute_start[3],minute_finish[3];
int k;
int i;
/*Выделение памяти ещё для одного объекта базы*/
number_str++;
base=(struct telecast *)realloc(base,number_str*sizeof(struct telecast));
/*Ввод ещё одного объекта базы данных*/
base[number_str-1].id=number_str;
printf("Vvedi day:");
scanf("%s",&base[number_str-1].day);
printf("Vvedi channel:");
scanf("%s",&base[number_str-1].channel);
/*Ввод начального времени*/
memset(hour_start,'\0',3);
memset(minute_start,'\0',3);
i=1;
while (i==1)
{
i=0;
printf("Vvedi time_telecast_start v foramte hh:mm :");
scanf("%s",&base[number_str-1].time_telecast_start);
if (strlen(base[number_str-1].time_telecast_start)==5)
{
if (base[number_str-1].time_telecast_start[2]==':')
{
if (isdigit(base[number_str-1].time_telecast_start[0])!=0 && isdigit(base[number_str-1].time_telecast_start[1])!=0 && isdigit(base[number_str-1].time_telecast_start[3])!=0 && isdigit(base[number_str-1].time_telecast_start[4])!=0 )
{
for (k=0;k<=1;k++)
{
hour_start[k]=base[number_str-1].time_telecast_start[k];
minute_start[k]=base[number_str-1].time_telecast_start[k+3];
}
if (atoi(hour_start)>23 || atoi(hour_start)<0 || atoi(minute_start)>59 || atoi(minute_start)<0)
{
printf("Ne pravil'no vvedeno start_time!\n");
i=1;
}
}
else
{
printf("Ne pravil'no vvedeno start_time!\n");
i=1;
}
}
else
{
printf("Ne pravil'no vvedeno start_time!\n");
i=1;
}
}
else
{
printf("Ne pravil'no vvedeno start_time!\n");
i=1;
}
}
/*Ввод конечного времени*/
memset(hour_finish,'\0',3);
memset(minute_finish,'\0',3);
i=1;
while (i==1)
{
i=0;
printf("Vvedi time_telecast_finish v foramte hh:mm :");
scanf("%s",&base[number_str-1].time_telecast_finish);
if (strlen(base[number_str-1].time_telecast_finish)==5)
{
if (base[number_str-1].time_telecast_finish[2]==':')
{
if (isdigit(base[number_str-1].time_telecast_finish[0])!=0 && isdigit(base[number_str-1].time_telecast_finish[1])!=0 && isdigit(base[number_str-1].time_telecast_finish[3])!=0 && isdigit(base[number_str-1].time_telecast_finish[4])!=0 )
{
for (k=0;k<=1;k++)
{
hour_finish[k]=base[number_str-1].time_telecast_finish[k];
minute_finish[k]=base[number_str-1].time_telecast_finish[k+3];
}
if (atoi(hour_finish)<=23 && atoi(hour_finish)>=0 && atoi(minute_finish)<=59 && atoi(minute_finish)>=0 && atoi(hour_finish)>=atoi(hour_start))
{
if (atoi(hour_finish)==atoi(hour_start) && atoi(minute_finish)==atoi(minute_start))
{
i=1;
}
}
else
{
i=1;
}
}
else
{
i=1;
}
}
else
{
i=1;
}
}
else
{
i=1;
}
}
printf("Vvedi genre:");
scanf("%s",&base[number_str-1].genre);
printf("Vvedi name_telecast:");
scanf("%s",&base[number_str-1].name_telecast);
/*Перемещение к концу потока*/
fseek(fp,0L,SEEK_END);
/*Добавление в файл строки*/
fprintf(fp,"%d;%s;%s;%s;%s;%s;%s;\n",base[number_str-1].id,base[number_str-1].day,base[number_str-1].channel,base[number_str-1].time_telecast_start,base[number_str-1].time_telecast_finish,base[number_str-1].genre,base[number_str-1].name_telecast);
free(base);
fclose(fp);
return;
}
Код:
void del(application *app)
{
read(app);
struct telecast *base;
base=app->base_app;
FILE *fp;
fp=app->fp_app;
int number_str=app->number_str_app;
char line[256];
int i;
int j=0;
FILE *fp1;
int rem;
struct telecast *rem1,temp;
/*Вывод строк из файла на экран*/
fseek(fp,0L,SEEK_SET);
while ((fgets(line,256,fp)!=NULL))
{
printf("%s\n",line);
}
printf("Vvedi nomer stroki dlya ee udaleniya ili 0 dlya otmenbI:");
scanf("%d",&rem);
if (rem==0)
{
free(base);
fclose(fp);
return;
}
for(i=0;i<number_str;i++)
{
if (base[i].id==rem)
{
/*Перезапись id, чтобы было всё по порядку*/
for(j=rem;j<number_str;j++)
{
base[j].id=j;
}
/*Перезапись массива, чтобы было всё по порядку*/
for(j=rem;j<number_str;j++)
{
base[j-1]=base[j];
}
number_str--;
printf("Zapis' uspeshno udalena!\n");
for(j=0;j<number_str;j++)
{
fprintf(fp,"%d;%s;%s;%s;%s;%s;\n",base[j].id,base[j].day,base[j].channel,base[j].time_telecast_start,base[j].time_telecast_finish,base[j].genre,base[j].name_telecast);
printf("%d;%s;%s;%s;%s;%s;\n",base[j].id,base[j].day,base[j].channel,base[j].time_telecast_start,base[j].time_telecast_finish,base[j].genre,base[j].name_telecast);
}
free(base);
fclose(fp);
return;
}
}
printf("Net takoy stroki!\n");
free(base);
fclose(fp);
return;
}
Код:
void read(application *app)
{
struct telecast *base;
char line[256];
char temp[7][256];
int i,k;
int j=0; //Количество элементов между двумя ';'(включая и ';') в массиве
char *semicolon,*semicolon_temp;
FILE *fp;
char hour_start[3],hour_finish[3],minute_start[3],minute_finish[3];
int number_str=0;
if ((fp = fopen("base.txt","a+")) == NULL)
{
perror("Error pri open/creat file!\n");
exit(0);
}
//Заполняем строки temp[i] символом '\0'
for (i=0; i<=6; i++)
{
memset(temp[i],'\0',256);
}
//Считывание записей из файла
while ((fgets(line,256,fp)!=NULL))
{
if (semicolon=strchr(line,';'))
{
semicolon_temp=line;
for (i=0;i<=6;i++)
{
j=semicolon-semicolon_temp+1;
semicolon_temp=semicolon;
if (i==0)
{
semicolon=semicolon-j+1;
}
if (i!=0)
{
semicolon=semicolon-j+2;
}
strncpy(temp[i],semicolon,semicolon_temp-semicolon);
if (i==0)
{
semicolon=semicolon+j;
}
if (i!=0)
{
semicolon=semicolon+j-1;
}
semicolon=strchr(semicolon,';');
//Проверка на кривость строк
if ((i!=6 && semicolon==NULL) || (i==6 && semicolon!=NULL))
{
break;
}
//Проверка времени
if (i==4)
{
if (strlen(temp[3])==5 && strlen(temp[4])==5)
{
if (temp[3][2]==':' && temp[4][2]==':')
{
if (isdigit(temp[3][0])!=0 && isdigit(temp[3][1])!=0 && isdigit(temp[3][3])!=0 && isdigit(temp[3][4])!=0 && isdigit(temp[4][0])!=0 && isdigit(temp[4][1])!=0 && isdigit(temp[4][3])!=0 && isdigit(temp[4][4])!=0)
{
//Заполняем строки hour,minute символом '\0'
memset(hour_start,'\0',3);
memset(hour_finish,'\0',3);
memset(minute_start,'\0',3);
memset(minute_finish,'\0',3);
for (k=0;k<=1;k++)
{
hour_start[k]=temp[3][k];
hour_finish[k]=temp[4][k];
minute_start[k]=temp[3][k+3];
minute_finish[k]=temp[4][k+3];
}
if (atoi(hour_start)>23 || atoi(hour_start)<0 || atoi(hour_finish)>23 || atoi(hour_finish)<0 || atoi(minute_start)>59 || atoi(minute_start)<0 || atoi(minute_finish)>59 || atoi(minute_finish)<0 || atoi(hour_start)>atoi(hour_finish))
{
break;
}
if (atoi(hour_start)==atoi(hour_finish) && atoi(minute_start)>=atoi(minute_finish))
{
break;
}
}
else
{
break;
}
}
else
{
break;
}
}
else
{
break;
}
}
if (i==6)
{
number_str++;
base=(struct telecast *)realloc(base,number_str*sizeof(struct telecast));
base[number_str-1].id=number_str;
strcpy(base[number_str-1].day,temp[1]);
strcpy(base[number_str-1].channel,temp[2]);
strcpy(base[number_str-1].time_telecast_start,temp[3]);
strcpy(base[number_str-1].time_telecast_finish,temp[4]);
strcpy(base[number_str-1].genre,temp[5]);
strcpy(base[number_str-1].name_telecast,temp[6]);
}
}
}
}
app->fp_app=fp;
app->base_app=base;
app->number_str_app=number_str;
return;
}