Computer Science Canada File IO |
Author: | MysticVegeta [ Sat May 28, 2005 11:16 pm ] |
Post subject: | File IO |
I cant seem to find a tut on File Input and Output. I want to know how its done. Someone people put a tut in the tut section or reply here pls. Thanks ![]() |
Author: | wtd [ Sat May 28, 2005 11:56 pm ] | ||||||||||
Post subject: | |||||||||||
Streams. First, you have to include the header "fstream".
Now, for an input file we have the std::ifstream class. For output files, we have std::ofstream. We need to create an instance of either class to have a stream.
Once we have that we can use it just like std::cin. Let's say I want to read an int from the file.
Now... you shouldn't take it for granted that this'll work. It may not.
And once we're done, let's close the file.
|
Author: | MysticVegeta [ Sun May 29, 2005 1:01 pm ] |
Post subject: | |
Nice thanks i got it to work. How about to save the file? what the command for it? ![]() |
Author: | wtd [ Sun May 29, 2005 1:25 pm ] | ||
Post subject: | |||
|
Author: | MysticVegeta [ Sun May 29, 2005 1:37 pm ] |
Post subject: | |
It works! cool! 1 more question, After i do any programming in C++, I have to make 1 more variable, and then at the end i have to "cin" it to make the window not close itself after execution. Is there another way to do it by not creating an extra variable? |
Author: | wtd [ Sun May 29, 2005 1:44 pm ] | ||
Post subject: | |||
Not any particulaly elegant ways. That said, I suggest making it a separate procedure.
|
Author: | MysticVegeta [ Sun May 29, 2005 1:51 pm ] |
Post subject: | |
oh lol, int way is shorter ![]() I am not at "voids" yet |
Author: | wtd [ Sun May 29, 2005 3:25 pm ] | ||||
Post subject: | |||||
MysticVegeta wrote: oh lol, int way is shorter
![]() I am not at "voids" yet Function (and procedures or "void functions") should be among the first thing you master. They permit your code to be expressive. Consider the following two examples:
Which main function tells you what the program is doing, rather than how it's doing it? |
Author: | MysticVegeta [ Sun May 29, 2005 4:45 pm ] |
Post subject: | |
oh i see so they act like procedures and can be called just like turing. I like the File IO specially in C++, its so easy to learn and there is no "Bufferreader", "Printwriter" or whatever like Java, so its pretty straight-forward. ![]() About the voids again, I am not taking a course on C++, our school doesn't have one ![]() |
Author: | wtd [ Mon May 30, 2005 2:04 am ] |
Post subject: | |
MysticVegeta wrote: About the voids again, I am not taking a course on C++, our school doesn't have one
![]() Well, I didn't take a course in C++ either. Install a compiler and dive in. |
Author: | MysticVegeta [ Mon May 30, 2005 7:37 am ] |
Post subject: | |
wtd wrote: Install a compiler and dive in.
way to go! Also, will i be able learn the basics? i am only 15. ![]() |
Author: | MysticVegeta [ Mon May 30, 2005 7:40 am ] |
Post subject: | |
You told me that we use voids just like procedures but i saw people using classes. What is the difference between them? |
Author: | wtd [ Mon May 30, 2005 11:42 am ] |
Post subject: | |
MysticVegeta wrote: wtd wrote: Install a compiler and dive in.
way to go! Also, will i be able learn the basics? i am only 15. ![]() Sure. I probably would have learned this stuff a lot sooner, except that I didn't develop an interest in programming until 5 or 6 years ago. |
Author: | wtd [ Mon May 30, 2005 12:09 pm ] | ||||||||||||
Post subject: | |||||||||||||
MysticVegeta wrote: You told me that we use voids just like procedures but i saw people using classes. What is the difference between them?
A procedure in C++ is just a function that returns void. A class is a way of describing an object. What is an object, you ask? Well, an object groups a set of data with a set of relevant functions. In the example I like to use a lot, let's consider a name class. Now, let's do it the hard way.
Now here's the problem: in this case the first and last names are two entirely separate pieces of information. If I have several names I could mix them up. But there's a better way. I can tie these two pieces of data together, and make them a single entity.
But that's not quite right, either. We can change the individuals components of the name arbitrarily. We need to hide them; make them private. But if we do that, how do we set them in the first place? Well, we need a constructor.
Now, there's a new problem. The function which gives us a full name can't actually get at the components of the name. Of course, if it were a function inside the class, it would be able to see that information just fine.
Now, proper coding procedure in C++ says we should only ever have non-constant values when absolutely necessary. That way the compiler catches any unintended changes that might cause subtle bugs. So, let's make our name constant.
Now, we have a problem. When we create a class we have functions which can alter the object's data (in this case the individual component names) and functions which do not. The latter can be used even with a constant object, but the former cannot. Since full_name doesn't change anything (just creates a new string) we can specify that it's ok to use with a constant object.
Make sense? ![]() |
Author: | MysticVegeta [ Mon May 30, 2005 3:39 pm ] |
Post subject: | |
woah! six examples. Sure does make sense now ![]() ![]() |
Author: | wtd [ Mon May 30, 2005 4:01 pm ] | ||||||
Post subject: | |||||||
Ah, I left out one thing. Rather than the assignment style of construction, you should use the initialization style, as shown in the following modification of the previous code. A recap:
Now, remember how I said things should be constant unless otherwise required? Well, let's say that our name should have constant first and last names. It cannot be changed, under any circumstances.
Now, when we call the constructor those two strings get initialized by default to empty strings. Since they're constant, we can't assign to them after initialization, so this fails. Instead we directly initialize them.
|
Author: | MysticVegeta [ Mon May 30, 2005 4:57 pm ] |
Post subject: | |
Wow, i wonder if you type at 1000WPM or you already have tuts typed out (which i smarter thing i guess) Or if you are like a super nerd who breathes programming (no offense) Anyways, Really good job man 9 examples. whew ![]() Thanks a lot |