c++ read and write, correct?

I have the code below as my original code for writing a value in a file, but which caused a runtime error because of the write function :

 
ofstream fp(hlfile, ios::out);
if(fp.write(&HList[i][j][k].Offset, sizeof(AddType))<=0)
cout << "WRITE ERROR - Hlist.\n";

Open in new window


so I changed it to the code to the below to fix it:

std::ostringstream ss;
ss << HList[i][j][k].Offset;
char TempArray[sizeof (AddType)];
std::strcpy( TempArray, ss.str().c_str());
std::cout << TempArray << std::endl;
if(fp.write(TempArray ,sizeof AddType) <=0)
   cout << "WRITE ERROR - Hlist.\n";

Open in new window

Can someone who knows c++ tell by reading them if it seems that these two scritps do the same thing?.  Also the same for the output code:

 My original code was
fpl.seekg(offset, ios::beg);
  if(fpl.read(&offset, sizeof(AddType))<=0)
        cout << "READ ERROR - Hlist.\n";
  fpl.close();

Open in new window


and I changed it to :
 
char * TempArray1;

TempArray1 = new char [sizeof(AddType)];
fpl.seekg(offset, ios::beg);
       fpl.read(TempArray1 , sizeof(AddType));
       offset =atol (TempArray1);

Open in new window


fyi - TempArray1 is a one dimesional array of long

Need more info?

Thanks!
Christa
christamccAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

evilrixSenior Software Engineer (Avast)Commented:
Did you actually discover why the first code failed? I'd be tempted to figure out why that failed and fix it problem rather than just re-implement it using the hacky 2nd version. Unless you understand why it failed you make have just hidden rather than fixed a defect.
0
christamccAuthor Commented:
As far as I understand, the first one failed because write needs a char * _STR as the first argument, but the 'wrong code' passes a reference to  long:
&HList[i][j][k].Offset

Open in new window

 This is my assumption...  but please, experts, tell me if that assumption is wrong.

thanks
0
evilrixSenior Software Engineer (Avast)Commented:
it does, but you just cast it to char *

if(reinterpret_cast<char *>(fp.write(&HList[i][j][k].Offset), sizeof(AddType))<=0)

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Fundamentals of JavaScript

Learn the fundamentals of the popular programming language JavaScript so that you can explore the realm of web development.

christamccAuthor Commented:
Thanks evilrix, that seems to work!

Can you just confirm that we should just apply the same for the read?

thanks
0
evilrixSenior Software Engineer (Avast)Commented:
>> Can you just confirm that we should just apply the same for the read?
Yes
0
evilrixSenior Software Engineer (Avast)Commented:
I mean yes you should - and, yes I can confirm :)
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C++

From novice to tech pro — start learning today.