Click here to Skip to main content
15,887,746 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralRe: Read stream of bytes in a buffer as a String [modified] Pin
CodingLover9-Oct-07 19:05
CodingLover9-Oct-07 19:05 
GeneralRe: Read stream of bytes in a buffer as a String Pin
chandu0049-Oct-07 19:17
chandu0049-Oct-07 19:17 
GeneralRe: Read stream of bytes in a buffer as a String Pin
CodingLover9-Oct-07 19:28
CodingLover9-Oct-07 19:28 
GeneralRe: Read stream of bytes in a buffer as a String Pin
ktm TechMan9-Oct-07 20:56
ktm TechMan9-Oct-07 20:56 
GeneralRe: Read stream of bytes in a buffer as a String Pin
CodingLover9-Oct-07 21:05
CodingLover9-Oct-07 21:05 
GeneralRe: Read stream of bytes in a buffer as a String Pin
chandu00410-Oct-07 0:22
chandu00410-Oct-07 0:22 
QuestionRe: Read stream of bytes in a buffer as a String Pin
David Crow10-Oct-07 2:56
David Crow10-Oct-07 2:56 
AnswerRe: Read stream of bytes in a buffer as a String Pin
CodingLover10-Oct-07 19:16
CodingLover10-Oct-07 19:16 
Ok, here is my code. Sorry I don't use tags here, it gives a real mess to read the code correctly.


=============================================================================================================
#include <iostream>
#include <fstream>
#include <string>

using namespace std ;

ofstream filedata ;

// Packet content of initial 16 bytes
struct pac_cont
{
unsigned int des_list ;
unsigned int mem_ID ;
unsigned char dm_con ;
unsigned char ser_ID ;
unsigned short act ;
};

void dataExtract(int length, char *buffer)
{
struct pac_cont* p = (struct pac_cont*)buffer;

filedata.open( "RecordData.txt", ios::app ) ;

if(filedata.is_open())
{
filedata << "\t" << static_cast<int>(p->des_list) << "\t\t" << static_cast<int>(p->mem_ID)
<< "\t\t" << static_cast<int>(p->dm_con) << "\t\t" << static_cast<int>(p->ser_ID)
<< "\t\t" << static_cast<int>(p->act) << "\n";
filedata.close() ;
}

else
{
filedata << "Error in opening file RecordData.txt\n" ;
}
}


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()))
{
// 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 ;
}

============================================================================================================

I'll comment it as much as possible. First I read a binary file, and found how many stream of bytes are there. Actually length of those streams are not same.

Basically each stream contain three part, four bytes for part one, four bytes for part two and int value of that four bytes gives the next part bytes. The third part have 16 fixed bytes at the beginning of each. I've extract the data there using dataExtract() function, you can see that. Up to now it is ok.

Now I want to read the bytes in part three except first 16 bytes. There is a message, actually record from a online chat. I want to read it and write to a file. How can I do that.

Thanks.Smile | :)




I appreciate your help all the time...

QuestionRe: Read stream of bytes in a buffer as a String Pin
David Crow11-Oct-07 3:04
David Crow11-Oct-07 3:04 
AnswerRe: Read stream of bytes in a buffer as a String Pin
CodingLover11-Oct-07 17:48
CodingLover11-Oct-07 17:48 
GeneralRe: Read stream of bytes in a buffer as a String Pin
David Crow12-Oct-07 3:24
David Crow12-Oct-07 3:24 
QuestionHow to create "unistall" in setup project using VC++2003 Pin
TooShy2Talk9-Oct-07 17:15
TooShy2Talk9-Oct-07 17:15 
QuestionWhy my colcontaing method of datagrid doesn't work well? Pin
shakaqrj9-Oct-07 17:06
shakaqrj9-Oct-07 17:06 
AnswerRe: Why my colcontaing method of datagrid doesn't work well? Pin
chandu0049-Oct-07 18:51
chandu0049-Oct-07 18:51 
GeneralRe: Why my colcontaing method of datagrid doesn't work well? Pin
shakaqrj9-Oct-07 20:24
shakaqrj9-Oct-07 20:24 
GeneralRe: Why my colcontaing method of datagrid doesn't work well? Pin
shakaqrj10-Oct-07 20:59
shakaqrj10-Oct-07 20:59 
QuestionCapture video, using IEEE1394 Pin
dum9-Oct-07 16:49
dum9-Oct-07 16:49 
QuestionArray Lengths? Pin
Michael1019-Oct-07 16:20
Michael1019-Oct-07 16:20 
AnswerRe: Array Lengths? Pin
Naveen9-Oct-07 16:34
Naveen9-Oct-07 16:34 
GeneralRe: Array Lengths? Pin
Michael1019-Oct-07 16:38
Michael1019-Oct-07 16:38 
GeneralRe: Array Lengths? Pin
Naveen9-Oct-07 16:47
Naveen9-Oct-07 16:47 
GeneralRe: Array Lengths? Pin
Michael1019-Oct-07 16:49
Michael1019-Oct-07 16:49 
GeneralRe: Array Lengths? Pin
Naveen9-Oct-07 16:54
Naveen9-Oct-07 16:54 
AnswerRe: Array Lengths? Pin
David Crow9-Oct-07 16:43
David Crow9-Oct-07 16:43 
GeneralRe: Array Lengths? Pin
Michael1019-Oct-07 16:55
Michael1019-Oct-07 16:55 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.