
-----------------------------------
MysticVegeta
Sat May 28, 2005 11:16 pm

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  :)

-----------------------------------
wtd
Sat May 28, 2005 11:56 pm


-----------------------------------
Streams.

First, you have to include the header "fstream".

#include 

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.

std::ifstream input_file("info.dat");

Once we have that we can use it just like std::cin.  Let's say I want to read an int from the file.

int i;
input_file >> i;

Now... you shouldn't take it for granted that this'll work.  It may not.


int i;
input_file >> i;

if (input_file.fail())
{
   std::cerr > ch;
}

-----------------------------------
MysticVegeta
Sun May 29, 2005 1:51 pm


-----------------------------------
oh lol, int way is shorter  :lol:  
I am not at "voids" yet

-----------------------------------
wtd
Sun May 29, 2005 3:25 pm


-----------------------------------
oh lol, int way is shorter  :lol:  
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:

#include 

int main()
{
   char ch;
   std::cin >> ch;

   return 0;
}

#include 

void pause(std::istream& in = std::cin)
{
   char ch;
   in >> ch;
}

int main()
{
   pause();

   return 0;
}

Which main function tells you what the program is doing, rather than how it's doing it?

-----------------------------------
MysticVegeta
Sun May 29, 2005 4:45 pm


-----------------------------------
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.  :P 
About the voids again, I am not taking a course on C++, our school doesn't have one  :cry:

-----------------------------------
wtd
Mon May 30, 2005 2:04 am


-----------------------------------
About the voids again, I am not taking a course on C++, our school doesn't have one  :cry:

Well, I didn't take a course in C++ either.

Install a compiler and dive in.

-----------------------------------
MysticVegeta
Mon May 30, 2005 7:37 am


-----------------------------------
Install a compiler and dive in.
way to go! 

Also, will i be able learn the basics?
i am only 15.  :(

-----------------------------------
MysticVegeta
Mon May 30, 2005 7:40 am


-----------------------------------
You told me that we use voids just like procedures but i saw people using classes. What is the difference between them?

-----------------------------------
wtd
Mon May 30, 2005 11:42 am


-----------------------------------
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.

-----------------------------------
wtd
Mon May 30, 2005 12:09 pm


-----------------------------------
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.

std::string full_name(std::string first_name, std::string last_name)
{
   return first_name + " " + last_name;
}

int main()
{
   std::string first_name("Bob");
   std::string last_name("Smith");
   
   std::cout 