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


Groups > comp.lang.c++ > #6275

Re: fstream - clobbering

From ruben safir <ruben@mrbrklyn.com>
Newsgroups comp.lang.c++
Subject Re: fstream - clobbering
Date 2011-06-06 06:10 -0400
Organization PANIX Public Access Internet and UNIX, NYC
Message-ID <isi928$i22$1@reader1.panix.com> (permalink)
References <isi2c8$flj$1@reader1.panix.com> <seekp-20110606103912@ram.dialup.fu-berlin.de>

Show all headers | View raw


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

Back to comp.lang.c++ | Previous | Next — Previous in thread | Find similar | Unroll thread


Thread

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

csiph-web