为什么薪水未在以下代码中显示在文件中?

发布于 2025-02-08 15:06:45 字数 1071 浏览 1 评论 0 原文

其他所有内容都显示在文件中,因为它应该是银行雇员的薪水。

#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];
    for (int i = 1; i <= n; i++) {
        printf("Enter data of employee %d\n", i);
        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");
    for (int i = 1; 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);
}

Everything else is displaying in file as it is supposed to be other than the salary of the bank employee.

#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];
    for (int i = 1; i <= n; i++) {
        printf("Enter data of employee %d\n", i);
        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");
    for (int i = 1; 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);
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

怪我入戏太深 2025-02-15 15:06:45

C中的数组indice从0而不是1开始,并且为数组声明指定的内容不是最大索引,而是元素数量。

因此,使用数组 struct 将导致OUT-OUT-OUT-OUT-OUT-OUT-OUT-OUT-范围访问。

n-1 ,而不是 1 to n 这样:

for (int i = 0; i < n; i++)

您应该从 0 循环到 可变长度数组)例如 struct Data D [n]; 可能无法与大数据大小一起使用,因为它可以在堆栈上分配有限的大小。您应该通过 malloc()而在堆上分配内存:

struct data *d = malloc(sizeof(*d) * n);
if (d == NULL) {
    /* allocateion failed */
    exit(1);
}

您应该添加 #include&lt; stdlib.h&gt; 使用 malloc()(和 exit())。
跳棋可能会说您应该使用 free()在使用后进行分配的内存,但是

Array indice in C starts from 0, not 1, and what to specify for array declaration is not the maximum index but the number of elements.

Therefore, your loop for (int i = 1; i <= n; i++) with the array struct data d[n]; will lead to out-of-range access.

You should loop from 0 to n-1 instead of 1 to n like this:

for (int i = 0; i < n; i++)

Also note that VLA (variable-length array) like struct data d[n]; may not work with large data size because it may be allocated on the stack with limited size. You should allocate memory on the heap via malloc() instead like this:

struct data *d = malloc(sizeof(*d) * n);
if (d == NULL) {
    /* allocateion failed */
    exit(1);
}

You should add #include <stdlib.h> to use malloc() (and exit()).
Checkers may say that you should use free() to deallocate allocated memory after use, but it may not be required in the real world just before end of program execution.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文