Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c++ > #6264 > unrolled thread

fstream - clobbering

Started byruben safir <ruben@mrbrklyn.com>
First post2011-06-06 08:16 +0000
Last post2011-06-06 06:10 -0400
Articles 2 — 1 participant

Back to article view | Back to comp.lang.c++


Contents

  fstream - clobbering ruben safir <ruben@mrbrklyn.com> - 2011-06-06 08:16 +0000
    Re: fstream - clobbering ruben safir <ruben@mrbrklyn.com> - 2011-06-06 06:10 -0400

#6264 — fstream - clobbering

Fromruben safir <ruben@mrbrklyn.com>
Date2011-06-06 08:16 +0000
Subjectfstream - clobbering
Message-ID<isi2c8$flj$1@reader1.panix.com>
I'm trying to figure out why the data file seems to be clobbered




 void Deck::add_questions(Question &enter){
	   //open file
	   Deck tmp;
	   std::ofstream all_quest( tmp.database.c_str(), std::ios::binary );
	   if(!all_quest){
	     std::cerr << "can't open question database for writing" << std::endl;
	     exit(1);
	   }
	   all_quest.seekp( 0, std::ios_base::end);
	   std::cout << "value of enter.quest ============> \n\n" << enter.quest << "Size of string\n" << sizeof(char) *enter.quest.size()  << std::endl;
	   all_quest.write(enter.quest.c_str(), sizeof(char) * enter.quest.size() );
	   all_quest.write(enter.answer.c_str(), sizeof(char) * enter.answer.size() );
	   all_quest.close();
	   return;
	   
   }




I don't think that std::ios::binary clobbers the file and  
all_quest.seekp( 0, std::ios_base::end);

should be setting the the put pos at the end of the file.


Ruben

[toc] | [next] | [standalone]


#6275

Fromruben safir <ruben@mrbrklyn.com>
Date2011-06-06 06:10 -0400
Message-ID<isi928$i22$1@reader1.panix.com>
In reply to#6264
On 06/06/2011 04:40 AM, Stefan Ram wrote:
> ruben safir<ruben@mrbrklyn.com>  writes:
>> I don't think that std::ios::binary clobbers the file and
>> all_quest.seekp( 0, std::ios_base::end);
>
>    »seekp« seem to be defined using »pubseekoff« in ISO/IEC
>    14882:1998(E), which is defined in terms of »seekoff«,
>    which is defined based on »::std::fseek«.
>
>    ISO/IEC 9899:1990 explains about »fseek«:
>
>        »A binary stream need not meaningfully support
>        fseek calls with a whence value of SEEK_END.«
>


sigh

I get the gist of this.  i finally got it to work by changing the code 
to the following


    void Deck::add_questions(Question &enter){
	   //open file
	   Deck tmp;
	   int pos;
	   std::ofstream all_quest( tmp.database.c_str(), 
std::ios::binary|std::ios::app );  <========== Change
	   if(!all_quest){
	     std::cerr << "can't open question database for writing" << std::endl;
	     exit(1);
	   }
	   all_quest.seekp( 0, std::ios_base::end);
	   pos = all_quest.tellp();
	   std::cout << "We are at Pos ==>" << pos << std::endl;

	   std::cout << "value of enter.quest ============> \n\n" << 
enter.quest << "Size of string\n" << sizeof(char) *enter.quest.size() 
<< std::endl;
	   all_quest.write(enter.quest.c_str(), sizeof(char) * 
enter.quest.size() );
	   all_quest.write(enter.answer.c_str(), sizeof(char) * 
enter.answer.size() );
	   all_quest.close();
	   return;
	
    }

but if I'm understanding what you wrote, there is no means to do this 
with portability.

Ruben

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.c++


csiph-web