Featured Post

Trie implementation in C

Complete File Input Output Solution in C

This is a complete file input output solution where user enters some information on the terminal and it gets stored in a file. Also this is a structured file operation program which suports :

1. Insertion of a record
2. Deletion of a record
3. Updation of a record
4. Searching of a record

Note: This program assumes that user enters the data in the correct format as required. i.e. Where the input is number and user enters a character , the program will go into infinite loop. User input is not validated in this program as the intent for writing this program is to understand file input output.

#include <stdio.h>
#define REC_SIZE 56

enum { NOTFOUND = 0, FOUND};

FILE *fp;
typedef struct record
{
    int id;
    char name[20];
    char desig[30];
} Record;

void die(char* msg)
{
    perror(msg);
    exit(0);
}

void insert(num)
{
    Record rec;
    int i ;

    fp = fopen("Record.dat", "a");
    if(!fp)
        die("fopen");

    for(i = 0 ; i< num ; i++)
    {
        printf("\nEnter id (number): ");
        scanf("%d",&rec.id);  //User is expected to enter an integer here

        printf("\nEnter name : ");
        getchar();
        scanf("%[^\n]",rec.name);

        printf("\nEnter desig : ");
        getchar();
        scanf("%[^\n]",rec.desig);

        fwrite(&rec,sizeof(struct record),1,fp);
    }
    fclose(fp);
}

FILE* search(int id)
{
    FILE *fp;
    short flag = NOTFOUND;

    fp = fopen("Record.dat", "r");
    int temp;
    char ch;


    while( fread(&temp,sizeof(int),1,fp) != 0)
    {
        //printf("temp: %d\n",temp);
        if(temp == id)
        {
            flag = FOUND;
            break;
        }
        else
            fseek(fp,sizeof(struct record)- sizeof(int),SEEK_CUR);
    }

    if(flag == NOTFOUND)
    {
        fp = NULL;
        //printf("Record Not Found!!!\n");

    }
    else
    {
        fseek(fp,-sizeof(int),SEEK_CUR);
    }

    return fp;
}

void replace(char *initial , char * final)
{
    strcpy(initial, final);
}

void delete(int id)
{
    FILE *fp1 , *fp2 ,*fpfinal;
    char ch;
    int i = 0;
    int prior = 0 ,tempfp;

    if((fp1 = search(id)) == NULL)
    {
        printf("Record Not Found");
        return;
    }

    fp2 = fopen("temp.txt","w+");

    if(!fp2)
        die("fopen()");

    tempfp = ftell(fp1);
    fseek(fp1,0,SEEK_SET);

    prior =  ftell(fp1);

    while(prior < tempfp)
    {
        fputc(fgetc(fp1),fp2);
        prior++;
    }

    fseek(fp1,sizeof(struct record),SEEK_CUR);
    while((ch = fgetc(fp1)) != EOF)
    {
        fputc(ch,fp2);
    }

    fseek(fp2,0,SEEK_SET);
    fpfinal = fopen("Record.dat","w");

    while((ch = fgetc(fp2)) != EOF)
    {
        fputc(ch,fpfinal);
    }

    fclose(fp1);
    fclose(fp2);
    fclose(fpfinal);

    if(remove("temp.txt") == -1)
        die("Error removing temp file");
}


void update(int id)
{
    int choice;
    FILE *fp ,*fpnew;
    Record temprec;

    if((fp = search(id)) == NULL)
    {
        printf("Record not present!!\n");
        return;
    }

    printf("\nWhich field u wanna update ? \n1. Id 2. Name 3. Designation\n Enter : ");
    scanf("%d",&choice); //User is expected to enter an integer here

    fpnew = fopen("Record.dat","r+");
    fseek(fpnew, ftell(fp), SEEK_SET);

    fclose(fp);

    switch (choice)
    {
    case 1 :
        printf("\nEnter new id : ");
        scanf("%d",&temprec.id); //User is expected to enter an integer here
        fwrite(&temprec.id,sizeof(temprec.id),1,fpnew);
        break;
    case 2 :
        printf("\nEnter new name : ");
        getchar();
        scanf("%[^\n]",temprec.name);
        fseek(fpnew,sizeof(temprec.id),SEEK_CUR);
        fwrite(temprec.name,sizeof(temprec.name),1,fpnew);
        break;
    case 3 :
        printf("\nEnter new desig : ");
        getchar();
        scanf("%[^\n]",temprec.desig);
        fseek(fpnew,sizeof(temprec.id)+ sizeof(temprec.name),SEEK_CUR);
        fwrite(temprec.desig,sizeof(temprec.desig),1,fpnew);
        break;
    default :
        printf("Wrong Choice\n");
        break;
    }
    fclose(fpnew);
}

void list_rec()
{
    FILE *fp;
    char line[REC_SIZE];
    Record rec;
    int nbytes,i=0;

    fp = fopen("Record.dat","r");
    if(fp == NULL)
        die("Open Record.dat");
    while(1)
    {
        nbytes = fread(&rec,sizeof(struct record),1,fp);
        if(nbytes == 0)
            break;
        printf("%d\t%s\t%s\n",rec.id,rec.name,rec.desig);
    }
    fclose(fp);
}
int main()
{
    int id, num, choice ;
    while(1)
    {
        printf("\n1.Insert\n2.Search\n3.Delete\n4.List\n5.Update\n6.Exit\nEnter choice : ");
        scanf("%d",&choice); //User is expected to enter an integer here

        switch (choice)
        {
        case 1 :
            printf("\nEnter num of records: ");
            scanf("%d",&num);//User is expected to enter an integer here
            insert(num);
            break;
        case 2 :
            printf("\nEnter id to search: ");
            scanf("%d",&id); //User is expected to enter an integer here

            if(search(id) == NULL)
            {
                printf("\nRecord Not Found!!\n");
            }
            else
                printf("\nRecord is present in the system!!!\n");

            break;
        case 3 :
            printf("\nEnter id to delete: ");
            scanf("%d",&id); //User is expected to enter an integer here
            delete(id);
            break;
        case 4 :
            list_rec();
            break;
        case 5 :
            printf("\nEnter id to update: ");
            scanf("%d",&id); //User is expected to enter an integer here 

            update(id);
            break;
        case 6 :
            exit(0);
        default :
            printf("Wrong Choice\n");
            break;
        }
    }
}




Output in file "Record.dat" , in the same directory as the proram resides.

Comments