Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 File IO
Index -> Programming, C++ -> C++ Help
Goto page 1, 2  Next
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
MysticVegeta




PostPosted: 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
Sponsor
Sponsor
Sponsor
sponsor
wtd




PostPosted: Sat May 28, 2005 11:56 pm   Post subject: (No 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();
MysticVegeta




PostPosted: Sun May 29, 2005 1:01 pm   Post subject: (No subject)

Nice thanks i got it to work. How about to save the file? what the command for it? Razz
wtd




PostPosted: Sun May 29, 2005 1:25 pm   Post subject: (No 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;
}
MysticVegeta




PostPosted: Sun May 29, 2005 1:37 pm   Post subject: (No 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?
wtd




PostPosted: Sun May 29, 2005 1:44 pm   Post subject: (No 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;
}
MysticVegeta




PostPosted: Sun May 29, 2005 1:51 pm   Post subject: (No subject)

oh lol, int way is shorter Laughing
I am not at "voids" yet
wtd




PostPosted: Sun May 29, 2005 3:25 pm   Post subject: (No 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?
Sponsor
Sponsor
Sponsor
sponsor
MysticVegeta




PostPosted: Sun May 29, 2005 4:45 pm   Post subject: (No 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
wtd




PostPosted: Mon May 30, 2005 2:04 am   Post subject: (No 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.
MysticVegeta




PostPosted: Mon May 30, 2005 7:37 am   Post subject: (No 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
MysticVegeta




PostPosted: Mon May 30, 2005 7:40 am   Post subject: (No subject)

You told me that we use voids just like procedures but i saw people using classes. What is the difference between them?
wtd




PostPosted: Mon May 30, 2005 11:42 am   Post subject: (No 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.
wtd




PostPosted: Mon May 30, 2005 12:09 pm   Post subject: (No 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
MysticVegeta




PostPosted: Mon May 30, 2005 3:39 pm   Post subject: (No 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
Display posts from previous:   
   Index -> Programming, C++ -> C++ Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 2  [ 17 Posts ]
Goto page 1, 2  Next
Jump to:   


Style:  
Search: