Question
The language is C and explanation with codes would be appreciated. You need to write a function called addFile(char * filename, int * sum) that
The language is C and explanation with codes would be appreciated.
You need to write a function called addFile(char * filename, int * sum) that opens a file named filename. If it fails, return false, and DO NOT fclose(). You have to read the integers in the file, and store the sum. Further instructions are in the comments in the function in file fileint.c.
You also need to write a function called writeSum(char * filename, int sum) that writes the sum as an integer which name is filename. Further instructions are in the comments in the function in file fileint.c
Required codes as follows:
// *** | |
// *** You MUST modify this file. | |
// *** | |
#include | |
#include | |
#ifdef TEST_ADDFILE | |
bool addFile(char * filename, int * sum) | |
{ | |
// You cannot assume * sum is zero. Thus, * sum needs to be set 0 | |
// open a file whose name is filename for reading | |
// if fopen fails, return false. Do NOT fclose | |
// if fopen succeeds, read integers using fscan (do not use fgetc) | |
// | |
// * sum stores the result of adding all numbers from the file | |
// When no more numbers can be read, fclose, return true | |
// | |
return true; | |
} | |
#endif | |
#ifdef TEST_WRITESUM | |
bool writeSum(char * filename, int sum) | |
{ | |
// open a file whose name is filename for writing | |
// if fopen succeeds, write sum as an integer using fprintf | |
// fprintf should use one newline ' ' | |
// fclose, return true | |
// | |
return true; | |
} | |
#endif |
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started