Initially, File handling is difficult to understand but once you got the grip on the subject then it will be the piece of cake. In order to have the grip, you always need the end of thread. =)
Here is a working code of file handling. This code make the file at the instance and write the values in it, then read from the file and display them on the console.
# include <iostream.h>
# include <fstream.h>
# include <stdlib.h>
int main ()
{
ofstream outClientFile( "clients.txt", ios::out);
if(!outClientFile)
{
cout << "File caanot open" << endl;
exit(1);
}
cout << "Enter the account, name, and balance" << endl
<< "Enter end-of-file to end input. " << endl;
int account;
char name[30];
double balance;
while (cin>> account >> name >> balance)
{
outClientFile << account <<',' << name << ',' << balance << endl;
cout << "\n\n\nEnter the account, name, and balance" << endl
<< "Enter end-of-file to end input. " << endl;
}
ifstream inClientFile( "clients.txt", ios::in);
if(!inClientFile)
{
cout << "File caanot open" << endl;
exit(1);
}
cout << "Reading the record" << endl;
while (inClientFile >> account >> name >> balance)
{
cout << "Account: " << account <<'\n'
<< "Name: " << name << '\n'
<< "Balance: " << balance << '\n\n';
}
return 0;
}
Here is a working code of file handling. This code make the file at the instance and write the values in it, then read from the file and display them on the console.
# include <iostream.h>
# include <fstream.h>
# include <stdlib.h>
int main ()
{
ofstream outClientFile( "clients.txt", ios::out);
if(!outClientFile)
{
cout << "File caanot open" << endl;
exit(1);
}
cout << "Enter the account, name, and balance" << endl
<< "Enter end-of-file to end input. " << endl;
int account;
char name[30];
double balance;
while (cin>> account >> name >> balance)
{
outClientFile << account <<',' << name << ',' << balance << endl;
cout << "\n\n\nEnter the account, name, and balance" << endl
<< "Enter end-of-file to end input. " << endl;
}
ifstream inClientFile( "clients.txt", ios::in);
if(!inClientFile)
{
cout << "File caanot open" << endl;
exit(1);
}
cout << "Reading the record" << endl;
while (inClientFile >> account >> name >> balance)
{
cout << "Account: " << account <<'\n'
<< "Name: " << name << '\n'
<< "Balance: " << balance << '\n\n';
}
return 0;
}
0 comments:
Post a Comment