Re: How to load a text file into a char **?
On Jul 19, 2:43 pm, "Daniel T." <danie...@earthlink.net> wrote:
James Kanze <james.ka...@gmail.com> wrote:
"Daniel T." <danie...@earthlink.net> wrote:
ampheta...@gmail.com wrote:
Is there a safe (unlikely to cause overflows or segfaults)
way to load a text file into a char ** array?
A char** array? Are you sure you got that right?
Maybe he needs to interface with some legacy C code.
(Otherwise, of course, std::vector< std::string > is the obvious
choice.)
A vector<string> would equate to a char* array, not a char** array.
You're right. I was thinking of the more usual case, and just
read it as a typo: an array that you "access" through a char**.
char[] char_array = "hello world";
char*[] char_ptr_array = { "a", "b", "c" };
char**[] char_ptr_ptr_array = ?
Maybe the OP just messed up a bit in his terminology?
Probably. Although there's an outside chance that he wanted an
array of lines, each line being an array of words, I suspect
that that's not really the case.
I thought of using getline, but it needs a fixed-length
string, and I don't know how many lines or characters per
line the file has.
The below is the same as what I had before, except the last
step of turning the vector<vector<char> > into a char** is
added.
Yes. I was basing my posting on yours, since you seemed to be
the only one posting with the correct approach.
Note that in this case, you cannot simply return &result[0],
and expect it to work. For obvious reasons, you must use
&result[0] before either tmp or result go out of scope. In
a larger application, the solution, I think would be to
create a class which contained these two members, contructed
the above in its constructor, and had a function to return
the char**. (It the needed type really is char**, as was
the case with openldap, you'll have to const_cast.)
I agree, but I say avoid the (multiple) const_casts by using a vector<
vector< char > > instead of a vector< string >.
That's a possibility. I think it depends. If the data really
is supposed to be immutable, and it's just the legacy interface
which doesn't use const, I prefer the const_cast, since it says
what I mean.
In my case (which by coincidence, showed up two days ago), I was
dealing with data from the client; pushing an std::vector<char>
out to the interface level, when I was logically dealing in
strings, didn't seem appropriate (although I find I often use
vectors of char, instead of strings, at the implementation
level).
Note that our solutions are remarkably similar. The only
difference is that I don't have to go through the extra step
to remove the const.
Great minds think alike:-). I wasn't criticizing your
suggestions, just completing them. I did think it important to
point out that even if the char** was imposed by some sort of
legacy interface, some sort of std::vector was still the
preferred option. I may prefer vector< string > and the
const_cast, while you prefer vector< vector< char > >, but the
difference is peanuts, compared to the difference between one of
those and char** or char *[].
--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orient=E9e objet/
Beratung in objektorientierter Datenverarbeitung
9 place S=E9mard, 78210 St.-Cyr-l'=C9cole, France, +33 (0)1 30 23 00 34