Fading Coder

One Final Commit for the Last Sprint

Home > Tech > Content

File Processing and Student Exam Result Analysis in C

Tech May 7 3

File Character and Line Coutning

#include <stdio.h>
#include <ctype.h>
#define MAX_SIZE 100

int main() {
    FILE* file_ptr;
    file_ptr = fopen("input.txt", "r");
    if (!file_ptr) {
        perror("File opening failed");
        return 1;
    }

    int line_count = 0;
    int char_count = 0;
    char buffer[MAX_SIZE];
    
    while (fgets(buffer, MAX_SIZE, file_ptr)) {
        line_count++;
    }
    
    rewind(file_ptr);
    
    int current_char;
    while ((current_char = fgetc(file_ptr)) != EOF) {
        if (!isspace(current_char)) {
            char_count++;
        }
    }
    
    printf("File analysis results:\n");
    printf("Total lines: %d\n", line_count);
    printf("Non-whitespace characters: %d\n", char_count);
    
    fclose(file_ptr);
    return 0;
}

Student Exam Processing System

#include <stdio.h>
#include <string.h>
#define MAX_STUDENTS 10

typedef struct {
    long exam_id;
    char student_name[20];
    float theory_score;
    float practical_score;
    float total_score;
    char status[10];
} Student;

void load_student_data(Student students[], int count);
void save_results(Student students[], int count);
void display_results(Student students[], int count);
int evaluate_students(Student all[], int total, Student passed[]);

int main() {
    Student all_students[MAX_STUDENTS];
    Student passed_students[MAX_STUDENTS];
    int passed_count;
    
    printf("Loading %d student records...\n", MAX_STUDENTS);
    load_student_data(all_students, MAX_STUDENTS);
    
    passed_count = evaluate_students(all_students, MAX_STUDENTS, passed_students);
    
    printf("\nExam results:\n");
    display_results(all_students, MAX_STUDENTS);
    save_results(passed_students, passed_count);
    
    float pass_percentage = (float)passed_count / MAX_STUDENTS * 100;
    printf("\nPass rate: %.2f%%\n", pass_percentage);
    
    return 0;
}

void display_results(Student students[], int count) {
    printf("ID\t\tName\t\tTheory\tPractical\tTotal\t\tStatus\n");
    for (int i = 0; i < count; i++) {
        printf("%ld\t%s\t%.2f\t%.2f\t\t%.2f\t\t%s\n", 
               students[i].exam_id, students[i].student_name,
               students[i].theory_score, students[i].practical_score,
               students[i].total_score, students[i].status);
    }
}

void load_student_data(Student students[], int count) {
    FILE* input_file = fopen("students.txt", "r");
    if (!input_file) {
        perror("Failed to open input file");
        return;
    }
    
    for (int i = 0; i < count; i++) {
        fscanf(input_file, "%ld %s %f %f", 
              &students[i].exam_id, students[i].student_name,
              &students[i].theory_score, &students[i].practical_score);
    }
    fclose(input_file);
}

void save_results(Student students[], int count) {
    FILE* output_file = fopen("passed_students.txt", "w");
    if (!output_file) {
        perror("Failed to create output file");
        return;
    }
    
    fprintf(output_file, "ID\t\tName\t\tTheory\tPractical\tTotal\t\tStatus\n");
    for (int i = 0; i < count; i++) {
        fprintf(output_file, "%ld\t%s\t%.2f\t%.2f\t\t%.2f\t\t%s\n",
               students[i].exam_id, students[i].student_name,
               students[i].theory_score, students[i].practical_score,
               students[i].total_score, students[i].status);
    }
    fclose(output_file);
}

int evaluate_students(Student all[], int total, Student passed[]) {
    int pass_count = 0;
    
    for (int i = 0; i < total; i++) {
        all[i].total_score = all[i].theory_score + all[i].practical_score;
        
        if (all[i].total_score >= 60) {
            strcpy(all[i].status, "Passed");
            passed[pass_count++] = all[i];
        } else {
            strcpy(all[i].status, "Failed");
        }
    }
    return pass_count;
}

Random Seelction and File Output

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>
#define MAX_ENTRIES 100

int main() {
    char entries[MAX_ENTRIES][MAX_ENTRIES];
    char winners[5][MAX_ENTRIES];
    int selected[MAX_ENTRIES] = {0};
    
    FILE* input_file = fopen("entries.txt", "r");
    if (!input_file) {
        perror("Failed to open input file");
        return 1;
    }
    
    int entry_count = 0;
    while (fgets(entries[entry_count], MAX_ENTRIES, input_file)) {
        entry_count++;
    }
    fclose(input_file);
    
    srand(time(NULL));
    printf("Selected winners:\n");
    
    for (int i = 0; i < 5; i++) {
        int random_index;
        do {
            random_index = rand() % entry_count;
        } while (selected[random_index]);
        
        selected[random_index] = 1;
        strcpy(winners[i], entries[random_index]);
        printf("%s", winners[i]);
    }
    
    printf("\nEnter output filename: ");
    char filename[MAX_ENTRIES];
    scanf("%s", filename);
    
    FILE* output_file = fopen(filename, "w");
    if (!output_file) {
        perror("Failed to create output file");
        return 1;
    }
    
    for (int i = 0; i < 5; i++) {
        fputs(winners[i], output_file);
    }
    
    fclose(output_file);
    return 0;
}

Related Articles

Understanding Strong and Weak References in Java

Strong References Strong reference are the most prevalent type of object referencing in Java. When an object has a strong reference pointing to it, the garbage collector will not reclaim its memory. F...

Comprehensive Guide to SSTI Explained with Payload Bypass Techniques

Introduction Server-Side Template Injection (SSTI) is a vulnerability in web applications where user input is improper handled within the template engine and executed on the server. This exploit can r...

Implement Image Upload Functionality for Django Integrated TinyMCE Editor

Django’s Admin panel is highly user-friendly, and pairing it with TinyMCE, an effective rich text editor, simplifies content management significantly. Combining the two is particular useful for bloggi...

Leave a Comment

Anonymous

◎Feel free to join the discussion and share your thoughts.