Hi all,
I want to use a function to write some values to a text file. That function is called from the main function. Every time when I can that function from main, the perviously written value is overwrite. That's true, it wont work.
So what I have done is open a file globally and use it inside the function. My question is at that time in main function the calling of it not looping. Only one time write values. What can be caused to this issue. Here is the basic view of it.
using namespace std ;
// Globale file to record exact data
ofstream filedata ;
struct pac_cont
{
unsigned int des_list ; // Destination list
unsigned int mem_ID ; // Member ID
unsigned char dm_con ; // Data/Message control
unsigned char ser_ID ; // Service ID
unsigned short act ; // Activity
};
void dataExtract(int length, char *buffer)
{
struct pac_cont* p = (struct pac_cont*)buffer;
// processing the buffer
filedata.open( "RecordData.txt" ) ;
if(filedata.is_open())
{
filedata << p->des_list << "\t" << p->mem_ID << "\t" << p->dm_con << "\t"
<< p->ser_ID << "\t" << p->act << "\n";
filedata.close() ;
}
else
{
filedata << "Wrong\n" ;
}
}
int main ()
{
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 ; // int value of 4 bytes
if(tmp_len <= 5000)
{
fileopen.read(buffer, tmp_len) ;
filerecord << ID << "\t\t" << tmp_len << "\t\t" << "-\n" ;
if(tmp_len > 16)
{
dataExtract(tmp_len, buffer) ;
}
else
{
cout << "Stream is in wrong length\n" ;
}
}
else
{
filerecord << ID << "\t\t" << tmp_len << "\t\t" << "Large Size\n" ;
fileopen.seekg(tmp_len,ios_base::cur);
}
ID++ ;
count_mess++ ;
}
cout << "Number of streams: " << ID << endl ;
cout << "Number of messages: " << (count_mess-1) << endl ;
fileopen.close() ;
filerecord.close() ;
cin.get() ;
return 0 ;
}