1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152
|
/// Data Base
#include <stdio.h>
#include <windows.h>
#include <string.h>
void inputData();
void retrieveData();
struct Info
{
char name[50];
char lastname[50];
char city[50];
char country[50];
char tel[20];
}info;
int main()
{
int temp, status;
unsigned options;
do
{
printf("\n\t\t\t\tData Base\n\t\t\t\t---------");
printf("\n\n\n\tChoose an option:\n\n");
printf("\n 1. Input info");
printf("\n 2. Retrieve info");
printf("\n 3. Exit Data Base");
printf("\n\n Select: ");
status = scanf("%d", &options);
while((temp = getchar()) != EOF && temp != '\n');
if(!status)
{
printf("\n\n Invalid option.. Choose 1, 2 or 3\a");
Sleep(2000);
system("cls");
options = 0;
continue;
}
else if(options == 1)
{
inputData();
}
else if(options == 2)
{
retrieveData();
}
else if(options == 3)
{
printf("\n\n\t\t\t Exiting Data Base");
Sleep(2000);
system("cls");
}
else
{
printf("\n\n Invalid option.. Choose 1, 2 or 3\a");
Sleep(2000);
system("cls");
}
}while(options != 3);
return 0;
}
void inputData()
{
FILE *file;
file = fopen("records.txt", "a");
if(!file)
{
printf("\n File could not be open: \n\n\a");
Sleep(1000);
printf(" %s", strerror(errno));
Sleep(2500);
system("cls");
return;
}
system("cls");
printf("\n\t\t\t\tData Base\n\t\t\t\t---------");
printf("\n\n Enter info");
printf("\n\n\n\tName: ");
scanf("%s", info.name);
printf("\n\n\tLast name: ");
scanf("%s", info.lastname);
printf("\n\n\tCity: ");
scanf("%s", info.city);
printf("\n\n\tCountry: ");
scanf("%s", info.country);
printf("\n\n\tPhone: ");
scanf("%s", info.tel);
fprintf(file, "%s %s %s %s %s\n", info.name, info.lastname, info.city, info.country, info.tel);
fclose(file);
printf("\n\n\n Contact saved to ledger\n\n"
" ..returning to main menu");
Sleep(2500);
system("cls");
}
void retrieveData()
{
FILE *file = fopen("records.txt", "r");
int found = 0;
char tmp[50];
if(!file)
{
printf("\n File could not be open: \n\n\a");
Sleep(1000);
printf(" %s", strerror(errno));
Sleep(2500);
system("cls");
return;
}
system("cls");
printf("\n\t\t\t\tData Base\n\t\t\t\t---------");
printf("\n\n Search info");
printf("\n\n\n\tName: ");
scanf("%s", tmp);
while(!feof(file))
{
fscanf(file, "%s%s%s%s%s", info.name, info.lastname, info.city, info.country, info.tel);
if(strcmp(tmp, info.name) == 0)
{
printf("\n\n Info found:\n\n Last name: %s\n City:\t "
"%s\n Country: %s\n Phone: %s\n\n",
info.lastname, info.city, info.country, info.tel);
found = 1;
Sleep(7000);
printf("\n\n ..returning to main menu");
Sleep(3000);
system("cls");
}
}
if(!found)
{
printf("\n\n Info not found\n\a");
Sleep(2500);
printf("\n\n ..returning to main menu");
Sleep(2000);
system("cls");
}
fclose(file);
}
|