LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 01-11-2011, 03:07 PM   #16
dwhitney67
Senior Member
 
Registered: Jun 2006
Location: Maryland
Distribution: Kubuntu, Fedora, RHEL
Posts: 1,541

Rep: Reputation: 335Reputation: 335Reputation: 335Reputation: 335

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.
Code:
unsigned char* buf = new unsigned char[lMsgLen];
...

delete [] lMsgLen;

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.
 
1 members found this post helpful.
Old 01-12-2011, 01:03 AM   #17
coders123
LQ Newbie
 
Registered: Jan 2011
Posts: 14

Original Poster
Rep: Reputation: 0
Thanks a lot for your reply. But still I'm having new problems. I read the buffer as u told.

Code:
unsigned char* buff;
buff = new unsigned char [100];
o_File.seekg(lProcessedLen+4,std::ios::beg);
o_File.read((char*)buff, sizeof(buff));
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.

Thank you.

Last edited by coders123; 01-12-2011 at 01:05 AM.
 
Old 01-12-2011, 03:18 AM   #18
dwhitney67
Senior Member
 
Registered: Jun 2006
Location: Maryland
Distribution: Kubuntu, Fedora, RHEL
Posts: 1,541

Rep: Reputation: 335Reputation: 335Reputation: 335Reputation: 335
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.
 
Old 01-12-2011, 03:41 AM   #19
coders123
LQ Newbie
 
Registered: Jan 2011
Posts: 14

Original Poster
Rep: Reputation: 0
Hi,

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.

My actual code is as follows.

Code:
o_File.open("test",std::ios::in|std::ios::binary);

	if(!o_File.good())
	{
		return false;
	}
	else
	{
		long lMsgType = 0;
		long lMsgLen =0;
		while(!o_Dump.eof())
		{
			o_File.seekg(lProcessedLen+4,std::ios::beg);
			o_File.read(reinterpret_cast<char*>(&lMsgLen), sizeof(lMsgLen));

			unsigned char* buff;
			buff = new unsigned char [lMsgLen];
			o_File.seekg(lProcessedLen+4,std::ios::beg);
			o_File.read((char*)buff, lMsgLen);

			pMsg = New Message(lMsgType,lMsgLen,(char*) buff);
			if(pMsg != NULL)
			{
				vector_Messages.push_back(pBSEMsg);

			}
			lProcessedLen+= lMsgLen+4;
			lMsgLen = 0;
			lMsgType=0;
		}
           }
           o_File.close();
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.

Last edited by coders123; 01-12-2011 at 03:49 AM.
 
Old 01-12-2011, 06:16 AM   #20
dwhitney67
Senior Member
 
Registered: Jun 2006
Location: Maryland
Distribution: Kubuntu, Fedora, RHEL
Posts: 1,541

Rep: Reputation: 335Reputation: 335Reputation: 335Reputation: 335
What is lProcessedLen initialized to?

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.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] binary File reading in c swatihurde Linux - Software 4 08-08-2010 08:41 AM
Reading binary file in Python pwc101 Programming 4 04-28-2010 04:01 AM
Reading text file-writting binary file cdog Programming 5 06-13-2006 11:56 AM
Reading and Writing integers to binary file oulevon Programming 2 02-26-2006 12:27 AM
problem in reading Microsoft word as a binary file ljqu_happy Programming 15 02-02-2005 10:10 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:39 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration