[英]error in C++, what to do ?: could not find an match for ostream::write(long *, unsigned int)
I am trying to write data stored in a binary file using turbo C++. 我正在尝试使用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. 我想将一个4字节long
数据写入该文件。 When i tries to write data using char pointer. 当我尝试使用char指针写入数据时。 It runs successfully. 它运行成功。 But i want to store large value ie eg. 但是我想存储很大的价值,例如。 2454545454 Which can be stored in long
only. 2454545454只能long
存储。
I dont know how to convert 1 byte into bit. 我不知道如何将1字节转换为位。 I have 1 byte of data as a character. 我有1个字节的数据作为字符。 Moreover what im trying to do is to convert 4 chars into long and store data into it. 此外,即时通讯试图做的是将4个字符转换为long并将数据存储到其中。 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. 我正在实施隐写术,并且已成功将txt文件存储到图像中,但现在尝试检索该文件数据。
EDIT: 编辑:
I am using Turbo Complier. 我正在使用Turbo Complier。
Cast it to a char*
将其转换为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: 如果Tsize
是可寻址的,并且很长,您可以这样做:
fo.write(reinterpret_cast<char*>(&Tsize),sizeof(long));
I believe write takes a char* , not a long* 我相信write需要一个char *,而不是很长的*
ostream& write ( const char* s , streamsize n );
http://www.cplusplus.com/reference/iostream/ostream/write/ http://www.cplusplus.com/reference/iostream/ostream/write/
1) 1)
const long L = Tsize;
fo.write(reinterpret_cast<const char*>(&L),sizeof(L));
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. 首先,您不需要创建一个新的long
来存储Tsize。您可以指向已经拥有的Tsize。
Second, the write
call takes a stream of bytes. 其次, write
调用占用字节流。 What you need to do is to cast your pointer to char*. 您需要做的是将指针转换为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
. 我很确定static_cast适用于此处的指针,但是如果不起作用,则将代码更改为使用reinterpret_cast
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.