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 }
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
Overriding Layouts
- 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
Overriding Views
Overriding System Images
Overriding jQuery
Overriding Plugins tmpl
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 VideoThe 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





