0

Hi all,

I want to read a stream of bites/bytes as a String.

Here's what I have done up to now. Using a buffer I found that number of bytes, which include a text message from a binary file. Now what I want is to read those bytes stream as a String to get the message.

My issue is, use a buffer of size 5000. Some message use more than that. So I have to iteratively use the buffer cleaning after every read. But confusing, and worried about a simple way.

Thanks.

2
Contributors
5
Replies
6
Views
10 Years
Discussion Span
Last Post by Ancient Dragon
0

why not use std::string and you won't have to worry about the size because std::string will do that for you. Just call getline() to read the entire line.

0

Reason is that, the required byte stream is a part of a stream of bytes. To do earlier processes I used the buffer.

0

Most of the time there is no reason to clear the buffer on each iteration if you know the byte count of the characters read from the stream. Post a little code and maybe we (everyone here) can help simplify it.

0

Here is the main function, I want to read the buffer length equals to (tmp_len - 16) number of bytes. I think this is clear for you.

int main ()
{
	ifstream fileopen ;			
	ofstream filerecord ;
    char buffer[5000] ;
	unsigned int tmp_len = 0 ;	// Length of a stream
	int count_mess = 0 ;		// Count total message
	int ID = 1 ;				// Stream ID

// Open the file in binary format
	fileopen.open ( "G00046_002_01.srf", ios_base::binary ) ;

// Record the stream length
	filerecord.open ( "RecordLength.txt", ios_base::out ) ;

	if((fileopen.is_open()) && (filerecord.is_open()))
	{
	// Set the pointer to the begining of file
		fileopen.seekg (0, ios::beg) ;

	// Headers of the RecordLength.txt file
		filerecord << "Stream ID\tLength\t\tDescription\n\n" ;

	// Headers of the RecordData.txt file
		filedata.open ( "RecordData.txt", ios_base::out ) ;
		if(filedata.is_open())
		{
			filedata << "Destination List\t" << "Member ID\t" << "Data/Message\t" 
					 << "Service ID\t" << "Activity ID\n\n" ;
			filedata.close() ;
		}

		else
		{
			cout << "Error in opening file\n" ;
		}

		while(!fileopen.eof())
		{
			fileopen.read(buffer, 4);	// Dummy read of 4 bytes
			fileopen.read(buffer, 4) ;  // Read next 4 bytes

			tmp_len = *(unsigned int*)buffer ;  // Integer value of previous 4 bytes

			if(tmp_len <= 5000)
			{
				filerecord << ID << "\t\t" << tmp_len << "\t\t" << "-\n" ;

				if(tmp_len > 16)
				{
				// Procesing initial 16 bytes
					fileopen.read(buffer, 16) ;
					dataExtract(16, buffer) ;

				// Move the buffer pointer
					fileopen.seekg((tmp_len - 16), ios_base::cur) ;
				}

				else
				{
					cout << "Stream is in wrong length\n" ;
				}

			}

			else
			{
				filerecord << ID << "\t\t" << tmp_len << "\t\t" << "Large Size - More than 5000 bytes\n" ;

			// Processing intial 16 bytes
				fileopen.read(buffer, 16) ;
				dataExtract(16, buffer) ;

			// Move the buffer pointer
				fileopen.seekg((tmp_len - 16), ios_base::cur) ;
			}

			ID++ ;
			count_mess++ ;
		}

		cout << "Number of streams: " << ( ID - 1 ) << endl ;
		cout << "Number of messages: " << ( count_mess - 1 ) << endl ;
		fileopen.close() ;
		filerecord.close() ;
	}

	else
	{
		cout << "Error in opening files" << endl ;
	}

	cin.get() ;
	return 0 ;
}
0

You could handle it by dynamically allocating the buffer size and using another buffer that doesn't change size to read the initial 4 bytes, something like this:

char buf[4];
char * buffer = 0;

while( fileopen.read(buf, 4) )
{
     fileopen.read( &temp_len, sizeof(temp_len) );
     if( temp_len < 1)
     {
            cout << "error\n";
            break;
      }
      buffer = new char[temp_len+1];
      fileopen.read(buffer,temp_len);
      // now do something with the buffer contents
      delete[] buffer;
      buffer = 0;
}
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.