Hello World C++

Hello World C++ 

The obligation of hello world. 

The Hello world program is a simple computer that prints (or displays) the string “Hello, world!” or some variant thereof. It is typically one of the simplest programs possible in almost all computer languages, and often used as first program that it’s giving to beginners to compile as their first program.

include <fstream>
using namespace std;

int main()
 ofstream SaveFile("mycodinglab.txt");
 SaveFile << "Hello World, from mycodinglab.com!";
 return 0;
  1. #include <fstream> – You need to include this file in order to use C++’s functions for File I/O. In this file, are declared several classes, including ifstream, ofstream and fstream, which are all derived from istream and ostream.
  2. ofstream SaveFile(“mycodinglab.txt”);
  3. ofstream means “output file stream”. It creates a handle for a stream to write in a file.
  4. SaveFile – that’s the name of the handle. You can pick whatever you want!
  5. (“mycodinglab.txt”); – opens the file mycodinglab.txt, which should be placed in the directory from where you execute the program. If such a file does not exists, it will be created for you.
  6. SaveFile << “Hello World, from mycodinglab.txt”; (“<<”) is a predefined operator and put the text above in the file. As mentioned before, SaveFile is a handle to the opened file stream. So, we write the handle name, << and after it we write the text in inverted commas.
  7. SaveFile.close(); – As we have opened the stream, when we finish using it, we have to close it. SaveFile is an object from class ofstream, and this class (ofstream) has a function that closes the stream. That is the close() function. So, we just write the name of the handle, dot and close(), in order to close the file stream!

You must be logged in to post a comment.

Proudly powered by WordPress   Premium Style Theme by www.gopiplus.com
%d bloggers like this: