…in the punchbowl


A programming language nit…

Filed under: c++, language, nitpick, programming — Matt @ 9:39

Warning: geek content ahead.

When my nose is to the grindstone, I use C++ pretty frequently. One (honestly quite minor) thing about the language has been bothering me of late.

The standard library provided with C++ has a set of facilities (“classes” for those of you in the know) for writing things (primarily to files) called iostreams. It also provides a ‘string’ class to manage ‘strings’ of characters; like this sentence, for instance.

To open a file with an iostream (an fstream specifically) you have to provide a filename. Filenames, like all words, consist of a series (or string) of characters. As such, you might hope to provide the requisite file name in a string object…

But your hopes would be dashed. C++ is based on an earlier language, called (get ready for it) C. In C, strings of characters were handled by a special convention: you would “point” to a place in memory that held the series of characters, and a “null” value (zero) would mark the end of the string. In C++ parlance, the creatures that use this convention are called c-strs. Guess what format the fstream class expects of file names? Yup, the c-str.

Now, I don’t mind that fstream accepts c-strs. There are still an awful lot of them floating around, and if you’re mixing C (or C-style) code with C++ — the chances of which are excellent — you’ll appreciate that fstream does this. But why, oh why, does fstream not accept file names in the form of a string object? Why must I convert a C++ construct (the string object containing the file name) to a C construct (a c-str containing the file name) in order to use a C++ construct (the fstream)?

Poor form, in my book.



  1. nerd… 😉

    Comment by Kathy — 2006.06.30 @ 10:22

  2. Dispite all my efforts to avoid “all things BPA” in high school, I unfortunately now know exactly what you are talking about.

    Comment by david — 2006.06.30 @ 10:46

  3. David…????

    Comment by Matt — 2006.06.30 @ 12:26

RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

Blog at WordPress.com.

%d bloggers like this: