Character and Line Counting in Text Files
To perform basic file analysis, such as counting the number of lines and identifying the density of non-whitespace characters, the following implementation utilizes fgetc to iterate through a file stream. This approach avoids loading the entire file into memory, making it efficient for larger datasets.
#include <stdio.h>
#include <ctype.h>
int main() {
FILE *fp = fopen("data4.txt", "r");
if (fp == NULL) {
perror("Error opening file");
return 1;
}
int row_count = 0;
int visible_char_count = 0;
int current_char;
while ((current_char = fgetc(fp)) != EOF) {
if (current_char == '\n') {
row_count++;
}
if (!isspace(current_char)) {
visible_char_count++;
}
}
fclose(fp);
// Handle the last line if it doesn't end with a newline character
printf("Statistics for data4.txt:\n");
printf("Total Rows: %d\n", row_count + 1);
printf("Total Characters (excluding whitespace): %d\n", visible_char_count);
return 0;
}
Processing Academic Records with Structured Data
This implementation focuses on handling student examination data. It reads raw scores from a file, calculates the aggregate totals, determines the pass/fail status based on a specific threshold, and exports the successful candidates to a separate report. The code uses structured types and dedicated functions for input, processing, and output operations.
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define CAPACITY 10
#define THRESHOLD 60.0
typedef struct {
long serial_number;
char student_name[30];
float score_theory;
float score_practical;
float total_score;
char status[15];
} Candidate;
void load_records(Candidate list[], int size);
void export_successful(Candidate list[], int count);
void print_summary(Candidate list[], int size);
int analyze_results(Candidate list[], int size, Candidate filtered[]);
int main() {
Candidate all_students[CAPACITY];
Candidate passed_students[CAPACITY];
load_records(all_students, CAPACITY);
int pass_count = analyze_results(all_students, CAPACITY, passed_students);
print_summary(all_students, CAPACITY);
export_successful(passed_students, pass_count);
printf("\nSuccess Rate: %.2f%%\n", (float)pass_count / CAPACITY * 100);
return 0;
}
void load_records(Candidate list[], int size) {
FILE *src = fopen("examinee.txt", "r");
if (!src) return;
for (int i = 0; i < size; i++) {
if (fscanf(src, "%ld %s %f %f", &list[i].serial_number, list[i].student_name,
&list[i].score_theory, &list[i].score_practical) != 4) break;
}
fclose(src);
}
int analyze_results(Candidate list[], int size, Candidate filtered[]) {
int count = 0;
for (int i = 0; i < size; i++) {
list[i].total_score = list[i].score_theory + list[i].score_practical;
if (list[i].total_score >= THRESHOLD) {
strcpy(list[i].status, "Pass");
filtered[count++] = list[i];
} else {
strcpy(list[i].status, "Fail");
}
}
return count;
}
void print_summary(Candidate list[], int size) {
printf("%-12s %-15s %-10s %-10s %-10s %-10s\n", "ID", "Name", "Theory", "Practice", "Total", "Status");
for (int i = 0; i < size; i++) {
printf("%-12ld %-15s %-10.2f %-10.2f %-10.2f %-10s\n",
list[i].serial_number, list[i].student_name,
list[i].score_theory, list[i].score_practical,
list[i].total_score, list[i].status);
}
}
void export_successful(Candidate list[], int count) {
FILE *dest = fopen("list_pass.txt", "w");
if (!dest) return;
fprintf(dest, "ID\tName\tTotal\tStatus\n");
for (int i = 0; i < count; i++) {
fprintf(dest, "%ld\t%s\t%.2f\t%s\n",
list[i].serial_number, list[i].student_name,
list[i].total_score, list[i].status);
}
fclose(dest);
}
Randomized Selection System
A common requirement in administrative software is the random selection of records (e.g., alottery or random audit). This program reads a list of entities, performs a unique random selection using a tracking array, sorts the chosen entries by their identifier, and records the session metadata to an output file.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#define MAX_POOL 100
#define SELECT_COUNT 5
typedef struct {
char uid[25];
char full_name[40];
} Member;
int fetch_data(Member pool[], const char *path);
void pick_winners(Member pool[], int total, Member result[]);
void sort_by_uid(Member result[], int count);
void log_results(Member result[], int count, const char *path);
int main() {
Member entry_pool[MAX_POOL];
Member winners[SELECT_COUNT];
srand((unsigned int)time(NULL));
int total_entries = fetch_data(entry_pool, "list.txt");
if (total_entries < SELECT_COUNT) return 1;
pick_winners(entry_pool, total_entries, winners);
sort_by_uid(winners, SELECT_COUNT);
log_results(winners, SELECT_COUNT, "results.txt");
return 0;
}
int fetch_data(Member pool[], const char *path) {
FILE *f = fopen(path, "r");
if (!f) return 0;
int i = 0;
while (i < MAX_POOL && fscanf(f, "%s %s", pool[i].uid, pool[i].full_name) == 2) i++;
fclose(f);
return i;
}
void pick_winners(Member pool[], int total, Member result[]) {
int *used = calloc(total, sizeof(int));
int found = 0;
while (found < SELECT_COUNT) {
int idx = rand() % total;
if (!used[idx]) {
result[found++] = pool[idx];
used[idx] = 1;
}
}
free(used);
}
void sort_by_uid(Member result[], int count) {
for (int i = 0; i < count - 1; i++) {
for (int j = 0; j < count - i - 1; j++) {
if (strcmp(result[j].uid, result[j+1].uid) > 0) {
Member tmp = result[j];
result[j] = result[j+1];
result[j+1] = tmp;
}
}
}
}
void log_results(Member result[], int count, const char *path) {
FILE *f = fopen(path, "w");
if (!f) return;
time_t now = time(NULL);
fprintf(f, "Selection Date: %s", ctime(&now));
for (int i = 0; i < count; i++) {
fprintf(f, "[%d] %-20s %-20s\n", i + 1, result[i].uid, result[i].full_name);
}
fclose(f);
}