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

为何以下C语言代码中银行员工薪资未写入输出文件?

Fixing the Missing Salary Data Issue in Your C Code

Hey there! Let's break down why only the salary field isn't writing correctly to your output file—this is a super common pitfall with C arrays that's easy to fix once you spot it.

The Root Problem: Array Indexing in C

In C, arrays are 0-indexed—meaning the first element lives at index 0, the second at 1, and so on, up to n-1 for an array of size n.

Looking at your code, you declared struct data d[n]; but your loops run from i=1 to i=n. This means you're accessing elements outside the array's valid bounds (like d[n] when i hits n), which triggers what's called undefined behavior. The reason only the salary field is broken is that the memory layout for your struct means the sal array is the one getting corrupted by this out-of-bounds access.

The Fix

All you need to do is adjust your loops to use valid 0-based indexing. We'll keep the user-facing employee numbering starting at 1 so it's still intuitive for whoever's entering data:

Corrected Full Code

#include <stdio.h>
struct data {
    char ide[50], name[50], bname[50], brname[50], con[10], sal[30];
};
int main() {
    int n;
    printf("Enter number of employees: ");
    scanf("%d", &n);
    struct data d[n];
    
    // Loop from 0 to n-1 for valid array access
    for (int i = 0; i < n; i++) {
        printf("Enter data of employee %d\n", i + 1); // Show user-friendly 1-based numbering
        printf("1) ID: ");
        scanf("%s", d[i].ide);
        printf("2) NAME: ");
        scanf("%s", d[i].name);
        printf("3) SALARY: ");
        scanf("%s", d[i].sal);
        printf("4) CONTACT: ");
        scanf("%s", d[i].con);
        printf("5) BANK NAME: ");
        scanf("%s", d[i].bname);
        printf("6) BRANCH NAME: ");
        scanf("%s", d[i].brname);
    }
    
    FILE *fp;
    fp = fopen("dbms.txt", "w");
    // Same fix for the write loop
    for (int i = 0; i < n; i++) {
        fprintf(fp, "1) ID: %s\n2) NAME: %s\n3) SALARY: %s\n4) CONTACT: %s\n5) BANK NAME: %s\n6) BRANCH NAME: %s\n", 
                d[i].ide, d[i].name, d[i].sal, d[i].con, d[i].bname, d[i].brname);
    }
    fclose(fp);
    return 0; // Add this to comply with C standard for main()
}

Extra Tips for Improvement

  • If you ever need to perform calculations on salaries (like adding bonuses), consider using a numeric type like float or double instead of char[]. You'd use %f in scanf and fprintf for these types.
  • The scanf("%s") function stops reading when it hits a space or newline. If you need to handle names/bank names with spaces (like "John Doe" or "State Bank of India"), swap scanf for fgets()—just remember to strip the trailing newline character it captures.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.04.29 00:39:08