write() function...

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Jul 2008
Posts: 21
Reputation: azwraith69 is an unknown quantity at this point 
Solved Threads: 0
azwraith69's Avatar
azwraith69 azwraith69 is offline Offline
Newbie Poster

write() function...

 
0
  #1
Jan 27th, 2009
good day everyone.. thx for reading..

this is my problem:
i want to use the write() function, in ofstream, to write strings into a file.. but write() requires parameters of const char pointer and streamsize...
for(unsigned i=0; i != questions.size(); i++)
	{
		qptr = &questions[i]; 		//vector element(string)
		tempfile.write(qptr, stringsize);
	}

do i really need to convert string to char first? any simpler way?

and,, any better idea than using write()??

thx..
I am living in a mere program...
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 2,936
Reputation: niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute 
Solved Threads: 305
Moderator
Featured Poster
niek_e's Avatar
niek_e niek_e is offline Offline
Cenosillicaphobiac

Re: write() function...

 
0
  #2
Jan 27th, 2009
If you're talking about std::strings you could use the c_str() method?

(I'm assuming "questions" is a vector of std::strings)
  1. tempfile.write(questions[i].c_str(), questions[i].size());

But if tempfile is an ofstream, why not use the << operator?

  1. ofstream tempfile("yourfilename_goes_here");
  2. std::string foo = "bar";
  3. tempfile << foo;
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 21
Reputation: azwraith69 is an unknown quantity at this point 
Solved Threads: 0
azwraith69's Avatar
azwraith69 azwraith69 is offline Offline
Newbie Poster

Re: write() function...

 
0
  #3
Jan 28th, 2009
Originally Posted by niek_e View Post
If you're talking about std::strings you could use the c_str() method?

(I'm assuming "questions" is a vector of std::strings)
  1. tempfile.write(questions[i].c_str(), questions[i].size());

But if tempfile is an ofstream, why not use the << operator?

  1. ofstream tempfile("yourfilename_goes_here");
  2. std::string foo = "bar";
  3. tempfile << foo;
ofstream,, so i can't use the 1st one..

my strings in the vectors have white spaces.. i think << stops on white spaces,, correct me if i'm wrong,, so can't use that one..

thx for help..

any other ideas?? i'll try to use write() for now,, converting my strings to char.. but any better ideas will help..

thx
I am living in a mere program...
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 2,936
Reputation: niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute 
Solved Threads: 305
Moderator
Featured Poster
niek_e's Avatar
niek_e niek_e is offline Offline
Cenosillicaphobiac

Re: write() function...

 
0
  #4
Jan 28th, 2009
Originally Posted by azwraith69 View Post
i'll try to use write() for now,, converting my strings to char.. but any better ideas will help..
that's what option 1 does
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 671
Reputation: Freaky_Chris is a jewel in the rough Freaky_Chris is a jewel in the rough Freaky_Chris is a jewel in the rough 
Solved Threads: 113
Freaky_Chris's Avatar
Freaky_Chris Freaky_Chris is offline Offline
Practically a Master Poster

Re: write() function...

 
0
  #5
Jan 28th, 2009
  1. #include <fstream>
  2. #include <string>
  3.  
  4. int main(int argc, char *argv[]){
  5. std::string example = "Hello, world!\nHow are you?\n\tSuper thanks!";
  6. std::ofstream exfile("test.txt");
  7.  
  8. if(!exfile.good()) return 0;
  9. exfile << example;
  10.  
  11. exfile.close();
  12. return 0;
  13. }
I see no whitespace problem...
  1. Hello, world!
  2. How are you?
  3. Super thanks!
Chris
Last edited by Freaky_Chris; Jan 28th, 2009 at 5:51 am.
Knowledge is power -- But experience is everything
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 21
Reputation: azwraith69 is an unknown quantity at this point 
Solved Threads: 0
azwraith69's Avatar
azwraith69 azwraith69 is offline Offline
Newbie Poster

Re: write() function...

 
0
  #6
Jan 28th, 2009
ok,, thx for everything..

i thought << operator in ofstream is the same as << operator in cout... now i knew it doesn't stop at white spaces..

thx again!
I am living in a mere program...
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC