#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <malloc.h>
#include <string.h>
#include <windows.h>
int main()
{
FILE *TarifInfor;
size_t TarifCnt, stop = 0;
char opt;
while (stop == 0)
{
system("CLS");
printf("\tMENU\n");
printf("\n1 - Input new info\n");
printf("\n2 - Output info\n");
printf("\n3 - Search info\n");
printf("\n4 - Add new info\n");
printf("\n5 - Delete string\n");
printf("\n6 - Clear file\n");
printf("\nPush another button for EXIT\n");
printf("\nInput ");
opt = getch();
switch (opt)
{
case '1':
system("CLS");
printf("How many new consumers ?\n");
scanf("%d", &TarifCnt);
InputTarifInfor(TarifInfor, TarifCnt);
printf("\nPress any button to continue");
getch();
break;
case '2':
system("CLS");
TarifInfor = fopen("TarifInfor.txt", "r");
fseek(TarifInfor, 0, SEEK_END);
int pos = ftell(TarifInfor);
if (pos > 0)
{
rewind(TarifInfor);
fclose(TarifInfor);
OutputTarifInfor(TarifInfor, TarifCnt);
}
else
{
printf("File empty. Please input some info first");
fclose(TarifInfor);
}
printf("\nPress any button to continue");
getch();
break;
case '3':
system("CLS");
TarifInfor = fopen("TarifInfor.txt", "r");
fseek(TarifInfor, 0, SEEK_END);
pos = ftell(TarifInfor);
if (pos > 0)
{
rewind(TarifInfor);
fclose(TarifInfor);
SearchInfo(TarifInfor, TarifCnt);
}
else
{
printf("File empty. Please input some info first");
fclose(TarifInfor);
}
printf("\nPress any button to continue");
getch();
break;
case '4':
system("CLS");
printf("How many new consumers you want to add ?\n");
scanf("%d", &TarifCnt);
AddTarifInfor(TarifInfor, TarifCnt);
printf("\nPress any button to continue");
getch();
break;
case '5':
system("CLS");
TarifInfor = fopen("TarifInfor.txt", "r");
fseek(TarifInfor, 0, SEEK_END);
pos = ftell(TarifInfor);
if (pos > 0)
{
rewind(TarifInfor);
fclose(TarifInfor);
SetDelete(TarifInfor, TarifCnt);
}
else
{
printf("File empty. Please input some info first");
fclose(TarifInfor);
}
printf("\nPress any button to continue");
getch();
break;
case '6':
system("CLS");
ClearFile(TarifInfor);
getch();
break;
default:
stop++;
getch();
break;
}
}}
int SetCount(FILE *TarifInfor)
{
TarifInfor = fopen("TarifInfor.txt", "r");
int count = 0;
while (!feof(TarifInfor))
{
if (fgetc(TarifInfor) == '\n')
{
count++;
}
}
fclose(TarifInfor);
return count;
}
int TempCount(FILE *temp)
{
temp = fopen("temp.txt", "r");
int count = 0;
while (!feof(temp))
{
if (fgetc(temp) == '\n')
{
count++;
}
}
fclose(temp);
return count;
}
void InputTarifInfor(FILE *TarifInfor, size_t TarifCnt)
{
char *NameC, *Energy, *Tarif, *tempInfoStr;
TarifInfor = fopen("TarifInfor.txt", "w+");
for (size_t i = 0; i < TarifCnt; i++)
{
NameC = (char *)calloc(80, 1);
Energy = (char *)calloc(80, 1);
Tarif = (char *)calloc(80, 1);
tempInfoStr = (char *)calloc(255, 1);
printf("Info%d:\n", i + 1);
printf("Input name of consumer %d ", i + 1);
scanf("%s", NameC);
printf("Input quantity of energy %d ", i + 1);
scanf("%s", Energy);
printf("Input tarif %d ", i + 1);
scanf("%s", Tarif);
strcat(tempInfoStr, NameC);
strcat(tempInfoStr, " ");
strcat(tempInfoStr, "- Name of consumer ; ");
strcat(tempInfoStr, Energy);
strcat(tempInfoStr, " ");
strcat(tempInfoStr, "- quantity of energy; ");
strcat(tempInfoStr, Tarif);
strcat(tempInfoStr, " - tarif; ");
strcat(tempInfoStr, "\n");
fprintf(TarifInfor, tempInfoStr);
free(NameC);
free(Energy);
free(Tarif);
free(tempInfoStr);
}
fclose(TarifInfor);
}
int OutputTarifInfor(FILE *TarifInfor, size_t TarifCnt)
{
size_t outp, maxi;
char *tempInfoStr;
printf("Input output block quantity max - %d", SetCount(TarifInfor));
printf("\nInput ");
scanf("%d", &outp);
if (outp > SetCount(TarifInfor))
{
printf("the entered number is more than the amount of information!");
return 0;
}
TarifInfor = fopen("TarifInfor.txt", "r");
for (size_t i = 0; i < outp; i++)
{
tempInfoStr = (char *)calloc(255, 1);
printf("Info%d:\n", i + 1);
fgets(tempInfoStr, 255, TarifInfor);
puts(tempInfoStr);
free(tempInfoStr);
}
fclose(TarifInfor);
}
void AddTarifInfor(FILE *TarifInfor, size_t TarifCnt)
{
char *NameC, *Energy, *Tarif, *tempInfoStr;
TarifInfor = fopen("TarifInfor.txt", "a");
for (size_t i = 0; i < TarifCnt; i++)
{
NameC = (char *)calloc(80, 1);
Energy = (char *)calloc(80, 1);
Tarif = (char *)calloc(80, 1);
tempInfoStr = (char *)calloc(255, 1);
printf("Info%d:\n", i + 1);
printf("Input name of consumer %d ", i + 1);
scanf("%s", NameC);
printf("Input quantity of energy %d ", i + 1);
scanf("%s", Energy);
printf("Input tarif %d ", i + 1);
scanf("%s", Tarif);
strcat(tempInfoStr, NameC);
strcat(tempInfoStr, " ");
strcat(tempInfoStr, "- name of consumer; ");
strcat(tempInfoStr, Energy);
strcat(tempInfoStr, " ");
strcat(tempInfoStr, "- quantity of energy; ");
strcat(tempInfoStr, Tarif);
strcat(tempInfoStr, " - tarif; ");
strcat(tempInfoStr, "\n");
fprintf(TarifInfor, tempInfoStr);
free(NameC);
free(Energy);
free(Tarif);
free(tempInfoStr);
}
fclose(TarifInfor);
}
void ClearFile(FILE *TarifInfor)
{
TarifInfor = fopen("TarifInfor.txt", "w");
fprintf(TarifInfor, NULL);
fclose(TarifInfor);
printf("File cleared!\nPress any button to continue");
}
void SearchInfo(FILE *TarifInfor, size_t TarifCnt)
{
char *tmpStr, *InfoStr, *tempInfoStr, *salarySearch;
int check = 0;
printf("Input name of consumer looked for");
printf("\nInput ");
scanf("%s", &salarySearch);
TarifInfor = fopen("TarifInfor.txt", "r");
for (size_t i = 0; i < SetCount(TarifInfor); i++)
{
tmpStr = (char *)calloc(80, 1);
tempInfoStr = (char *)calloc(255, 1);
InfoStr = (char *)calloc(255, 1);
fgets(tempInfoStr, 255, TarifInfor);
strcat(InfoStr, tempInfoStr);
tmpStr = strtok(tempInfoStr, " ");
if ((tmpStr) == salarySearch)
{
puts(InfoStr);
check++;
}
free(tmpStr);
free(tempInfoStr);
}
if (check == 0)
{
printf("\nInfo not found!!");
}
fclose(TarifInfor);
}
void SetDelete(FILE *TarifInfor, size_t TarifCnt)
{
char *tmpStr, *InfoStr, *tempInfoStr, *salarySearch;
FILE *temp;
size_t check = 0;
printf("Input name of consumer looked for delete");
scanf("%s", &salarySearch);
TarifInfor = fopen("TarifInfor.txt", "r");
temp = fopen("temp.txt", "w+");
for (size_t i = 0; i < SetCount(TarifInfor); i++)
{
tmpStr = (char *)calloc(80, 1);
tempInfoStr = (char *)calloc(255, 1);
InfoStr = (char *)calloc(255, 1);
fgets(tempInfoStr, 255, TarifInfor);
strcat(InfoStr, tempInfoStr);
tmpStr = strtok(tempInfoStr, " ");
if (!((tmpStr) == salarySearch))
{
fputs(InfoStr, temp);
}
else
{
check++;
}
free(tmpStr);
free(tempInfoStr);
}
if (check != 0)
{
fseek(TarifInfor, 0, SEEK_SET);
fseek(temp, 0, SEEK_SET);
fclose(TarifInfor);
TarifInfor = fopen("TarifInfor.txt", "w");
fclose(TarifInfor);
TarifInfor = fopen("TarifInfor.txt", "w");
for (size_t i = 0; i < TempCount(temp); i++)
{
tempInfoStr = (char *)calloc(255, 1);
fgets(tempInfoStr, 255, temp);
fputs(tempInfoStr, TarifInfor);
free(tempInfoStr);
}
fclose(TarifInfor);
fclose(temp);
remove("temp.txt");
printf("string deleted");
}
else
{
remove("temp.txt");
printf("string no found");
}
return 0;
}
