You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

DevC++/CodeBlocks控制台数据表格显示错乱问题求助(附C语言代码)

C语言科目管理程序表格显示错乱问题解决

问题根源

  • 你用\t制表符对齐表格,但不同终端环境下制表符的默认宽度(4或8空格)不统一,DevC++/CodeBlocks的控制台可能采用了和你朋友不同的制表符宽度,导致列错位。
  • 若控制台使用非等宽字体(比如宋体),即使制表符宽度一致,不同字符的显示宽度差异也会让表格错乱。
  • 代码存在两处冗余/逻辑问题:
    • display函数里无需打开subjects.txt,直接使用内存数组的数据即可,文件操作属于冗余步骤。
    • add函数直接用scanf("%d", n)覆盖从文件加载的n值,会导致旧数据丢失,应该追加新数据而非覆盖原有数组。

解决方案

1. 用固定宽度格式化输出替代制表符

将display函数的输出语句改为%格式控制固定宽度,确保每一列对齐,示例:

printf("%-15s%-25s%6d%6d%6d\n", b[i].scode, b[i].descript, b[i].units, b[i].lect, b[i].lab);
  • %-15s:左对齐,占15字符宽度,适配科目代码
  • %-25s:左对齐,占25字符宽度,适配描述文本
  • %6d:右对齐,占6字符宽度,适配数字类型

2. 设置控制台为等宽字体

在DevC++/CodeBlocks的控制台窗口右键→属性→字体,选择Consolas、Courier New这类等宽字体,保证每个字符显示宽度一致。

3. 修复代码逻辑问题

  • 移除display函数中的文件打开/关闭代码
  • 修改add函数逻辑,实现新数据追加而非覆盖旧数据

修改后的完整代码

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

struct Subject {
    char scode[30];
    char descript[50];
    int units;
    int lect;
    int lab;
};


int add(struct Subject b[], int *n);
int display(struct Subject b[], int n);
int search(struct Subject b[], int n);
int update(struct Subject b[], int n);
int del(struct Subject b[], int *n);
void exit_program();
void SaveFile(struct Subject b[], int n);
void LoadFile(struct Subject b[], int *n);

int main() {
    struct Subject b[50];
    int n = 0, ch;
    int i;

    LoadFile(b, &n);

    do {
        printf("\nA. Add");
        printf("\nB. Display");
        printf("\nS. Search");
        printf("\nU. Update");
        printf("\nD. Delete");
        printf("\nX. Exit");
        printf("\nYour Option -> ");
        ch = getchar();

        switch (tolower(ch)) {
            case 'a':
                add(b, &n);
                break;
            case 'b':
                display(b, n);
                break;
            case 's':
                search(b, n);
                break;
            case 'u':
                update(b, n);
                break;
            case 'd':
                del(b, &n);
                break;
            case 'x':
                SaveFile(b, n);
                exit_program();
                break;
            default:
                printf("Invalid option. Please try again.\n");
        }

        while (getchar() != '\n');
    } while (ch != 'x');

    return 0;
}

void SaveFile(struct Subject b[], int n) {
    FILE *file = fopen("subjects.txt", "w");
    if (file == NULL) {
        perror("Error opening file for writing");
        return;
    }

    for (int i = 0; i < n; i++) {
        fprintf(file, "%s %s %d %d %d\n", b[i].scode, b[i].descript, b[i].units, b[i].lect, b[i].lab);
    }

    fclose(file);
}

void LoadFile(struct Subject b[], int *n) {
    FILE *file = fopen("subjects.txt", "r");
    if (file == NULL) {
        perror("Error opening file for reading");
        return;
    }

    while (fscanf(file, "%s %s %d %d %d", b[*n].scode, b[*n].descript, &b[*n].units, &b[*n].lect, &b[*n].lab) == 5) {
        (*n)++;
        if (*n >= 50) {
            printf("Warning: Maximum number of subjects reached.\n");
            break;
        }
    }

    fclose(file);
}

int display(struct Subject b[], int n) {
    if (n == 0) {
        printf("No records to display.\n");
        return 0;
    }
    printf("The Records are: \n");
    // 固定宽度表头
    printf("%-15s%-25s%6s%6s%6s\n", "SubCode", "Descript", "Units", "Lecs", "Labs");
    for (int i = 0; i < n; i++) {
        printf("%-15s%-25s%6d%6d%6d\n", b[i].scode, b[i].descript, b[i].units, b[i].lect, b[i].lab);
    }
    return 0;
}

int add(struct Subject b[], int *n) {
    int add_count;
    printf("Enter number of Subjects to add: ");
    scanf("%d", &add_count);

    // 检查数组上限
    if (*n + add_count > 50) {
        printf("Error: Adding %d subjects will exceed maximum limit of 50.\n", add_count);
        return 1;
    }

    for (int i = *n; i < *n + add_count; i++) {
        printf("\nEnter Subject Code: ");
        scanf("%s", b[i].scode);
        printf("Enter Description: ");
        scanf("%s", b[i].descript);
        printf("Enter Units: ");
        scanf("%d", &b[i].units);
        printf("Enter Lec Units: ");
        scanf("%d", &b[i].lect);
        printf("Enter Lab Units: ");
        scanf("%d", &b[i].lab);
    }
    *n += add_count; // 更新总记录数
    SaveFile(b, *n);

    return 0;
}

// 以下函数可自行补充实现
int search(struct Subject b[], int n) { return 0; }
int update(struct Subject b[], int n) { return 0; }
int del(struct Subject b[], int *n) { return 0; }
void exit_program() { printf("Exiting program...\n"); exit(0); }

内容的提问来源于stack exchange,提问作者Snuffles

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.03 16:20:26