简体   繁体   中英

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)

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. 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.

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.

Of course, always check reportfile to assure it is not NULL (IE the call to fopen() was successful.

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

the calls to fopen() and fclose() are scattered all over the code. 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.

the 'desired output' indicates 2 lines of column headers, etc but there is no code to actually output those column headers ( other than year ). Similar considerations exist for the data lines, the Average: line, the author line. 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. So this division will result in a DIVIDE BY ZERO crash of the code.

There are plenty more problems, but the above should get you started in the right direction.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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