views:

146

answers:

4

I am trying to write data stored in a binary file using turbo C++. But it shows me an error

could not find an match for ostream::write(long *, unsigned int)

I want to write a 4 byte long data into that file. When i tries to write data using char pointer. It runs successfully. But i want to store large value i.e. eg. 2454545454 Which can be stored in long only.

I dont know how to convert 1 byte into bit. I have 1 byte of data as a character. Moreover what i m trying to do is to convert 4 chars into long and store data into it. And at the other side i want to reverse this so as to retrieve how many bytes of data i have written.

long *lmem;
lmem=new long;
*lmem=Tsize;
fo.write(lmem,sizeof(long));// error occurs here
delete lmem;

I am implementing steganography and i have successfully stored txt file into image but trying to retrieve that file data now.

EDIT:

I am using Turbo Complier.

+4  A: 

Cast it to a char*

long *lmem;
lmem=new long;
*lmem=Tsize;
fo.write(reinterpret_cast<char*>(lmem),sizeof(long));
delete lmem;

Or even better (as allocation on the stack is far faster and less error prone)

long lmem = Tsize;
fo.write(reinterpret_cast<char*>(&lmem),sizeof(long));

If Tsize is addressable and a long you could do this:

fo.write(reinterpret_cast<char*>(&Tsize),sizeof(long));
Yacoby
will it retain data that i have stored in long. i.e value that i have initialized ?
Shantanu Gupta
@Shantanu As long as the long variable is still in scope and the memory has not been deleted then the stored value will be the same before and after writing the value.
Yacoby
@Shantanu In other words, all type casting is doing is treating the same data as a different type. It doesn't modify the original data.
Yacoby
@Yacoby: thx for answer, i will get back in 15 min after checking whether my data is getting retained or not.
Shantanu Gupta
@Yacoby: will reinterpret_cast works in Turbo compiler. I am not working in VC++
Shantanu Gupta
@Shantanu As long as the Turbo C++ compiler is standards compliant then it should. reinterpret_cast/dynamic_cast/static_cast is the C++ version of the C style casts. In other words I would be very surprised if it didn't work.
Yacoby
@Yacoby: why should i use reinterpret_cast when i can directly cast long to char pointer. Any specific reason. If it is gud in performance or typesafe or any such reason ?
Shantanu Gupta
@Shantanu The "new-style casts" were introduced to give programmers a chance to state their intentions more clearly and for the compiler to catch more errors.
Yacoby
@Yacoby: thx, it means it uses the same concept as .NET framework provided.
Shantanu Gupta
@Yacoby: ur solution worked out in my problem.
Shantanu Gupta
A: 

I believe write takes a char* , not a long*

ostream& write ( const char* s , streamsize n );

http://www.cplusplus.com/reference/iostream/ostream/write/

Tom
@Tom yes u r rit. write takes char. But i m not typecasting it with fear of losing data. I dont know whether it will be able to retain that data as it is ?
Shantanu Gupta
A: 

1)

const long L = Tsize;
fo.write(reinterpret_cast<const char*>(&L),sizeof(L));
Alexey Malistov
A: 

There are several problems here.

First, you don't need to create an all new long to store Tsize in. You can make a pointer to the Tsize you already have.

Second, the write call takes a stream of bytes. What you need to do is to cast your pointer to char*.

Like this:

fo.write( static_cast<char*>(&Tsize), sizeof(Tsize) );

Third, writing one long like this is pretty inefficient. If you have a large array of long values that you want to write, use a single write call to get all of them, like:

fo.write( static_cast<char*>(array), sizeof(*array) * array_count );

I am pretty sure static_cast works for pointers here, but if it does not then change the code to use reinterpret_cast.

Zan Lynx
@Zan will this static_cast<char*> works in Turbo compiler ?
Shantanu Gupta
static_cast can't convert unrelated types (it works on parent/child relationships). It can't convert from long* to char*.
Mark B