I cannot tell exactly how the program should work because you did not provide enough information.
Regardless, I can help explain how to do simple file operations using C.
Remember to include the stdio.h library..
The following is used to open a file:
FILE* myfile = fopen("Expenses.txt", mode)
...where mode is a string to determine how the file should be opened:
r read text mode
w write text mode (truncates file to zero length or creates new file)
a append text mode for writing (opens or creates file and sets file pointer to the end-of-file)
rb read binary mode
wb write binary mode (truncates file to zero length or creates new file)
ab append binary mode for writing (opens or creates file and sets file pointer to the end-of-file)
r+ read and write text mode
w+ read and write text mode (truncates file to zero length or creates new file)
a+ read and write text mode (opens or creates file and sets file pointer to the end-of-file)
r+b or rb+ read and write binary mode
w+b or wb+ read and write binary mode (truncates file to zero length or creates new file)
a+b or ab+ read and write binary mode (opens or creates file and sets file pointer to the end-of-file)
Note that this function will return NULL if there is a failure opening the file for the mode specified.
Use int fgetc(FILE*) to get the current character at the file pointer and move the pointer forward.
Use int fputc(char, FILE*) to write the specified char at the file pointer and move the pointer forward.
Check out the source supplied for a good reference on C-language programming.
Here is an example of something similar to what you are doing:
//include
#include
int main(int argc, char *argv[]) {
//declare variables to be used
FILE* expfil1;
FILE* expfil2;
char* filname1 = "Expenses.txt";
char* filname2 = "Expenses2.txt";
int gamecount = 4;
double tmpexpense = 0.0;
double maxexpense = 50000.0;
double avgexpense = 0.0;
char tmpdat = 0;
int i = 0;
//attempt to open the 1st file to read existing data
expfil1 = fopen(filname1, "r");
if (expfil1 != NULL){
//open was successful
//attempt to open the 2nd file for writing
expfil2 = fopen(filname2, "a");
if (expfil2 == NULL){
printf("Unexpected error while opening the file %s for writing!\nClosing program...\n", filname2);
fflush(stdout);
return -1;
}
//copy the data 1 char t a time until End Of File reached or Error
tmpdat = fgetc(expfil1);
while (tmpdat != EOF){
if (fputc(tmpdat, expfil2) == EOF){
printf("Unexpected error while writing to the file %s!\nClosing program...\n", filname2);
fflush(stdout);
return -1;
}
tmpdat = fgetc(expfil1);
}
//attempt to close the 2nd file
if (fclose(expfil2) == EOF){
printf("Error closing file the file %s!\n", filname2);
fflush(stdout);
}
//attempt to close the 1st file
if (fclose(expfil1) == EOF){
printf("Error closing file the file %s!\n", filname1);
fflush(stdout);
}
}else{
//open failed
}
//attempt to open the 1st file for writing
expfil1 = fopen(filname1, "w");
if (expfil2 == NULL){
printf("Unexpected error while opening the file %s for writing!\nClosing program...\n", filname1);
fflush(stdout);
return -1;
}
//ask the user to input the 4 expenses
for (i=0; i
printf("Enter the expenses for game #%d: ", (i+1));
fflush(stdout);
//get the input as double data type
fflush(stdin);
if (scanf("%lf", &tmpexpense) == EOF){
printf("Error getting input from standard input stream!\n");
fflush(stdout);
}
fflush(stdin);
fflush(stdout);
//add to the expense tally
avgexpense += tmpexpense;
}
//average the expenses
avgexpense = avgexpense / gamecount;
//check what bracket the expense falls into
tmpexpense = avgexpense / maxexpense;
printf("Average expense per game is %.2Lf R\n", avgexpense);
printf("This is...");
if (tmpexpense >= 0.8){
printf("Very Expensive");
}else if(tmpexpense >= 0.6 && tmpexpense < 0.8){
printf("Expensive");
}else if(tmpexpense >= 0.5 && tmpexpense < 0.6){
printf("Less Expensive");
}else if(tmpexpense >= 0.4 && tmpexpense < 0.5){
printf("Not Costly");
}else{
printf("Good");
}
fflush(stdout);
//attempt to write the average expense to the 1st file
if (fprintf(expfil1, "%.2Lf\n", avgexpense) == -1) {
printf("Error writing to the file %s!\n", filname1);
fflush(stdout);
};
//attempt to close the file
if (fclose(expfil1) == EOF){
printf("Error closing file the file %s!\n", filname1);
fflush(stdout);
return -1;
}
return 0;
}