fprintf和c

时间:2017-10-30 04:02:53

标签: c loops printf

我成功编写了这个在终端输出报告的程序。

但是,当我尝试将相同的报告打印到磁盘文件时,我似乎无法弄清楚如何保留' while'循环并获取所有报告以打印到磁盘文件。

我尝试过删除标题等内容。功能并将该语法放在'摘要'功能以及所有必要的&fbspf'那里的陈述。结果只是出现在磁盘文件输出中的一行。这是transactiondata.txt file

IMPORT     -25.19
EXPORT      35.41
EXPORT     100.25
IMPORT    -500.34
EXPORT     240.35
IMPORT    -134.56
EXPORT     459.56

我应该如何构建代码,以便获得磁盘文件中终端窗口中显示的相同输出?

磁盘文件输出应为look like this

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 Corporation\n");
    printf("550 Warm Sands Drive\n");
    printf("Palm Springs, CA 92262\n\n");

    printf("Type            Amount              Net\n");
    printf("----            ------              ---\n");
}

// *************************************************************
// Process - produces the detail lines of the report
// *************************************************************


void process (void)
{
    while(!feof(datafile))
    {
        fgets(type, 7, datafile);
        fscanf(datafile,"%f\n", &amount);
        absamount = fabs(amount);
        runningsum = amount + runningsum;
        printf("%s %15.2f %17.2f\n",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.2f\nTransactions processed: %d\n", total, count);

}

1 个答案:

答案 0 :(得分:0)

使用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,"%f\n", &amount);
        absamount = fabs(amount);
        runningsum = amount + runningsum;
        fprintf(of, "%s %15.2f %17.2f\n",type,absamount,runningsum);
        fprintf(stdout, "%s %15.2f %17.2f\n",type,absamount,runningsum); // to print to console

    count++;              // You could also use count = count + 1
    total = total + amount;
    }
    fclose(of);

}

至少我应该更加注意,我的上述逻辑几乎是一个带有多个开口的低效代码,如果将fopen和fclose调用移动到main(),服务器会更好。并且考虑到FILE * reportfile是全局的,除了使用 reportfile 而不是 更改fprintf调用之外,没有什么需要。

而不是使用那些fprintf调用open reopen close logic

此代码

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代码。