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 ;
      }

That appears to be only part of your program because main() looks incomplete.

>>That's true, it wont work.
It will work if you use the correct flags. Add std::ate as the second argument to the open function. Read this for more information

>>Only one time write values
No idea because the code you posted does not write anything to the file in main() nor does main() ever call dataExtract().

If filedata is to be opened and closed in the dataExtract() function, it need not be a global, declare inside the function. As mentioned, use the appropriate file mode to set the writing action to occur at end of existing content (I'd use ios::app ).

Alternatively, you could open the file in main( ), pass the opened file handle to the function. Note that file handles MUST be passed by reference.

Why are you passing the data in a char* buffer? Why not just use your struct as the data type of the parameter?

Val

That appears to be only part of your program because main() looks incomplete.

Yes, it is a part of the code.

It will work if you use the correct flags. Add std::ate as the second argument to the open function. Read this for more information

Thanks for the link. Actually I've refer the same page to do this. When I used std::ate it wont work. I try to append the data using std::app. It works, but I think it is not efficient, because after writing each line close the file. Isn't it?

>>Only one time write values
No idea because the code you posted does not write anything to the file in main() nor does main() ever call dataExtract().

I'm not clear you. You say that my code is nothing done. If you think that better to put full code here, I'll do it.

If filedata is to be opened and closed in the dataExtract() function, it need not be a global, declare inside the function. As mentioned, use the appropriate file mode to set the writing action to occur at end of existing content (I'd use ios::app ).

Just go through my above post, I think it is not efficient at all.

Why are you passing the data in a char* buffer? Why not just use your struct as the data type of the parameter?

What's wrong there. Actually I've work with a binary file. Extract the streams of bytes, some of them are too short and some of them are too long. Using a buffer it is so easy, what I feel.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.