Sunday, January 13, 2008

Long MAX in C++

Yesterday I wrote a program to print long max in C++. In 32 bit systems it prints a very small value [ Refer the program ). I am wonder if I want to store a long number which is the most appropriate data type in C++.

#include < stdlib.h>
#include < iostream>
#include < limits>

using namespace std;

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

// Print the long max value
// 64 bit machine this prints -> 9223372036854775807
// 32 bit machine this prints -> 2147483647
cout << "Max value for long : " << numeric_limits::max() << endl;

return (EXIT_SUCCESS);
}

Wednesday, January 9, 2008

Configure printers in Ubuntu

It is not very hard to configure printers in Ubuntu. The easiest way is run "http://localhost:631". This will prompt the Common UNIX Printing System. You can add,remove and manage using this web console. Another way to do this in Xwindow system run System -> Administration -> Printing. This will prompt a window based printer management system.

Tuesday, January 8, 2008

Read Property File from JAVA

In most causes we need to keep our configuration information in a property file. This will increase the usability as well as maintainability of a project. This program demonstrate how to read a Java property file and how to extract values from that.

Sample property file: [ myprop.properties ]

name=Jeorge
age=17
country=Sri Lanka
city=Colombo

/*
* Main.java
*
* Created on January 8, 2008, 1:55 PM
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/

package blog2;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Properties;

/**
*
* @author eranga
*/
public class Main {

/**
* @param args the command line arguments
*/
public static void main(String[] args) throws FileNotFoundException, IOException, Exception {

if(args.length != 1){
throw new Exception("Please provide property file name as a command line parameter !");
}

String propertyFile = args[0];
File configPropsFile = new File(propertyFile);

if(!configPropsFile.exists())
throw new Exception("The config file : " + propertyFile + " does not exist.");

Properties myProp = new Properties();

myProp.load(new FileInputStream(propertyFile));

// Access properties
String prop1 = myProp.getProperty("name");
}

}

Monday, January 7, 2008

C++ Pre Processor Commands

When you write C++ programs ( Specially when error handling )you might need to get running file name, current execution line and system time. Following program demonstrate that.

//
// File: PreProcessorCommands.cc
// Author: eranga
//
// Created on January 7, 2008, 5:24 PM
//

#include
#include
using namespace std;

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

cout << "File Name : " << __FILE__ << endl;
cout << "Current Time : " << __TIME__ << endl;
cout << "Current Line : " << __LINE__ << endl;

return (EXIT_SUCCESS);
}