Reading Text file to an array

Started by
11 comments, last by brandonman 16 years, 9 months ago
Quote:Original post by antiquechrono
Quote:
3) Even strtok()'s own documentation says never to use it.

Where is your source?


Um, strtok()'s own documentation. If you're on a Unix-compatible system, try 'man strtok'. Otherwise, try googling that. Third hit for me.

Quote:(Emphasis mine)
BUGS
Never use this function. This function modifies its first
argument. The identity of the delimiting character is
lost. This function cannot be used on constant strings.


It's actually right there, literally, for many implementations.

Of course, there would be plenty of reason not to use it in C++ anyway: it's yet another function treating null-delimited sequences of text as somehow special, instead of supporting an actual string type.

Incidentally, the null-delimited approach to string representation is pretty brain-dead. Compared to the main not-a-real-string-type alternative - "raw Pascal strings", where a length count is stored at the beginning of the string, they are never any better algorithmically, prevent you from including nulls in the string text (which is useful for some applications), and generally speaking, save a whopping three bytes of memory. (It's possible to encode the length count as a variable length integer, squeezing things out even more, while adding at most constant overhead to each operation.) This string representation was chosen by the C language implementors back in the 70s, when those three bytes were important (and when optimizing compilers might have not gotten the same or better performance out of a loop over array indices as a loop while characters are non-zero). It's ancient and barbaric and really should be forgotten.

Advertisement
Quote:Original post by antiquechrono
Quote:Original post by rip-off
antiquechrono be aware that in your first code example you used a std::auto_ptr instance to hold the address of something allocated with new []. This is incorrect, as the std::auto_ptr destructor will call delete on this, not delete [] which is required.

Moral: never use new[]/delete[] Always use std::vector or boost::array.


Ugh, that is what I get for trying to be creative lol. Thanks for the heads up though.


By the way, a rough paraphrase from interviews with Bjarne Stroustrup:

Q. Why is there no std::auto_ptr equivalent for arrays?
A. That is what std::vector is for.

Although, std::vector allows some extra things that you don't always want. If saving a couple words of memory (by not remembering a size and capacity) is more important to you, consider boost::scoped_array. (The boost::array is different; it's not dynamic, and requires a compile-time size as a template argument. It exists for the purpose of getting around C++'s legacy rules for passing arrays between functions etc., letting you treat arrays like actual objects.)
antiquechrono, it doesn't seem to read it. I run that code, but i just get system("pause")'s thing: Press any key to continue... Nothing is printing.

This topic is closed to new replies.

Advertisement