Skip to content

luc085.c

Problem Statement

Suppose a file contains student records (Name, Age). Write a program to read these records and display them in sorted order by name.

Metadata

Property Detail
Author Amit Dutta amitdutta4255@gmail.com
Date 08 Feb 2026
License MIT License (See the LICENSE file for details)
Difficulty Advanced (index: 6 / 10)

Concepts

Beta Feature

This concept detection system is still in beta and may occasionally show incorrect or incomplete results.

  • Array
  • Pointers
  • Sorting
  • Iteration
  • Recursion

Actions

Raw View on GitHub

You can print or save this file by opening Raw and using your browser.

Source Code

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

struct student
{
    char name[40];
    int age;
};

void create_dummy_data();
int compare_names(const void *a, const void *b);

int main()
{
    FILE *fp;
    struct student s[100];
    int count = 0, i;

    // Create sample file for demonstration
    create_dummy_data();

    fp = fopen("students.dat", "rb");
    if (fp == NULL)
    {
        printf("Cannot open file!\n");
        exit(1);
    }

    // Read records into array
    while (fread(&s[count], sizeof(struct student), 1, fp) == 1)
    {
        count++;
    }
    fclose(fp);

    // Sort the array
    qsort(s, count, sizeof(struct student), compare_names);

    printf("--- Student List (Sorted by Name) ---\n");
    for (i = 0; i < count; i++)
    {
        printf("Name: %-20s Age: %d\n", s[i].name, s[i].age);
    }

    return 0;
}

int compare_names(const void *a, const void *b)
{
    return strcmp(((struct student *)a)->name, ((struct student *)b)->name);
}

void create_dummy_data()
{
    FILE *fp = fopen("students.dat", "wb");
    struct student data[] = {
        {"Zack", 20}, {"Alice", 19}, {"Bob", 21}, {"Charlie", 20}, {"Yasmine", 19}
    };
    if (fp)
    {
        fwrite(data, sizeof(struct student), 5, fp);
        fclose(fp);
    }
}

Explanation

Explain with AI

Copy the prompt below and paste it into any AI assistant.

    You are explaining a C programming code to a beginner.

    STRICT RULES:

    - Only use the given code. Do NOT assume anything not present.

    - Do NOT add extra examples.

    - Keep explanation clear and short.

    - If something is unclear, say "Not clear from code".

    - Follow the exact format below. Do NOT change headings.

    FORMAT:

    [START]

    ## What it does

    (Explain the overall purpose in 1-2 sentences)

    ## Step-by-step

    (Explain how the code works in steps, simple language)

    ## Key Concepts

    (List concepts like loop, condition, function, etc.)

    ## Notes

    (Mention any limitations, errors, or assumptions)

    [END]

    CODE (luc085.c):

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

    struct student
    {
        char name[40];
        int age;
    };

    void create_dummy_data();
    int compare_names(const void *a, const void *b);

    int main()
    {
        FILE *fp;
        struct student s[100];
        int count = 0, i;

        // Create sample file for demonstration
        create_dummy_data();

        fp = fopen("students.dat", "rb");
        if (fp == NULL)
        {
            printf("Cannot open file!\n");
            exit(1);
        }

        // Read records into array
        while (fread(&s[count], sizeof(struct student), 1, fp) == 1)
        {
            count++;
        }
        fclose(fp);

        // Sort the array
        qsort(s, count, sizeof(struct student), compare_names);

        printf("--- Student List (Sorted by Name) ---\n");
        for (i = 0; i < count; i++)
        {
            printf("Name: %-20s Age: %d\n", s[i].name, s[i].age);
        }

        return 0;
    }

    int compare_names(const void *a, const void *b)
    {
        return strcmp(((struct student *)a)->name, ((struct student *)b)->name);
    }

    void create_dummy_data()
    {
        FILE *fp = fopen("students.dat", "wb");
        struct student data[] = {
            {"Zack", 20}, {"Alice", 19}, {"Bob", 21}, {"Charlie", 20}, {"Yasmine", 19}
        };
        if (fp)
        {
            fwrite(data, sizeof(struct student), 5, fp);
            fclose(fp);
        }
    }