943,788 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 6183
  • C++ RSS
Jan 27th, 2009
0

write() function...

Expand Post »
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..
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
azwraith69 is offline Offline
21 posts
since Jul 2008
Jan 27th, 2009
0

Re: write() function...

If you're talking about std::strings you could use the c_str() method?

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

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

C++ Syntax (Toggle Plain Text)
  1. ofstream tempfile("yourfilename_goes_here");
  2. std::string foo = "bar";
  3. tempfile << foo;
Moderator
Featured Poster
Reputation Points: 4142
Solved Threads: 394
Industrious Poster
Nick Evan is offline Offline
4,132 posts
since Oct 2006
Jan 28th, 2009
0

Re: write() function...

Click to Expand / Collapse  Quote originally posted by niek_e ...
If you're talking about std::strings you could use the c_str() method?

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

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

C++ Syntax (Toggle Plain Text)
  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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
azwraith69 is offline Offline
21 posts
since Jul 2008
Jan 28th, 2009
0

Re: write() function...

Click to Expand / Collapse  Quote originally posted by azwraith69 ...
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
Moderator
Featured Poster
Reputation Points: 4142
Solved Threads: 394
Industrious Poster
Nick Evan is offline Offline
4,132 posts
since Oct 2006
Jan 28th, 2009
0

Re: write() function...

C++ Syntax (Toggle Plain Text)
  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...
C++ Syntax (Toggle Plain Text)
  1. Hello, world!
  2. How are you?
  3. Super thanks!
Chris
Last edited by Freaky_Chris; Jan 28th, 2009 at 5:51 am.
Reputation Points: 325
Solved Threads: 118
Master Poster
Freaky_Chris is offline Offline
702 posts
since Apr 2008
Jan 28th, 2009
0

Re: write() function...

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!
Reputation Points: 10
Solved Threads: 0
Newbie Poster
azwraith69 is offline Offline
21 posts
since Jul 2008

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Unordered Hashmap implementation
Next Thread in C++ Forum Timeline: Sorting files on creation date





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC