fileHandling guide...
May 17, 2010 at 9:02pm UTC
hi,
I've problem in file handling when i instantiate object thorough fstream ... then file does not open or created... and then through exception my problem close..
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125
#include"iostream"
#include"fstream"
#include"cstdlib"
#include"ClientData.h"
#include"iomanip"
#include <stdio.h>
using namespace std;
void outputline(ostream&,const ClientData&);
void main(){
int accountNumber;
char lastName[15];
char firstName[10];
double balance;
fstream outCredit;
outCredit.open("credit.txt" ,ios::out|ios::in|ios::binary);
// problem is here.... file does not open...and then program will exit...
if (!outCredit)
{
cerr<<"file could not be openened." <<endl;
exit(1);
}
//outCredit.clear();
cout<<"Enter account number" ;
ClientData client;
cin>>accountNumber;
while (accountNumber>0 && accountNumber <=100 )
{
cout<<"enter lastname, firstName and balance value" ;
cin>>lastName;
cin>>firstName;
cin>>balance;
client.setAccountNumber(accountNumber);
client.setLastName(lastName);
client.setFirstName(firstName);
client.setBalance(balance);
outCredit.seekp( (client.getAccountNumber()-1) * sizeof (ClientData) );
outCredit.write(reinterpret_cast < const char * >(&client),sizeof (ClientData) );
cout<<"enter accoutn number" <<endl;
cin>>accountNumber;
}
outCredit.close();
//ClientData obj;
ifstream inCredit("credit.txt" ,ios::in);
if (!inCredit)
{
cerr<<"file could not be openened." <<endl;
exit(1);
}
inCredit.read(reinterpret_cast <char * >(&client),sizeof (ClientData) );
while (inCredit && !inCredit.eof())
{
if (client.getAccountNumber()!=0)
outputline(cout,client);
inCredit.read(reinterpret_cast <char * >(&client),sizeof (ClientData) );
}
inCredit.close();
//remove("credit.txt");
}
void outputline(ostream &output,const ClientData &record)
{
output<<left<<setw(10)<<record.getAccountNumber()
<<setw(16)<<record.getLastName()
<<setw(11)<<record.getFirstName()
<<setw(10)<<setprecision(2)<<right<<fixed
<<showpoint<<record.getBalance()<<endl;
}
Last edited on May 18, 2010 at 11:42am UTC
May 17, 2010 at 9:31pm UTC
These are wrong:
1 2 3 4 5 6
#include"iostream"
#include"fstream"
#include"cstdlib"
#include"ClientData.h"
#include"iomanip"
#include <stdio.h>
They should be:
1 2 3 4 5 6
#include <iostream>
#include <fstream>
#include <cstdlib>
#include "ClientData.h"
#include <iomanip>
#include <cstdio>
May 18, 2010 at 11:21am UTC
still same error is showing ... on console screen ."file could not be openened."
May 18, 2010 at 12:02pm UTC
I think because you are opening the file for *reading* as well as writing that the file needs to already exist. I don't think it will create the file for you.
May 18, 2010 at 12:45pm UTC
then what should i do???
May 18, 2010 at 2:21pm UTC
My own experimentation revealed that if you append
ios::trunc in the open statement it will create the file but also erase it's contents.
You can check if open succeeded and if not create the file and reopen.
1 2 3 4 5 6 7
do {
outCredit.open("credit.txt" ,ios::out|ios::in|ios::binary);
if (!outCredit.is_open()){
ofstream out("credit.txt" ,ios::out|ios::binary);
}
}while (!outCredit.is_open());
Last edited on May 18, 2010 at 2:22pm UTC
Topic archived. No new replies allowed.