C Codes

Fixed Length File Records

Properties:

  • Every record in the file has exactly same size (in byte)
  • Computer knows exact location of records so easy access
  • Slow in transferring the records it has large size

Sample Program:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

void add_customer()
{
    FILE * fp;
    char filename[30] = "customer.txt";
    int registration_number;
    char name[20];
    int age;
    char city[20];
    int count = 0;
    int record_length = 50;
    int remain = 0;
    int space_count = 3;
    int i;

    fp = fopen(filename, "a");
    if(fp == NULL) {
        printf("Cannot open the file\n");
        exit(0);
    }

    printf("Enter the following details: Reg Num, name, Age, City\n\n");
    scanf("%d %s %d %s", &registration_number, name, &age, city);

    int trn, tage;
    trn = registration_number;
    tage = age;

    // Get the number of bytes of integer data
    while(trn != 0) {
        trn /= 10;
        count++;
    }

    while(tage != 0) {
        tage /= 10;
        count++;
    }

    // Add the count from strings and spaces
    count = count + strlen(name) + strlen(city);
    count = count + space_count;
    // Calculate the remain, needed to pad the extra bytes into the file
    remain = record_length - count;

    // Write the data into the file
    fprintf(fp, "%d %s %d %s ", registration_number, name, age,city);

    // Pad the remain with a special character not occurring in the file – say ‘#’
    for (i = 0; i < remain; i++)
        putc('#', fp);

    fclose(fp);
}

void display()
{
    FILE * fp2;
    char filename[30] = "customer.txt";

    int registration_number;
    char name[20];
    int age;
    char city[20];
    int count = 0;

    fp2 = fopen(filename, "r");
    if(fp2 == NULL) {
        printf("Cannot open the file\n");
        exit(0);
    }

    char ch;
    while((ch=getc(fp2)) != EOF)
    {
        if (ch != '#'){
            printf("%c", ch);
        }
        count ++;
        if(count  == 50) {
            printf("\n");
            count  = 0;
        }
    }

    fclose(fp2);
}

int main()
{
    int choice = 0;

    while(1)
    {
        printf("Menu\n");
        printf("1- Register a new customer\n2-Display All customers\n3-Exit\n\n");
        printf("Enter your choice\n");
        scanf("%d", &choice);

        switch(choice)
        {
            case 1: printf("New customer will be added to file\n");
                    add_customer();
                    printf("Customer added successfully\n");
                    break;

            case 2: display();
                    break;

            case 3: exit(0);
        }
    }
    return 0;
}

Advertisements

Let me Know What you Think!

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s