Question:
Problem Statement: File Handling in C/C++?
1970-01-01 00:00:00 UTC
Problem Statement: File Handling in C/C++?
Three answers:
SHer...
2009-11-23 21:51:09 UTC
include

#include

using namespace std;

int main()

{

int cost1,cost2,cost3,cost4,tot,percent,n,i;

string game;

ofstream output;

ifstream input;

output.open("Expenses.txt");

if(output.fail())

{ cout<<"Expenses file did not open for input please check it\n";

system("pause");

return 1;

}

cout<<"how many games are there? ";

cin>>n;

for(i=0;i
{

cout<<"Enter the game name: ";

cin>>game;

cout<<"Enter cost for game 1: ";

cin>>cost1;

cout<<"Enter cost for game 2: ";

cin>>cost2;

cout<<"Enter cost for game 3: ";

cin>>cost3;

cout<<"Enter cost for game 4: ";

cin>>cost4;

tot=cost1+cost2+cost3+cost4;

percent=tot/50000.*100.;

output<
if(percent>=80)

output<<"Very Expesive\n";

else if(percent>=60)

output<<"Expesive\n";

else if(percent>=50)

output<<"Less Expesive\n";

else if(percent>=40)

output<<"Not Costly\n";

else

output<<"Best\n";

}

output.close();

input.open("Expenses.txt");

if(input.fail())

{ cout<<"Expenses file did not open for input please check it\n";

system("pause");

return 1;

}

output.open("Expenses2.txt");

if(output.fail())

{ cout<<"Expenses2 file did not open for output please check it\n";

system("pause");

return 1;

}

cout<<"The expenses are\n";

for(i=0;i
{getline(input,game);

cout<
output<
}

output.close();

input.close();

system("pause");

return 0;

}
Bobdarshy
2009-11-22 04:00:54 UTC
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;

}
why so serious
2009-11-22 01:02:23 UTC
first you need to open the file for reading, make sure you put a try catch statement or a if statement to make sure that the file is there.. then you will use a bunch of if statements according to the expenses variable and cout the answers to the user..


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Continue reading on narkive:
Loading...