There is some prolem in the funciton modification below. Please checkit and tell me what is worng?

U

Unpopular

void directory::modification()//???????????
{
clrscr();

cout<< "\n\t @@@@@@ @@@@@ @@@@@ @@@@@@ @@@@@ @ @
@@@@@@ ";
cout<< "\n\t=====@ @ @ @ @ @ @@ @
@ =====";
cout<< "\n\t=====@@@@@@ @ @ @ @ @ @ @ @ @
@@@ =====";
cout<< "\n\t=====@ @ @ @ @ @ @ @@
@ @ =====";
cout<< "\n\t @@@@@@ @@@@@ @@@@@ @ @@@@@ @ @
@@@@@@ \n\n\n\n";
cout<<"I am inside the modification function before opening of
addressesFiile.";
getch();
long pn;
int n,i;
ifstream inFile;
ofstream outFile;

inFile.open("addressesFile");
if(!inFile)
{
cout<<"\nI am inside the modification function after opening
of addressesFiile. Checking now with if";
getch();
cout<<"\n File not found!";
outFile.close();
exit(-1);
}
outFile.open("new");
cout<<"\nI am inside the modification function after just creating
new";
getch();
n=test();
if(n==0)
{
cout<<"\nI am inside the modification function. new is
empty";
getch();
cout<<"\n The file is empty. ! ";
getch();
return;
}
cout<<"\nI am inside the modification function before entering the
while loop.";
getch();
int deleteMe = 0;
while(inFile.good())
{

inFile.read((char*)&obj,sizeof(obj));
outFile.write((char*)&obj,sizeof(obj));
cout<<"\nI am inside the modification function inside the while
loop and I have finished round "<<deleteMe+1;
deleteMe = deleteMe + 1;
getch();
}
cout<<"\nI am inside the modification function just outside
the while loop.";
inFile.close();
outFile.close();
outFile.open("addressesFile",ios::trunc);
inFile.open("new");
if(inFile.fail())
{
cout<<"\n Sorry! File not found !";
exit(-1);
}
char ch;
cout<<"\n Enter the Land Line Phone Number or the Contact :";
cin>>pn;
ch=cin.get();
cin.get(ch);
for(i=0;i<n;i++)
{
inFile.read((char*)&obj,sizeof(obj));
char d;
if(pn==landLinePhone)
{
//view1();
cout<<" Name : "<<obj.contactName<<"\n";
cout<<" Home Address : "<<obj.homeAddress<<"\n";
cout<<" EMAIL ADDRESS : "<<obj.email<<"\n";
cout<<" Cellular Phone Nmber : "<<obj.cellularPhone<<"\n";
cout<<" Land Line Phone Number : "<<obj.landLinePhone<<"\n
\n";

d=check("HOUSE PHONE NUMBER ");
if((d=='y') || (d=='Y'))
{
cout<<"\n Enter the new land line phone number of the
contact, please. :";
cin>>landLinePhone;
ch=cin.get();
cin.get(ch);
}
if(check("OFFICE PHONE NUMBER ")=='Y')
{
cout<<"\n Enter the new cellular phone number of the
contact, please. :";
cin>>cellularPhone;
ch=cin.get();
cin.get(ch);
}
if(check("Name of the Contact")=='y')
{
cout<<"\n Enter the name of the contact, please. : ";
cin.getline(contactName,20,'\n');
}
if(check("HOME ADDRESS")=='y')
{
cout<<"\n Enter the new Home Address of the contact,
please. :";
cin.getline(homeAddress,50,'\n');
}
if(check("EMAIL ADDRESS:")=='y')
{
cout<<"\n Enter the new E-mail Address of the contact,
please. :";
cin.getline(email,25,'\n');
}
}//the outer if ends here.
outFile.write((char*)&obj,sizeof(obj));
}
outFile.close();
inFile.close();
}
 
R

Ralph D. Ungermann

`Some problem' is rather vague. Anyway, here are some brief notes on
`what's wrong':
void directory::modification()//???????????
{
[...] -- many noisy lines ommited here and below
inFile.open("addressesFile");
outFile.open("new");

I'd guess, that file "new" is now empty and locked for writing in text mode.
n=test();
Hu?? What happens in test()?
if(n==0)
{
cout<<"\n The file is empty. ! ";
getch();
return;
Of course "new" is empty! So bye bye here?
while(inFile.good())
{
So let's do some _binary_ I/O on _text_ files -- good luck!
inFile.read((char*)&obj,sizeof(obj));
outFile.write((char*)&obj,sizeof(obj));
What is obj? There are _very_ strong restrinctions, if you want to use
it like that!
}

for(i=0;i<n;i++)
Hu, what's n? Ahh, I see, the result of your test() above -- weird.
And again, binary I/O on a text file, using undefined `obj':
inFile.read((char*)&obj,sizeof(obj));
Only cowards check return values -- real programmers post in c.l.c++ :)
if(pn==landLinePhone)
What's landLinePhone? Did you mean obj.landLinePhone?
{
cout<<" Name : "<<obj.contactName<<"\n";
cout<<" Home Address : "<<obj.homeAddress<<"\n";
cout<<" EMAIL ADDRESS : "<<obj.email<<"\n";
cout<<" Cellular Phone Nmber : "<<obj.cellularPhone<<"\n";
cout<<" Land Line Phone Number : "<<obj.landLinePhone<<"\n
Ever thought of providing a suitable operator <<()? Then you may write
cout << obj;
d=check("HOUSE PHONE NUMBER ");
check()? Sorry, but I'm getting tired now.

I think, switching to binary file mode will fix one of your `some problem'.
But you should improve your coding style. And if you need more help,
you'll have to post code, that compiles and runs.

-- ralph
 
J

James Kanze

`Some problem' is rather vague. Anyway, here are some brief notes on
`what's wrong':
I'd guess, that file "new" is now empty and locked for writing
in text mode.

First, until you test the status of outFile, you don't know
whether the open succeeded or not. If it didn't succeed, you
can't say anything about the file "new". If it did succeed,
then their is an empty file by that name---if the file existed
beforehand, you've emptied it, and if it didn't, you've created
it. (That's what the standard requires.) I can't see why it
would be locked, however.

And of course, this is NOT the way to read a file.

(I didn't even look at the code in the original posting; it was
too big and too much of a mess to be bothered with. So I don't
really know what the OP was trying to do. But looping on
"inFile.good()" is never correct.)
So let's do some _binary_ I/O on _text_ files -- good luck!
What is obj? There are _very_ strong restrinctions, if you want to use
it like that!

Like: it must be an array of characters (char or unsigned char).
The cast in question is a reinterpret_cast, and any time you
need a reinterpret_cast, you should ask yourself questions.
(Although if all he does is then write it with write(), it
almost certainly doesn't matter, since read will read it as an
array of characters, and write will write it as an array of
characters. But then, why isn't it defined as an array of
characters?)

And of course, as you point out, if you really want to do binary
I/O, you need to open the files in mode binary, and imbue the
"C" locale.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top