Phone Diary sample ! (add,search,modify)simple version

#include <stdio.h>
#include <conio.h>
#include <string.h>
#include <stdlib.h>
void add();
void search();
void update();
struct number
{
    char name[20];
    char phnum[15];
n;

int main(void)
{

    int choice;
    printf("1.Add\n2.Search\n3.Update\n");
    scanf("%d", &choice);
    switch (choice)
    {
    case 1:
        add();
        break;

    case 2:
        search();
        break;
    case 3:
        update();
        break;
    }
}

void add()
{
    // struct number n;
    FILE *filepointer;
    filepointer = fopen("diary.txt""a+");
    printf("Enter The Phone Number (country code + 10digits) :");
    scanf("%s", &n.phnum);
    // printf("%s", n.phnum);
    fflush(stdin);

    printf("Choose The Contact Name :");
    scanf("%[^\n]s"strlwr(n.name));

    fwrite(&nsizeof(n), 1filepointer);
    fclose(filepointer);
    printf("\nSuccessfull Adding To File\n\n");
    printf("Press \'A\' to continue adding , \'S\' to search Contact ,\'U\' to update Contact, esc to close ");
    char ch = getch();

    if (ch == 'A' || ch == 'a')
    {
        system("cls");
        add();
    }
    if (ch == 'u' || ch == 'U')
    {
        system("cls");
        update();
    }
    if (ch == 's' || ch == 'S')
    {
        system("cls");
        search();
    }
}

void search()
{

    char searchname[50];

    FILE *p;

    p = fopen("diary.txt""r");

    fflush(stdin);
    printf("Enter Contact Name To Search :");
    scanf("%[^\n]s"strlwr(searchname));
    while (fread(&nsizeof(n), 1p))
    {

        if (strcmp(searchnamen.name) == 0)
        {

            printf("\n---------------\n%s\n%s\n---------------\n"strupr(n.name), n.phnum);
        }
        else
        {
            printf("No Data Found");
            fflush(stdin);

            printf("\nPress Y to Search Again And esc to exit  ");
            char ch = getch();
            if (ch == 'y' || ch == 'Y')
            {

                search();
            }
        }
        printf("Press \'A\' to continue adding  ,\'U\' to update Contact, esc to close \n");
        char ch = getch();

        if (ch == 'A' || ch == 'a')
        {
            system("cls");
            add();
        }
        if (ch == 'u' || ch == 'U')
        {
            system("cls");
            update();
        }
        if (ch == 's' || ch == 'S')
        {
            system("cls");
            search();
        }
    }
    fclose(p);
}

void update()
{
    FILE *p;
    char updatename[20];
    long int size = sizeof(n);
    p = fopen("diary.txt""r+");

    fflush(stdin);
    printf("Enter Contact Name To Update : ");
    scanf("%[^\n]s"strlwr(updatename));
    while (fread(&nsizeof(n), 1p) == 1)
    {
        if (strcmp(n.nameupdatename) == 0)
        {

            printf("\nEnter New Phone Number:");
            scanf("%s", &n.phnum);

            fseek(p, -sizeSEEK_CUR); //points current position in file
            fwrite(&nsizeof(n), 1p);
            break//imppppppp
        }
    }
    printf("\nSuccessfull updated New Number To File\n\n");
    printf("Press \'A\' to continue adding , \'S\' to search Contact ,\'U\' to update Contact, esc to close ");
    char ch = getch();

    if (ch == 'A' || ch == 'a')
    {
        system("cls");
        add();
    }
    if (ch == 'u' || ch == 'U')
    {
        system("cls");
        update();
    }
    if (ch == 's' || ch == 'S')
    {
        system("cls");
        search();
    }
    fclose(p);
}

SHARE

Milan Tomic

Hi. I’m Designer of Blog Magic. I’m CEO/Founder of ThemeXpose. I’m Creative Art Director, Web Designer, UI/UX Designer, Interaction Designer, Industrial Designer, Web Developer, Business Enthusiast, StartUp Enthusiast, Speaker, Writer and Photographer. Inspired to make things looks better.

  • Image
  • Image
  • Image
  • Image
  • Image
    Blogger Comment
    Facebook Comment

0 comments:

Post a Comment