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 Smile

Author:  wtd [ Sat May 28, 2005 11:56 pm ]
Post subject: 

Streams.

First, you have to include the header "fstream".

c++:
#include <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.

c++:
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.

c++:
int i;
input_file >> i;


Now... you shouldn't take it for granted that this'll work. It may not.


c++:
int i;
input_file >> i;

if (input_file.fail())
{
   std::cerr << "Somethin' bad happened." << std::endl;
   i = 42; // default value
}


And once we're done, let's close the file.

c++:
input_file.close();

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? Razz

Author:  wtd [ Sun May 29, 2005 1:25 pm ]
Post subject: 

c++:
std::ofstream output_file("out.txt");

if (output_file)
{
   output_file << 42 << " hello" << std::endl;
   output_file.close();
}
else
{
   std::cerr << "Something's broken.  I didn't do it!" << std::endl;
}

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.

c++:
void pause(std::istream& in = std::cin)
{
   char ch;
   in >> ch;
}

Author:  MysticVegeta [ Sun May 29, 2005 1:51 pm ]
Post subject: 

oh lol, int way is shorter Laughing
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 Laughing
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:

c++:
#include <iostream>

int main()
{
   char ch;
   std::cin >> ch;

   return 0;
}


c++:
#include <iostream>

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?

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. Razz
About the voids again, I am not taking a course on C++, our school doesn't have one Crying or Very sad

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 Crying or Very sad


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. Sad

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. Sad


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.

c++:
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 << full_name(first_name, last_name) << std::endl;

   return 0;
}


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.

c++:
struct name
{
   std::string first;
   std::string last;
};

std::string full_name(name& n)
{
   return n.first + " " + n.last;
}

int main()
{
   name bob;
   bob.first = "Bob";
   bob.last = "Smith";
   
   std::cout << full_name(bob) << std::endl;

   return 0;
}


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.

c++:
class name
{
   private:
      std::string first;
      std::string last;
   public:
      name(std::string f, std::string l)
      {
         first = f;
         last = l;
      }
};

std::string full_name(name& n)
{
   return n.first + " " + n.last;
}

int main()
{
   name bob("Bob", "Smith");
   
   std::cout << full_name(bob) << std::endl;

   return 0;
}


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.

c++:
class name
{
   private:
      std::string first;
      std::string last;
   public:
      name(std::string f, std::string l)
      {
         first = f;
         last = l;
      }

      std::string full_name()
      {
         return first + " " + last;
      }
};

int main()
{
   name bob("Bob", "Smith");
   
   std::cout << bob.full_name() << std::endl;

   return 0;
}


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.

c++:
class name
{
   private:
      std::string first;
      std::string last;
   public:
      name(std::string f, std::string l)
      {
         first = f;
         last = l;
      }

      std::string full_name()
      {
         return first + " " + last;
      }
};

int main()
{
   const name bob("Bob", "Smith");
   
   std::cout << bob.full_name() << std::endl;

   return 0;
}


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.

c++:
class name
{
   private:
      std::string first;
      std::string last;
   public:
      name(std::string f, std::string l)
      {
         first = f;
         last = l;
      }

      std::string full_name() const
      {
         return first + " " + last;
      }
};

int main()
{
   const name bob("Bob", "Smith");
   
   std::cout << bob.full_name() << std::endl;

   return 0;
}


Make sense? Smile

Author:  MysticVegeta [ Mon May 30, 2005 3:39 pm ]
Post subject: 

woah! six examples. Sure does make sense now Smile I will try to code some contests that i did with Turing now. Fun-time!!!! Smile

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:

c++:
class name
{
   private:
      std::string first;
      std::string last;
   public:
      name(std::string f, std::string l)
      {
         first = f;
         last = l;
      }

      std::string full_name() const
      {
         return first + " " + last;
      }
};

int main()
{
   const name bob("Bob", "Smith");
   
   std::cout << bob.full_name() << std::endl;

   return 0;
}


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.

c++:
class name
{
   private:
      const std::string first;
      const std::string last;
   public:
      name(std::string f, std::string l)
      {
         first = f;
         last = l;
      }

      std::string full_name() const
      {
         return first + " " + last;
      }
};

int main()
{
   const name bob("Bob", "Smith");
   
   std::cout << bob.full_name() << std::endl;

   return 0;
}


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.

c++:
class name
{
   private:
      const std::string first;
      const std::string last;
   public:
      name(std::string f, std::string l) : first(f), last(l) { }

      std::string full_name() const
      {
         return first + " " + last;
      }
};

int main()
{
   const name bob("Bob", "Smith");
   
   std::cout << bob.full_name() << std::endl;

   return 0;
}

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 Wink
Thanks a lot


: