C 语言 - 将数组打印到文本文件中

标签 c text text-files

我正在尝试用 C 语言编写一个程序,该程序可以以数组结构(非并行)形式获取员工信息并将其输出到文本文件中。 该程序将比较职位代码并计算员工的佣金率。 我有两个问题:

1 - 即使我输入 TEL、SAL、SSR 之一,销售人员的佣金也始终为 0。是我的字符串比较有问题吗?

2 - 如何将结果输出到文本文件(在项目文件夹内)?

编辑 - 我已经弄清楚如何将我的文件打印到 txt 中。谢谢大家的帮助!

#include "stdafx.h"
#include <string.h>
#include <stdlib.h>

//job codes
char TEL[4] = "TEL";
char SAL[4] = "SAL";
char SSR[4] = "SSR";

//create a sales person structure
struct salesPerson
{
    char name[31];
    int salesID;
    char jobCode[4];
    double totalSales;
    double commission;
}e[10]; //make an array of 10 employees

int _tmain(int argc, _TCHAR* argv[])
{
    //get employee info
    for(int i=0; i<3; i++)
    {
        printf("\nEnter the sales person's name: ");
        scanf(" %s", &e[i].name);
        printf("\nEnter the sales person's ID: \n");
        scanf(" %d%*c", &e[i].salesID);
        printf("\nEnter the sales person's job code: \n");
        scanf(" %s", &e[i].jobCode);
        printf("\nEnter the total sales of the sales person: ");
        scanf(" %lf", &e[i].totalSales);

        //determine commission based on jobCode
        if(strcmp(e[i].jobCode, TEL) == 0)
        {
            e[i].commission = e[i].totalSales*0.02;
        }
        if(strcmp(e[i].jobCode, SAL) == 0)
        {
            e[i].commission = e[i].totalSales*0.05;
        }
        if(strcmp(e[i].jobCode, SSR) == 0)
        {
            e[i].commission = e[i].totalSales*0.07;
        }
        else
        {
            printf("\n----------");
        }
    }

    printf("\n%d\n", e[0].commission);
    printf("\n%d\n", e[1].commission);
    printf("\n%d\n", e[2].commission);

    //print stuff to txt file
    FILE *fpOut, *fpIn;
    /*
    if ((fpIn = fopen("c:\\temp\\salesEmployees.txt", "w")) == NULL)
    {
        printf("Error opening the file for processing\n\n");
    }
    else
    {
        fpOut = fopen("c:\\temp\\salesEmployees.txt", "w");
        for(int i=0; i<3; i++)
        {
            //fscanf(fpIn,"%[^\n]", &e[i].name);
            //fscanf(fpIn,"%f%*c", &e[i].salesID);

            fprintf(fpOut, "\nName: %s", e[i].name);
            fprintf(fpOut, "\nSalesID: %d*c ", e[i].salesID);
            fprintf(fpOut, "\nJob code: %s ", e[i].jobCode);
            fprintf(fpOut, "\nTotal sales: %.2lf", e[i].totalSales);
            fprintf(fpOut, "\nCommission earned: %.2lf", e[i].commission);
            fprintf(fpOut, "\n\n");
        }

        //fclose(fpOut);
    }*/

}

最佳答案

要回答问题的第二部分(关于程序崩溃和将文件保存在同一目录中):您打开同一个文件进行输入和输出,而无需在两者之间关闭;不知道为什么输入还在那里。假设(为简单起见)您只想将从键盘输入的内容保存到文件中,则可以删除对 fpIn 的所有引用。至于“将文件保留在同一文件夹中” - 只需使用相对路径。当您从 c:\my\dir 运行时,打开文件 salesOutput.txt 将导致其写入 c:\my\dir\salesOutput .txt.

完整且可工作的代码(一些更改以适应我的编译器设置...):

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

//job codes
char TEL[4] = "TEL";
char SAL[4] = "SAL";
char SSR[4] = "SSR";

//create a sales person structure
struct salesPerson
{
    char name[31];
    int salesID;
    char jobCode[4];
    double totalSales;
    double commission;
}e[10]; //make an array of 10 employees

int main(int argc, char* argv[])
{
    //get employee info
    int i;
    for(i=0; i<3; i++)
    {
        printf("\nEnter the sales person's name: ");
        scanf(" %s", &e[i].name);
        printf("\nEnter the sales person's ID: \n");
        scanf(" %d%*c", &e[i].salesID);
        printf("\nEnter the sales person's job code: \n");
        scanf(" %s", &e[i].jobCode);
        printf("\nEnter the total sales of the sales person: ");
        scanf(" %lf", &e[i].totalSales);

        //determine commission based on jobCode
        if(strcmp(e[i].jobCode, TEL) == 0)
        {
            e[i].commission = e[i].totalSales*0.02;
        }
        if(strcmp(e[i].jobCode, SAL) == 0)
        {
            e[i].commission = e[i].totalSales*0.05;
        }
        if(strcmp(e[i].jobCode, SSR) == 0)
        {
            e[i].commission = e[i].totalSales*0.07;
        }
        else
        {
            printf("\n----------");
        }
    }

    printf("\n%lf\n", e[0].commission);
    printf("\n%lf\n", e[1].commission);
    printf("\n%lf\n", e[2].commission);

    //print stuff to txt file
    FILE *fpOut;
    {
        if((fpOut = fopen("salesEmployees.txt", "w")) == NULL)
        {
          printf("Unable to open file - quitting\n");
          return -1;
        };
        int i;
        for(i=0; i<3; i++)
        {
            fprintf(fpOut, "\nName: %s", e[i].name);
            fprintf(fpOut, "\nSalesID: %d*c ", e[i].salesID);
            fprintf(fpOut, "\nJob code: %s ", e[i].jobCode);
            fprintf(fpOut, "\nTotal sales: %.2lf", e[i].totalSales);
            fprintf(fpOut, "\nCommission earned: %.2lf", e[i].commission);
            fprintf(fpOut, "\n\n");
        }
        fclose(fpOut);
    }
}

显然,添加额外的 I/O、输入错误检查等是可取的(也是必要的)——但这应该会让你超越“它不起作用,我不知道为什么”的地步。我确实想知道为什么销售 ID 的打印输出中有 *c - 它只是添加到输出中 - 但我决定不删除它。

祝编码顺利!

关于C 语言 - 将数组打印到文本文件中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20085695/

相关文章:

css 在一行中隔开文本但保持彼此对齐/保持左侧?

python - 如何读取xml文件的一些内容并将其写入文本文件?

java - 将控制台结果写入文本文件

c# - 在 C# 中解析带有文字的文本文件

c - c 中使用 TCP 的多用户聊天服务器。如何向多个客户端发送数据?

c - Realloc 覆盖数组的内容?

c# - SIM800L从c#向arduino发送数据的问题

c - 如何知道 Linux 系统调用是否可重启?

text - 大量文本内容时uilabel崩溃

python - 如何检查文本文件中的一行文本?