C++: Some more stuff

I thought it might be best to write some extra info on the stuff I wrote yesterday about reading and writing stuff from and to files.

First thing is that I started off with a header (*.h) file. For those who really don’t know C++ (pretty much like me then), these files are used to kind of ‘define’ the corresponding source files.
What we did in there was let whichever other file includes this one know that the source file that should come with this header file contains 1 class with 2 public functions, both returning void.

We then write those functions in the *.cpp file (which we should have named the same as the .h file, except change it’s extension from .h to .cpp). In this file we use className::functionName to indicate that this function we’re writing belongs the that previously (in the .h file) defined class. The ‘::’ being the scope operator, you can use it to tell what which variable belongs to within your code.

I also seem to have forgotten to post the actual main.cpp file, sorry for this, It was late and I was both rushed and tired, but here it is:

#include "filerw.h"

int main()
{
FileRW filerw;
filerw.read();

return 0;
}

We include our header (.h) file so that the compiler knows where to look for that class we instantiate.

Now this is where it gets kind of weird for me. I’m used to writing in C#, so that first line would create a warning that filerw was never instantiated and at runtime an exception that filerw is null. Here though this seems enough to declare and instantiate. We then call the read() function from the class we wrote and it should work.

Our application will return 0 to let everything involved know that it exited correctly, any other value would mean that our application crashed.

As you can see I don’t do anything with the write() function. We could, you could add a line write(“anything”); there and it should do what you expect (append “anything” to the end of the file, if the file didn’t exists yet, it should now have been created).

So I think this is all I can tell you right now, I’ll try to write some more once I’ve written some more code.

Advertisements

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 )

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s