troubleshooting Question

c++ read and write, correct?

Avatar of christamcc
christamccFlag for United States of America asked on
6 Comments1 Solution310 ViewsLast Modified:
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";

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";
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(, sizeof(AddType))<=0)
        cout << "READ ERROR - Hlist.\n";

and I changed it to :
char * TempArray1;

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

fyi - TempArray1 is a one dimesional array of long

Need more info?

Senior Software Engineer (Avast)

Our community of experts have been thoroughly vetted for their expertise and industry experience.

Join our community to see this answer!
Unlock 1 Answer and 6 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 6 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros