Talk About Network

Google


Register and Login
Nick
Password
Register create new account Sign up is FREE and you can post replies, new topics, bookmark posts and more!
Recover lost password


Programming > C++ Moderated > Re: Dynamically...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 5 of 8 Topic 9582 of 9775
Post > Topic >>

Re: Dynamically read string

by Bob Lied <lied@[EMAIL PROTECTED] > May 9, 2008 at 09:28 AM

> howa wrote [2008-05-08 10:29 PM]:
> Are there any simple method to read string into dynamic array of
> string, e.g. char**
> 
> E.g.
> 
> char **c;
> 
> Since the number of line is unknown, how the dynamic allocation is
> done actually?

Instead of character buffers and pointers, use a vector of strings
(std::vector<std::string>).  Read input using an istream iterator
and append to the vector using std::copy and a back iterator.
Dynamic allocation is all done by the standard library.

Then, if you really need character pointers, you can use the vector
size to allocate an array of character pointers.  Iterate over
the vector: allocate a char[] buffer for each string; copy the string
into the buffer; and set a pointer in the array of character pointers.

-- 
      [ See http://www.gotw.ca/resources/clcm.htm
for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]
 




 8 Posts in Topic:
Dynamically read string
howa <howachen@[EMAIL   2008-05-08 21:29:23 
Re: Dynamically read string
Marcin Swiderski <sfid  2008-05-09 09:15:18 
Re: Dynamically read string
Maciej Sobczak <see.my  2008-05-09 09:16:01 
Re: Dynamically read string
Oncaphillis <oncaphill  2008-05-09 09:24:35 
Re: Dynamically read string
Bob Lied <lied@[EMAIL   2008-05-09 09:28:53 
Re: Dynamically read string
"Jim Langston"   2008-05-09 09:22:45 
Re: Dynamically read string
"Martin T." <  2008-05-09 09:21:07 
Re: Dynamically read string
Ulrich Eckhardt <dooms  2008-05-09 09:27:41 

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
tan12V112 Tue Jul 8 23:19:24 CDT 2008.