Calculate the total from one file, output the average to another file and standard output using fopen() and fscanf_s()/fscanf()
Compiler: Visual C++ Express Edition 2005
Compiled on Platform: Windows Xp Pro with SP2
Header file: Standard
Additional library: none/default
Additional project setting: Set project to be compiled as C
Project -> your_project_name Properties -> Configuration Properties -> C/C++ -> Advanced -> Compiled As: Compiled as C Code (/TC)
Other info:
To do: Calculate the total from one file, output the average to another file and standard output
To show: How to use fopen() and fscanf()/fscanf_s() function to read from a file, process and write the result to another file
Create files named input.txt and output.txt and put them in C:. Fill in some integers in the input.txt for example as shown below.
23 12 33 10 4 6 44 31 7 50
Run the following program.
/* Program to calculate the average of a list of numbers. */
/* calculate the total from one file, output the average */
/* into another file */
#include <stdio.h>
/* for exit() */
#include <stdlib.h>
int main(void)
{
int value, total = 0, count = 0;
/* fileptrIn and fileptrOut are variables of type (FILE *) */
FILE * fileptrIn, * fileptrOut;
// For testing the files existence
errno_t err = 0, err1 = 0;
// For file names & paths
char filenameIn[50], filenameOut[50];
// Make sure you have already created the file that contains some integers
// at your desired location for this example C:\input.txt that contains 23 12 33 10 4 6 44 31 7 50
printf("Please enter an input filename (use path if needed):\n");
scanf_s("%s", filenameIn, 50);
// Make sure you have already created this empty file, for this example C:\output.txt
printf("Please enter an output filename (use path if needed):\n");
// scanf("%s", filenameOut);
scanf_s("%s", filenameOut, 50);
/* open files for reading, "r" and writing, "w" */
// fopen(filenameIn, "r"), use secure version here
// Test the files existence
err = fopen_s(&fileptrIn, filenameIn,"r");
err1 = fopen_s(&fileptrOut, filenameOut,"w");
if(err !=0 )
{
printf("\nError opening %s for reading.\n", filenameIn);
exit (1);
}
else
printf("\nOpening %s for reading is OK.\n", filenameIn);
if(err1 != 0)
{
printf("Error opening %s for writing.\n", filenameOut);
exit (1);
}
else
printf("Opening %s for writing is OK.\n", filenameOut);
/* fscanf() */
printf("\nThe data:\n");
// EOF != fscanf(fileptrIn, "%i", &value) but using a secure version here
while(EOF != fscanf_s(fileptrIn, "%i", &value))
{
printf("%d ", value);
total += value;
++count;
}
/* end of while loop */
printf("\nThe total: %d\n", total);
/* Write the average value to the file. */
/* fprintf() */
printf("\n\nCalculate the average...\n");
fprintf(fileptrOut, "Average of %i numbers = %f \n", count, total/(double)count);
printf("Average of %i numbers = %f \n\n", count, total/(double)count);
printf("Check also your %s file content\n", filenameOut);
if(fclose(fileptrIn) == 0)
printf("%s closed successfully\n", filenameIn);
if(fclose(fileptrOut) == 0)
printf("%s closed successfully\n", filenameOut);
return 0;
}
Output example:
Please enter an input filename (use path if needed):
C:\input.txt
Please enter an output filename (use path if needed):
C:\output.txt
Opening C:\input.txt for reading is OK.
Opening C:\output.txt for writing is OK.
The data:
23 12 33 10 4 6 44 31 7 50
The total: 220
Calculate the average...
Average of 10 numbers = 22.000000
Check also your C:\output.txt file content
C:\input.txt closed successfully
C:\output.txt closed successfully
Press any key to continue . . .
Try changing the integers in the input.txt and re-run the program, check the output.txt content.