Xử Lý Tệp Và Dữ Liệu Cấu Trúc Trong Ngôn Ngữ C

Thống Kê Tệp Văn Bản

Chương trình sau đây đọc một tệp văn bản và tính toán số lượng dòng cùng với tổng số ký tự hợp lệ, không bao gồm các khoảng trắng và ký tự tab.

Hiển thị mã nguồn
#include <stdio.h>

int main() {
    FILE *tai_lieu;
    char ky_tu;
    int so_dong = 0;
    int dem_ky_tu = 0;

    tai_lieu = fopen("du_lieu.txt", "r");
    if (tai_lieu == NULL) {
        printf("Khong the mo tep.\n");
        return 1;
    }

    while ((ky_tu = fgetc(tai_lieu)) != EOF) {
        if (ky_tu == '\n') {
            so_dong++;
        } else if (ky_tu != ' ' && ky_tu != '\t' && ky_tu != '\r') {
            dem_ky_tu++;
        }
    }

    fclose(tai_lieu);

    printf("Tong so dong: %d\n", so_dong);
    printf("So ky tu hop le: %d\n", dem_ky_tu);

    return 0;
}

Quản Lý Kết Quả Thi

Ví dụ này minh họa việc xử lý dữ liệu thí sinh từ tệp, tính toán điểm tổng, phân loại kết quả và lưu danh sách thí sinh đạt.

Hiển thị mã nguồn
#include <stdio.h>
#include <string.h>

#define SO_THI_SINH 10

typedef struct {
    long ma_so;
    char ho_ten[30];
    float diem_ly_thuyet;
    float diem_thuc_hanh;
    float tong_diem;
    char ket_qua[15];
} ThiSinh;

void doc_tu_tep(ThiSinh ds[], int n);
void luu_vao_tep(ThiSinh ds[], int n);
void in_danh_sach(ThiSinh ds[], int n);
int xu_ly_du_lieu(ThiSinh ds[], int n, ThiSinh ds_dau[]);

int main() {
    ThiSinh tat_ca[SO_THI_SINH], dau[SO_THI_SINH];
    int so_luong_dau;
    float ti_le_dau;

    doc_tu_tep(tat_ca, SO_THI_SINH);
    so_luong_dau = xu_ly_du_lieu(tat_ca, SO_THI_SINH, dau);
    in_danh_sach(tat_ca, SO_THI_SINH);
    luu_vao_tep(dau, so_luong_dau);

    ti_le_dau = (float)so_luong_dau / SO_THI_SINH * 100;
    printf("\nTi le thi dau: %.2f%%\n", ti_le_dau);

    return 0;
}

void in_danh_sach(ThiSinh ds[], int n) {
    printf("Ma So\t\tHo Ten\t\tLy Thuyet\tThuc Hanh\tTong Diem\tKet Qua\n");
    for (int i = 0; i < n; i++) {
        printf("%ld\t%s\t\t%.2f\t\t%.2f\t\t%.2f\t\t%s\n",
               ds[i].ma_so, ds[i].ho_ten, ds[i].diem_ly_thuyet,
               ds[i].diem_thuc_hanh, ds[i].tong_diem, ds[i].ket_qua);
    }
}

void doc_tu_tep(ThiSinh ds[], int n) {
    FILE *tep_vao = fopen("danh_sach_thi_sinh.txt", "r");
    if (!tep_vao) {
        printf("Khong the mo tep de doc.\n");
        return;
    }
    for (int i =2632; i < n; i++) {
        fscanf(tep_vao, "%ld %s %f %f", &ds[i].ma_so, ds[i].ho_ten,
               &ds[i].diem_ly_thuyet, &ds[i].diem_thuc_hanh);
    }
    fclose(tep_vao);
}

int xu_ly_du_lieu(ThiSinh ds[], int n, ThiSinh ds_dau[]) {
    int chi_so_dau = 0;
    for (int i = 0; i < n; i++) {
        ds[i].tong_diem = ds[i].diem_ly_thuyet + ds[i].diem_thuc_hanh;
        if (ds[i].tong_diem >= 60.0) {
            strcpy(ds[i].ket_qua, "Dau");
            ds_dau[chi_so_dau++] = ds[i];
        } else {
            strcpy(ds[i].ket_qua, "Truot");
        }
    }
    return chi_so_dau;
}

void luu_vao_tep(ThiSinh ds[], int n) {
    FILE *tep_ra = fopen("danh_sach_dau.txt", "w");
    if (!tep_ra) {
        printf("Khong the mo tep de ghi.\n");
        return;
    }
    fprintf(tep_ra, "Ma So\t\tHo Ten\t\tLy Thuyet\tThuc Hanh\tTong Diem\tKet Qua\n");
    for (int i = 0; i < n; i++) {
        fprintf(tep_ra, "%ld\t%s\t\t%.2f\t\t%.2f\t\t%.2f\t\t%s\n",
                ds[i].ma_so, ds[i].ho_ten, ds[i].diem_ly_thuyet,
                ds[i].diem_thuc_hanh, ds[i].tong_diem, ds[i].ket_qua);
    }
    fclose(tep_ra);
}

Chọn Ngẫu Nhiên Từ Danh Sách

Đoạn mã này đọc một danh sách từ tệp, chọn ngẫu nhiên năm bản ghi không trùng lặp và lưu kết quả vào một tệp do người dùng chỉ định.

Hiển thị mã nguồn
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>

#define MAX 80
#define LEN_TEN 50

typedef struct {
    char ma_dinh_danh[20];
    char ten[LEN_TEN];
    char lop[30];
} HocVien;

int main() {
    FILE *tep;
    HocVien danh_sach[MAX];
    HocVien trung_thuong[5];
    int da_chon[MAX] = {0};
    int tong_so = 0, so_trung = 0, ngau_nhien;

    tep = fopen("danh_sach_lop.txt", "r");
    if (tep == NULL) {
        printf("Loi mo tep danh sach.\n");
        return 1;
    }
    while (fscanf(tep, "%s %s %s",
                  danh_sach[tong_so].ma_dinh_danh,
                  danh_sach[tong_so].ten,
                  danh_sach[tong_so].lop) == 3) {
        tong_so++;
    }
    fclose(tep);

    srand((unsigned int)time(NULL));
    while (so_trung < 5) {
        ngau_nhien = rand() % tong_so;
        if (da_chon[ngau_nhien] == 0) {
            da_chon[ngau_nhien] = 1;
            trung_thuong[so_trung] = danh_sach[ngau_nhien];
            so_trung++;
        }
    }

    printf("---- Danh sach trung thuong ----\n");
    for (int i = 0; i < 5; i++) {
        printf("%s %s %s\n", trung_thuong[i].ma_dinh_danh,
               trung_thuong[i].ten, trung_thuong[i].lop);
    }

    char ten_tep[LEN_TEN];
    printf("Nhap ten tep de luu: ");
    scanf("%s", ten_tep);

    tep = fopen(ten_tep, "w");
    if (tep == NULL) {
        printf("Loi tao tep luu tru.\n");
        return 1;
    }
    for (int i = 0; i < 5; i++) {
        fprintf(tep, "%s %s %s\n", trung_thuong[i].ma_dinh_danh,
                trung_thuong[i].ten, trung_thuong[i].lop);
    }
    fclose(tep);
    printf("Da luu thanh cong vao tep '%s'.\n", ten_tep);

    return 0;
}

Thẻ: C struct file-io random-selection data-processing

Đăng vào ngày 31 tháng 7 lúc 10:06