【问题标题】:Numbers getting appended instead of getting added to an int variable while doing file operations in C在 C 中进行文件操作时,数字被追加而不是被添加到 int 变量中
【发布时间】:2017-12-23 01:47:53
【问题描述】:

我正在尝试将员工 ID 和他们完成的文件数存储在文本文件中。并将文件数添加到 totalFiles 变量中。但是当我输出 totalFiles 的值时,我看到我提供的所有附加输入都存储在文件中,而不是添加预期值。为什么会这样?

代码如下:

#include <stdio.h>

int main(void) {
    int n, i, numFiles;
    char empid[10];
    int totalfiles = 0;
    printf("Enter the number of employees\n");
    scanf("%d", &n);
    FILE *fp;
    fp = fopen("employee.txt", "w");
    if (fp == NULL) {
        printf("Error");
        return 0;
    }
    for (i = 0; i < n; i++) {
        printf("For employee %d\n", i + 1);
        printf("Enter the employee id\n");
        scanf("%s", empid);
        fputs(empid, fp);
        printf("Enter the number of files done\n");
        scanf("%d", &numFiles);
        totalfiles += numFiles;
        fprintf(fp, "%d", numFiles);
    }
    fclose(fp);
    printf("Total number of files done by all employees : %d", totalfiles);
}

我得到的输出是这样的:

输入员工人数
2
对于员工 1
输入员工编号
33
输入完成的文件数
8
对于员工 2
输入员工编号
20
输入完成的文件数
6
所有员工完成的文件总数:14338206

如果您看到输出,您会注意到输出只是我提供的输入,它们只是彼此相邻。请让我知道我的代码中的错误。提前致谢!

【问题讨论】:

  • 14 是(正确的)总和,为什么最后会回显其他值超出了您的代码范围。看起来,你的employees.txt 是以某种方式输出的?
  • 可能14是这段代码输出。 338206 其他部分读取文件并输出。如果提供的代码是实际代码,则无法复制。
  • @RahulPakhare 正如我和许多其他人已经评论过的,这无法用您在此处介绍的内容来解释。展示更多你的环境,也许这会对它有所启发
  • \n 添加到最后一个printf。在末尾添加return 0;,以免混淆操作系统。这个程序是从下一个回显文件的批处理作业中调用的吗?
  • 请从终端逐字复制粘贴您用于运行程序的命令行。

标签: c file pointers append


【解决方案1】:

我修改了一些代码以获得更好的格式输出,您将在文件“employees.txt”中包含系列“员工ID”“完成的文件数”

#include <stdio.h>

    int main(void) {
        int n, i, numFiles;
        char empid[10];
        int totalfiles = 0;
        printf("Enter the number of employees\n");
        if (scanf("%d", &n) != 1)
            return 1;
        FILE *fp;
        fp = fopen("employee.txt", "w");
        if (fp == NULL) {
            printf("Error");
            return 0;
        }
        for (i = 0; i < n; i++) {
            printf("For employee %d\n", i + 1);
            printf("Enter the employee id\n");
            if (scanf("%s", empid) != 1)
                return 1;
            fprintf(fp, "%s ", empid);
            printf("Enter the number of files done\n");
            if (scanf(" %d", &numFiles) != 1)
                return 1;
            totalfiles += numFiles;
            fprintf(fp, "%d\n", numFiles);
        }
        fclose(fp);
        printf("Total number of files done by all employees : %d\n", totalfiles);
        return 0;
    }

【讨论】:

  • 不,我想将每个员工完成的文件数写入文件,而不是文件总数
  • 我对代码进行了一些修改,以更好地格式化输出
【解决方案2】:

您的最终printf 语句没有换行符,它的输出没有与进一步的输出分开。 14 确实显示为输出,但紧随其后的是一些其他输出,可能是 employee.txt 文件的内容,可能作为命令文件的一部分输出。

更改您的代码以输出每条信息的换行符:

#include <stdio.h>

int main(void) {
    int n, i, numFiles;
    char empid[10];
    int totalfiles = 0;
    printf("Enter the number of employees\n");
    if (scanf("%d", &n) != 1)
        return 1;
    FILE *fp;
    fp = fopen("employee.txt", "w");
    if (fp == NULL) {
        printf("Error");
        return 0;
    }
    for (i = 0; i < n; i++) {
        printf("For employee %d\n", i + 1);
        printf("Enter the employee id\n");
        if (scanf("%s", empid) != 1)
            return 1;
        fprintf(fp, "%s\n", empid);
        printf("Enter the number of files done\n");
        if (scanf("%d", &numFiles) != 1)
            return 1;
        totalfiles += numFiles;
        fprintf(fp, "%d\n", numFiles);
    }
    fclose(fp);
    printf("Total number of files done by all employees : %d\n", totalfiles);
    return 0;
}

【讨论】:

  • @RahulPakhare:Paul Ogilvie 评论了一个关于运行环境的问题。我独立编写了一个完整的答案,其中包含详细的解释和代码的更正版本。这回答了你的问题吗?
  • 按照保罗所说的话,真的没什么可做的了。你已经告诉过要做同样的事情。
  • @RahulPakhare:Paul 建议您在末尾输出 \n 并添加 return 0;。这是基本问题,它可能帮助您编写了一个快速修复程序。我还建议您在每次输出后输出一个\n,这可能有用也可能没有用,并检查对scanf() 的不同调用的返回值。响应提示的无效用户输入将导致应避免的未定义行为。如果您在在线测试框架中运行代码,则可能存在有效性测试,如果没有这些检查,它将失败。对于大多数问题,一个完整的答案并不适合评论。
猜你喜欢
  • 2017-03-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-04-07
  • 2011-03-11
  • 2015-02-02
  • 2021-05-04
  • 1970-01-01
相关资源
最近更新 更多