Advertisement

an array of strings

Started by February 22, 2002 04:03 PM
10 comments, last by graeme 22 years, 10 months ago
fstream is the canonical example of why a) you shouldn''t use the old headers; and b) why you especially should never ever ever mix the old and new headers!

Want more details?
[C++ FAQ Lite] iostreams and cstdio
[vt.edu] New Style Issues
[Google] Old fstream, new fstream

[ GDNet Start Here | GDNet Search Tool | GDNet FAQ | MS RTFM [MSDN] | SGI STL Docs | Google! ]
Thanks to Kylotan for the idea!
quote: Original post by krez
Original post by Fruny
- If a function requires a const C string (<tt>const char*</tt>, or a litteral <tt>"string"</tt>, pass use <tt>string::c_str()</tt>. If the C function needs to modify the string (i.e. expects a char[] buffer), use <tt>std::vector<char> buf(size)</tt> and pass the address of the first element <tt>&buf[0]</tt> for the <tt>char*</tt>

or just use a char array…
quote: - Similarly, do not use <tt>iostream.h</tt>, it is the ''old'' iostream library, available for compatibility only. Use <tt>iostream</tt> instead.

out of curiosity, what is the difference (besides that namespace crap)? i hear this all the time but nobody ever says why (except for "it''s newer", which is hardly a valid reason).

Have a look at the examples given for using a char array and then use the modern C++ approach. The latter is much more elegant, expandable, shorter and creates much less scope for errors. Why would you want to have to worry about buffer overruns, dynamic memory and array sizes when you don''t have to? There may be minor speed difference (which there won''t be if you put in all the proper error handling code), but unless your entire program consists of using a string vector I find it very hard to believe it will be a bottle neck.

The header issue is that, for this example, "#include <iostream>" puts all the identifiers in the file in the ::std namespace whereas "#include <iostream.h>" does not. In the same was you encapsulate related parts of code together in classes and files, namespaces do the same.

This topic is closed to new replies.

Advertisement