ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
In C++, you cannot declare a buffer on the stack (as you have done) with a variably-sized value. In other words, the compiler will not know the value of lMsgLen; that will only be known at runtime.
Thus, what you need to do is allocate the buffer on the heap during runtime.
Btw, Hungarian Notation died miserably years ago. Hardly no one uses it anymore for numerous reasons, one being that it makes the code harder to read, and two that sometimes the developer(s) will change the data type of a variable, which then forces them to rename the variable.
P.S. Your Message class would look sleeker if you merely utilized an std::basic_stream<unsigned char> type, in lieu of the char*.
Code:
typedef std::basic_string<unsigned char> Message;
std::vector<Message*> messages;
// allocate buf
unsigned char* buf = new unsigned char[msgLen];
// read data into buf
iFile.read((char*) buf, msgLen);
if (iFile.gcount() == msgLen)
{
messages.push_back(new Message(buf));
}
...
// send messages
for (std::vector<Message*>::iterator it = messages.begin(); it != messages.end(); ++it)
{
send(sd, (*it).data(), (*it).size(), 0);
delete *it;
}
Last edited by dwhitney67; 01-11-2011 at 03:09 PM.
But the file contain 0 since it is binary file. So after the read method buff actually doesn't show 100 characters if 0 is present in the file. So what is the way to ignore 0 as a null terminating character and include 0 also to the buff.
The read() will not stop reading because of a 0x00 (or NULL) character. It will stop reading if the end-of-file has been reached, or if it has satisfied the request to read N bytes.
In the code snip that you provided, it has an error. The sizeof() a pointer (ie buff) is either 4-bytes (32-bit architecture) or 8-bytes (64-bit architecture).
If you know that you want to read a fixed-size of 100 bytes, then there is no need to allocate on the heap. Also, as for the call to read(), the second parameter should be 100. Please re-examine my previous post where I present examples of dynamic allocation.
I really thankful to you for your immense support. I know I'm asking stupid questions too much.Really sorry for that.
Actually 100 is not present in the code. I just used it here for the thread. I need to allocate memory dynamically since the lenght is given as a variable. I corrected the problem with the second parameter. But still it is same. File end is not reached and it is a large file actually. But it doesn't read the data correctly.
The o_Dump.read((char*)buff, lMsgLen); line read only two bytes. Third byte is a 0. That is why I thought the problem is with the terminating character.
What value does lMsgLen represent? Is it the sum of the size of the lMsgLen + the size of the message?
Did you want to include the message length in your buffer? (because you are)
Lastly, you stated that you are unable to read the file for whatever reason; well, certainly not using the code you posted. I suggest that from now on, that you post complete and compilable code. No excuses. Also, reading from the file and placing the data into a vector are two distinct operations; get the first part working, then worry later about the other.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.