简体   繁体   English

C语言/如何从一个文件读取输入并将输出写入另一个文件

[英]C language / How to read input from one file and write an output to another file

I am having a problem with reading input from one file and write an output to another file.我在从一个文件读取输入并将输出写入另一个文件时遇到问题。

here is my code这是我的代码

#include <stdio.h>
#include <math.h>

//Variables declarations
FILE *reportfile;
FILE *inputfile;
char ratioName[20];
char nameorganization[25];
int asset1,asset2,asset3;
int lia1,lia2,lia3;
float asset;
float liabilites;
float ratio;
int ave_asset;
int ave_liabilites;
float ave_ratio;
char year[5]
//char currentasset[15];
//char currentLia[30];
//char tekstRatio[45];


//void
void ReadingData(void);
void DoCalcs(void);
void Report(void);


int main(void) {
    ReadingData();
    DoCalcs();
    Report();


 return 0;
}

void ReadingData(void){
    inputfile = fopen("c:\\class\\current.txt" , "r");
    fgets(nameorganization,25, inputfile);
    fscanf(inputfile,"%d%d\n", &asset1, &lia1);
    fscanf(inputfile,"%d%d\n", &asset2, &lia2);
    fscanf(inputfile,"%d%d", &asset3, &lia3);
   fclose(inputfile);
}

void DoCalcs(void){
    ratio = asset / liabilites;
    ave_asset = (asset1 + asset2 + asset3) / 3;
    ave_liabilites = (lia1 + lia2 + lia3) / 3;
    ave_ratio = ratio / 3;
}

void Report(void){
    reportfile = fopen("c:\\class\\alimbetm_cr.txt","w");
    fprintf(reportfile,"\n");
    fprintf(reportfile,"Current Ratio Report",ratioName);
    fprintf(reportfile,"Year");
    //fprintf(reportfile,"Current Asset",currentasset);


}


//void GettingInfo(void){
    //printf("Please type ratio: ");
    //scanf();
//}

when I run it , it saves file to new disk but removes old data, that is NOT what I want.当我运行它时,它将文件保存到新磁盘但删除旧数据,这不是我想要的。 What I want is read input/data from one file and write bot input/output to another file without removing input.我想要的是从一个文件读取输入/数据并将机器人输入/输出写入另一个文件而不删除输入。

This is input file data (current.txt)这是输入文件数据(current.txt)

Hi-Tech Leisure Products
47900       31007
34500        9100
57984       14822

This how it should be on a new file这应该如何放在新文件上

Hi-Tech Leisure Products
Current Ratio Report

                Current           Current          Current
Year            Assets            Liabilities      Ratio
----------------------------------------------------------
2010              47900             31007             1.54
2011              34500              9100             3.79
2012              57984             14822             3.91
----------------------------------------------------------
Average           46795             18310             3.08

This report produced by Raul Jimenez.

please help请帮忙

在这种情况下,您需要使用"a"而不是"w"因为write函数用于清除旧数据并写入新数据

The posted code does not compile!发布的代码无法编译! The first problem is this statement:第一个问题是这个语句:

char year[5] 

which is missing the trailing semicolon ;缺少尾随分号; . .

regarding:关于:

#include <math.h>

None of the 'features' of math.h are being used in the posted code.在发布的代码中没有使用math.h任何“功能”。 It is a very poor programming practice to include header files those contents are not being used.包含那些内容未被使用的头文件是一种非常糟糕的编程实践。 Suggest removing that statement.建议删除该语句。

regarding:关于:

reportfile = fopen("c:\\class\\alimbetm_cr.txt","w");

The mode w causes the output file to be truncated to 0 length.模式w导致输出文件被截断为 0 长度。

Since you want to keep the old contents of the output file and simply add more data.由于您想保留输出文件的旧内容并简单地添加更多数据。 Strongly suggest using;强烈建议使用;

reportfile = fopen("c:\\class\\alimbetm_cr.txt","a");

where the mode a will open the output file in append mode so the new data is added to the end of the existing file.其中模式a将以append模式打开输出文件,以便将新数据添加到现有文件的末尾。

Of course, always check reportfile to assure it is not NULL (IE the call to fopen() was successful.当然,始终检查reportfile以确保它不是 NULL(即对fopen()的调用成功。

Note this statement does not compile:请注意,此语句无法编译:

fprintf(reportfile,"Current Ratio Report",ratioName);

because it has a parameter but no matching 'output format conversion' specifier.因为它有一个参数但没有匹配的“输出格式转换”说明符。 Suggest (in this case) dropping the parameter: ratioName建议(在这种情况下)删除参数: ratioName

the calls to fopen() and fclose() are scattered all over the code.fopen()fclose()的调用分散在整个代码中。 As it is currently written, only one record will be read from the input file and only one record will be written to the output file.由于当前正在写入,因此只会从输入文件中读取一条记录,并且只会将一条记录写入输出文件。 This will be a major problem when the input file contains multiple records.当输入文件包含多条记录时,这将是一个主要问题。

the 'desired output' indicates that the first thing should be: "Hi-Tech Leisure Products" then: "Current Ratio Report" however, there is no statement (in Report() ) to actually output that second statement AND the char array ratioName[] is never set to any specific value. “所需输出”表示第一件事应该是:“高科技休闲产品”然后:“当前比率报告”但是,没有语句(在Report() )实际输出第二个语句和字符数组ratioName[]从未设置为任何特定值。

the 'desired output' indicates 2 lines of column headers, etc but there is no code to actually output those column headers ( other than year ). “所需输出”表示 2 行列标题等,但没有实际输出这些列标题的代码( year除外)。 Similar considerations exist for the data lines, the Average: line, the author line.数据行、 Average:行、 author行也存在类似的考虑。 Each datum of each line needs to be specifically output by the code, they will not 'magically' appear in the output file.每行的每个数据都需要由代码专门输出,它们不会“神奇地”出现在输出文件中。

regarding;关于;

ratio = asset / liabilites;

Neither asset nor liabilites is ever set to any specific value so they will be (due to where they are declared) containing the value(s) 0.0f. assetliabilites均未设置为任何特定值,因此它们将(由于它们的声明位置)包含值 0.0f。 So this division will result in a DIVIDE BY ZERO crash of the code.所以这种划分将导致代码的 DIVIDE BY ZERO 崩溃。

There are plenty more problems, but the above should get you started in the right direction.还有很多问题,但以上应该让你朝着正确的方向开始。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM