User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the C++ section within the Software Development category of DaniWeb, a massive community of 456,515 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,741 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C++ advertiser: Programming Forums
Views: 916 | Replies: 4
Reply
Join Date: Sep 2007
Location: Colombo, Sri Lanka
Posts: 184
Reputation: eranga262154 is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 2
eranga262154's Avatar
eranga262154 eranga262154 is offline Offline
Junior Poster

File open globally and write to it

  #1  
Sep 27th, 2007
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 ;
      }
Last edited by eranga262154 : Sep 27th, 2007 at 2:43 am.
“victory breeds hatred, the defeated live in pain; happily the peaceful live giving up victory and defeat” - Gautama Buddha
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Aug 2005
Location: near St Louis, Missouri, USA
Posts: 11,541
Reputation: Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of 
Rep Power: 40
Solved Threads: 972
Moderator
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Most Valuable Poster

Re: File open globally and write to it

  #2  
Sep 27th, 2007
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().
<<Freelance Programmer>> << Hobby Site>>
Signature links for sale. PM me for details
Reply With Quote  
Join Date: Aug 2007
Location: South Dakota
Posts: 993
Reputation: vmanes is a jewel in the rough vmanes is a jewel in the rough vmanes is a jewel in the rough vmanes is a jewel in the rough 
Rep Power: 6
Solved Threads: 97
vmanes's Avatar
vmanes vmanes is offline Offline
Posting Shark

Re: File open globally and write to it

  #3  
Sep 27th, 2007
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
Reply With Quote  
Join Date: Sep 2007
Location: Colombo, Sri Lanka
Posts: 184
Reputation: eranga262154 is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 2
eranga262154's Avatar
eranga262154 eranga262154 is offline Offline
Junior Poster

Re: File open globally and write to it

  #4  
Sep 28th, 2007
Originally Posted by Ancient Dragon View Post
That appears to be only part of your program because main() looks incomplete.


Yes, it is a part of the code.

Originally Posted by Ancient Dragon View Post
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?

Originally Posted by Ancient Dragon View Post
>>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.
“victory breeds hatred, the defeated live in pain; happily the peaceful live giving up victory and defeat” - Gautama Buddha
Reply With Quote  
Join Date: Sep 2007
Location: Colombo, Sri Lanka
Posts: 184
Reputation: eranga262154 is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 2
eranga262154's Avatar
eranga262154 eranga262154 is offline Offline
Junior Poster

Re: File open globally and write to it

  #5  
Sep 28th, 2007
Originally Posted by vmanes View Post
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.

Originally Posted by vmanes View Post
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.
“victory breeds hatred, the defeated live in pain; happily the peaceful live giving up victory and defeat” - Gautama Buddha
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb C++ Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the C++ Forum

All times are GMT -4. The time now is 3:56 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC