123
-=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- (c) WidthPadding Industries 1987 0|111|0 -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=-
Socoder -> C/C++/C#/Other -> C++ File Operations

Thu, 06 Aug 2009, 03:51
shroom_monk
OK, another C++ question from me...

I've been looking into doing file operations with C++, but I've hit a bit of a stumbling block. All the tutorials on the internet say include either <fstream> or <fstream.h>, but both of these are giving me trouble.

<fstream> just doesn't work - it includes, but I can't use any of the functions.

<fstream.h> works fine, but throws up a warning on compile that it is deprecated, and that <fstream> should be used.

Here's my sourcecode:


So what's the problem? I just don't get it...

-=-=-
A mushroom a day keeps the doctor away...

Keep It Simple, Shroom!
Thu, 06 Aug 2009, 04:38
JL235
Wikipedia suggests...

Thu, 06 Aug 2009, 05:03
Phoenix
All C++ standard headers lack the .h extension, so #include <fstream> is therefore correct. I think what you're missing is the namespace. To solve it, either put "using namespace std;" at the top or add "std::" in front of the fstream.
Thu, 06 Aug 2009, 05:15
shroom_monk
I've never quite understood the point of namespace... it simply makes the compiler assume you're using a certain set of functions, but when would this be useful?

Anyway, thanks for the tips. I'll give them a try now.

|edit| Yup, seems to be working now. Thanks! |edit|

-=-=-
A mushroom a day keeps the doctor away...

Keep It Simple, Shroom!
Thu, 06 Aug 2009, 05:24
Phoenix
Namespaces are there to avoid conflicts, and to structure your code.

Lousy example time! Let's say that you have a shirt class, and a menu class. Don't ask me how they fit in, perhaps you're making a virtual clothing store. They both need a button class, two different unrelated classes. So if you place the shirt button in a namespace called "shirt," and the other one in a namespace called "menu," then you can reference them as shirt::button and menu::button to distinguish between them. The standard library is placed in the namespace "std" so that there will never be any name conflicts with your code.

Additionally, namespaces can be used to show intentions. Something located in the "graphics" namespace will likely have something to do with graphics.
Thu, 06 Aug 2009, 05:33
shroom_monk
OK, but how does 'using namespace' fit into that, and why use that when you could just say shirt::button; rather than using namespace shirt; button; ?

-=-=-
A mushroom a day keeps the doctor away...

Keep It Simple, Shroom!
Thu, 06 Aug 2009, 05:38
cthug
using "namespace shirt; button();"

means "shirt::button()", it is a short(er) way of expressing it. But it has its problems like if you have two namespaces like:


-=-=-
CTHUG.co.nr
Get Ubuntu
Thu, 06 Aug 2009, 05:43
Phoenix
If you use shirt::button a lot, it does become pretty ugly. It looks much nicer if you can skip the namespace and the scope resolution operator (::). That's why we have the programmer's lazy shortcut, "using namespace xyz," for not having to write "shirt::" all the time. It cannot however used all the time, like cthug said, because there will be ambiguity if you use both button classes in the same file.
Thu, 06 Aug 2009, 16:57
Evil Roy Ferguso
Just to note, there is a much nicer way of using using than using namespace:



You can use using for individual items from other namespaces this way. Also remember that using are scoped, like anything else, and that



is also preferable to a global using namespace::std.

Also, never use using namespace something in the global scope in a header file -- people including your header will probably not want this, so use scoped usings or fully qualified names. If all of your code exists in its own namespace, though, you can do:



just fine.
Fri, 07 Aug 2009, 00:19
shroom_monk
OK, thanks for the explanation guys, I understand now. Thanks!

-=-=-
A mushroom a day keeps the doctor away...

Keep It Simple, Shroom!