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?
 
evilrixConnect With a Mentor Senior 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
 
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
Cloud Class® Course: SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

 
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.