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
|
#include <stdio.h>
#include <windows.h>
#include <string.h>
struct Info
{
char name[50];
char lastname[50];
char city[50];
char country[50];
char tel[20];
};
const char *file_name = "records.txt";
int menu_choice();
void input_data();
void display_data();
void search_data();
int main()
{
int choice;
for(;;)
{
choice = menu_choice();
switch(choice)
{
case 1:
input_data();
break;
case 2:
display_data();
break;
case 3:
search_data();
break;
case 4:
printf("Thanks for using our app.\tGood bye.");
return EXIT_SUCCESS;
default:
printf("Invalid choice, please try again");
}
}
return EXIT_SUCCESS;
}
int menu_choice()
{
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 data");
printf("\n 2. Load data");
printf("\n 3. Search");
printf("\n 4. Exit ");
printf("\n\n Select: ");
int choice;
scanf("%d", &choice);
return choice;
}
void input_data()
{
FILE *file;
struct Info info;
file = fopen("records.txt", "a");
if (file == NULL)
{
perror("Can't open records.txt");
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("%50s", info.name);
printf("\n\n\tLast name: ");
scanf("%50s", info.lastname);
printf("\n\n\tCity: ");
scanf("%50s", info.city);
printf("\n\n\tCountry: ");
scanf("%50s", info.country);
printf("\n\n\tPhone: ");
scanf("%20s", 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 display_data()
{
struct Info info = {0};
FILE *src = fopen(file_name, "r");
if (src == NULL)
{
perror(NULL);
return;
}
printf("--- Data in file ---\n");
while (fscanf(src, "%50s %50s %50s %50s %20s",
info.name, info.lastname, info.city, info.country, info.tel) != EOF)
{
printf("%-15s %-15s %-15s %-15s %-s\n",
info.name, info.lastname, info.city, info.country, info.tel);
}
}
void search_data()
{
printf("I am only a stub - you have to implement me.");
}
|