Wednesday, March 16, 2016

How To Read a Text File in C++

This simple example shows you how to load a text file the user enters as console input and print out the results to the screen.

#include <stdio.h>
#include <iostream>
#include <fstream>
using namespace std;

int main(int argc, char** argv) {

    //Get File    
    cout << "Please enter a file to open: ";
    //Filename string
    std::string filename;
    cin >> filename;
    printf ("Attempting to open %s\n", filename.c_str());
    //Make an ifstream construct with filename
    ifstream myfile(filename.c_str());
    //string to hold line contents
    string line;
    if (myfile.is_open())
        {
        while ( getline (myfile,line) ) //iterate through lines
            {
              cout << line << '\n';  //print line out
            }
        myfile.close(); //close when done
        }
    else cout << "Failed to find file";
    return 0;
}

Sample Console:

Please enter a file to open: test.txt
Attempting to open test.txt
Hello
World.
Press [Enter] to close the terminal ...

Enumerations In Java

Creating Enum Types

An enumeration is a data type that allows you to create variables with a list of predefined definitions. The variables created with specific Enum types must be equal to one of the predefined values in the list. They are constants, so the names in the enum are put in upper case. The following example creates an Enum type called "Color" which includes a predefined list of some colors.

public enum Color {
 RED, BLUE, GREEN, YELLOW, BLACK, WHITE, PINK
}
Watch Enum Video

Eclipse Shortcut

If you are using Eclipse you can create a new Enum by right clicking on the package you want to add an Enum to, selecting New, and clicking Enum.

Using The Enum

You can use the Enum like you would use any other variable. Declare the variable with the name of the Enum as the variable type, give the variable an identifier, and assign a value to it. The example below creates two Color variables, checks to see what the color is, and demonstrates what it looks like when you print the variable out.

//Creating Color Variables
Color myFavoriteColor = Color.BLACK;
Color herFavoriteColor = Color.BLUE;
//Checking With Conditional If-Then
if(myFavoriteColor == Color.BLACK){
 System.out.println("Your favorite color is Black!");
}
//Standard toString
System.out.println(myFavoriteColor);
Your favorite color is Black!
BLACK

Looping Through Enumerations

You can loop through all the values of an Enum with a for each loop using the .values() method which returns an array containing all possible values of the specified Enum. The example below loops through all the values in an Enum named Color and prints them to the console.

for(Color aColor:Color.values()){
 System.out.println(aColor);
}
RED
BLUE
GREEN
YELLOW
BLACK
WHITE
PINK

Multiple Enums In A File

It is possible to add multiple enums to a file by placing the Enums within a Class file.

public class Colors{
  public enum DarkColors {RED,BROWN,BLACK}
  public enum LightColors {YELLOW,PINK,WHITE}
  ....
}

You can then access the Enums with their class name followed by the enum name:

Colors myColor = Colors.DarkColors.BLACK;

Using Template Overrides in Joomla

When developing a Joomla template, there are many things you may want to override. It was easy for me to find documentation on overriding views, but there are many other things you can override as well (some of which were much harder for me to find documentation for). System images, views, jQuery, layouts, and plugins can all be overridden.


Overriding Layouts

Layouts are used in Joomla! to display things like icons, readmore, tags, etc. These are common things you will probably want to override if you're creating a custom template.
  • Navigate to your Joomla base directory and copy the file you want to override from layouts/whatever folder
  • Paste the file in your template folder/html/layouts 
For example:
joom/layouts/joomla/content/icons.php becomes joom/templates/mytemplate/html/layouts/joomla/content/icons.php
Now you can modify the file however you wish in the html folder of your template!


Overriding Views

Find the view you want, for example: joom/components/com_content/views/article/tmpl/default.php
Copy it to the html folder in your template: joom/templates/mytemplate/com_content/article/default.php
Note that you do not need to include the "tmpl" folder in the override html folder of your template


Overriding System Images

System images can be overridden by finding the image you want to override and placing your version in:
joom/templates/mytemplate/images/system/whatever.png


Overriding jQuery

jQuery can be overridden by placing the version of jQuery you want to use in the following folder of your template:
joom/templates/mytemplate/js/jui/jquery.min.js
Now whenever something calls JHtml::_('jquery.framework'); when your template is active it will load your version of jQuery.


Overriding Plugins tmpl

You may override most core system plugin templates by creating a new folder in your html directory with the name of the plugin as defined in the administrator language folder. For example, in joomla/administrator/language/en-GB there is a file called "en-GB.plg_content_pagenavigation.ini" - this means we must create a folder called "plg_content_pagenavigation" in our template's html folder. Copy the default view from the plugin you are overriding into your new folder.
/joom/templates/mytemplate/html/plg_content_pagenavigation/default.php
Thank you for Dean's Post on StackExchange for the plugin information

How to Determine If Your Windows PC is 32 or 64 Bit

There are many situations when you will need to know if your system is 32bit or 64bit. Some applications only run on 64bit systems for example. In windows, you can determine the architecture of your computer by following these instructions:


  • Right click on your start menu
  • Click "System
  • This will bring up information about your computer. Under "System Type" it will display if your computer is 32 or 64 bit.

Get Current Date/Time As String in Java

Watch Video

The simplest way to get the date and time as a String in Java is to import and use the following classes: Date and SimpleDateFormat. The Date class contains the current Date data and the SimpleDateFormat class allows us to format the date data in a manner we specify.

The following example prints out the current date and time formatted in US date/time notation.

import java.text.SimpleDateFormat;
import java.util.Date;

public class main {
 public static void main(String[] args) {

  SimpleDateFormat dateFormat = new SimpleDateFormat("MM/dd/y hh:mm:ss a");
  Date thisDate = new Date();
  System.out.println(dateFormat.format(thisDate));

 }

}
Sample Output10/26/2015 06:55:07 PM

Modifying The Date Format

You can modify the date format string when you create the SimpleDateFormat with any of the following:

  • MM - The numerical month of the year
  • dd - The numerical day of the month
  • D - The numerical day of the year
  • W - The numerical week of the month
  • w - The numerical week of the year
  • E - The text format day of the week abbreviation
  • EEEE - The full text format day of the week
  • MMMM - The text format month of the year
  • a - AM or PM
  • H - Numerical hour of the 24 hour day
  • h - Numerical hour as AM/PM from 1-12
  • m - Numerical minute in hour
  • s - Numerical second in minute

For example, you could modify the date format to print out the date in US text format:

SimpleDateFormat dateFormat = new SimpleDateFormat("EEEE MMMM dd, Y. h:mm a");
Date thisDate = new Date();
System.out.println(dateFormat.format(thisDate));
Sample OutputMonday October 26, 2015. 7:09 PM