我成功地编写了这个程序,在终端中输出一个报告。
但是,当我尝试将相同的报告打印到磁盘文件时,我似乎无法弄清楚如何保留"while"循环并将所有报告打印到磁盘文件。
我已经尝试了一些事情,例如删除"header"函数并将该语法与所有必要的"fprintf"语句一起放在"摘要"函数中。这样做的结果只是磁盘文件输出中出现的一行。 以下是事务数据.txt文件:
IMPORT -25.19
EXPORT 35.41
EXPORT 100.25
IMPORT -500.34
EXPORT 240.35
IMPORT -134.56
EXPORT 459.56
我应该如何构建代码,以便获得与磁盘文件中终端窗口中显示的相同输出?
磁盘文件输出应如下所示
The MoneyMaking Corporation
550 Warm Sands Drive
Palm Springs, CA 92262
Type Amount Net
---- ------ ---
IMPORT -25.19 -25.19
EXPORT 35.41 10.22
EXPORT 100.25 110.47
IMPORT -500.34 -389.97
EXPORT 240.35 -149.52
IMPORT -134.56 -284.08
EXPORT 459.56 175.48
The net total for the month is $174.48
Transactions processed: 7
这是我的代码:
//C Libraries
#include <stdio.h>
#include <math.h>
//Global Variable Declarations
FILE *datafile; //disk file (for input)
FILE *reportfile; // report file (for output)
char type [7]; //Transaction Type: either IMPORT or EXPORt
float amount; //Transaction Amount
float absamount; //abs amount
float total; //Total
float runningsum; // End net balance at end of every transaction
int count; //Count of transactions
// Function Prototypes
void header (void);
void process (void);
void summary (void);
//*************************************************
//*************************************************
// M A I N F U N C T I O N
//*************************************************
//*************************************************
int main (void){
// Initialize accumulating variables
datafile = fopen("c:\class\mod5\examples\transactiondata.txt","r"); // Open input file
count = 0;
total = 0.00;
runningsum = 0.00;
// Produce Report
header();
process();
summary();
fclose(datafile);
system("pause");
return 0;
}
// *************************************************************
// Header - prints a header on the report
// *************************************************************
void header (void)
{
printf("The MoneyMaking Corporationn");
printf("550 Warm Sands Driven");
printf("Palm Springs, CA 92262nn");
printf("Type Amount Netn");
printf("---- ------ ---n");
}
// *************************************************************
// Process - produces the detail lines of the report
// *************************************************************
void process (void)
{
while(!feof(datafile))
{
fgets(type, 7, datafile);
fscanf(datafile,"%fn", &amount);
absamount = fabs(amount);
runningsum = amount + runningsum;
printf("%s %15.2f %17.2fn",type,absamount,runningsum);
count++; // You could also use count = count + 1
total = total + amount;
}
}
// *************************************************************
// Summary - prints the report summary (including accumulators)
// *************************************************************
void summary (void)
{
// Local Variables
float net; //net values
printf("nThe net total for the month is $%1.2fnTransactions processed: %dn", total, count);
}
使用 fprintf。
void process (void)
{
FILE *of = fopen("O_File", "a"); // reportfile the report file I guess
while(!feof(datafile))
{
fgets(type, 7, datafile);
fscanf(datafile,"%fn", &amount);
absamount = fabs(amount);
runningsum = amount + runningsum;
fprintf(of, "%s %15.2f %17.2fn",type,absamount,runningsum);
fprintf(stdout, "%s %15.2f %17.2fn",type,absamount,runningsum); // to print to console
count++; // You could also use count = count + 1
total = total + amount;
}
fclose(of);
}
至少可以说,我应该更加注意,我的上述逻辑几乎是一个具有多个开口的低效代码,如果将 fopen 和 fclose 调用移动到 main() 会更好。 考虑到文件 *报告文件是全局的,除了将 fprintf 调用更改为使用报告文件而不是 for 写入之外,不需要太多。
而不是使用这些 fprintf 调用打开重新打开关闭逻辑
此代码
int ofd = open("O_File", O_RDWR|O_APPEND);
close(1);
close(2);
dup(ofd);
close(ofd);
在调用下面的代码序列之前
// Produce Report
header();
process();
summary();
应该服务于目的,
man -a dup
P.S - 几乎是未经其他平台测试的 Linux 代码。