[英]C Language - Printing array into text file
我正在嘗試使用C語言創建一個程序,該程序可以以數組結構(非並行)形式接收員工信息,並將其輸出到文本文件中。 該計划將比較工作代碼並計算員工的佣金率。 我有兩個問題:
1 - 即使我輸入TEL,SAL,SSR之一,salesPerson的佣金也總是為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
- 它只是被添加到輸出中 - 但我決定不刪除它。
好運,編碼!
始終查看編譯器的警告。 你有
printf("\n%d\n", e[0].commission);
在哪里給出整數格式說明符,但參數是double
。 它的前幾個字節可能為零 - 這就是結果打印為零的原因。 嘗試將其更改為:
printf("\n%.2lf\n", e[0].commission);
你將獲得美元和美分的佣金。 其他行也一樣。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.