Friday, May 22, 2009

How to read/write Excel in C++

It is not difficult to write/read Excel data in VB/VBA, the development language included with most Microsoft Office products. It's another story if wring such an application using C++.
It seems the most popular method is to use OLE (Object Linking and Embedding) interface. The advantage is that you can use most Excel functions such as formula, format. The disadvantage is obvious, the application relies on Windows.
A class to be used in environments other than Windows would be welcomed by many developers. BasicExcel is such an open-source project. However, as the name indicates, its function is BASIC. You cannot expect too much on complicated applications.

1. Comparison of methods of reading/writing EXCEL in C++ (chinese)
2. How to automate Excel from C++ without using MFC or #import
3. BasicExcel-A Class to Read and Write to Microsoft Excel

Tuesday, May 12, 2009

C++ I/O Tips 2 Input/Output

Tips and tricks for using C++ I/O (input/output)

How to prepare the output stream to print fixed precision numbers (3.40 instead of 3.4)
  std::cout.setf(ios::fixed, ios::floatfield);

How to set the width of a printing field
  Given: int one=4, two=44;
std::cout << one << std::endl.;
//output: "4"

std::cout << setw(2) << one << std::endl.;
//output: " 4"

std::cout << setw(2) << one << std::endl.;
//output: "X4"

std::cout << setw(2) << two << std::endl.;
//output: "44"

How to post source code in

when I tried to publish a post in which I can show code snippets, I encountered some difficulties:
(1)Pieces between arrow brackets can not be displayed because in such a case Blogger will try to automatically corrected the HTML code by closing all tags such as an C++ library import<iostream>.
(2)There is neither line indent nor highlight. The code is ugly and hard to read.
So, how to display source code in a good manner? I did some research online. Here are the steps I followed to give the source code a "makeup":

Step 1: Edit the html template
(Layout->Edit HTML)
Edit Use google-code-prettify,a Javascript module and CSS file, to allow syntax highlighting of source code snippets in an html page.
There are many other modules which can do the same job, such as SyntaxHighlighter (You can save them in your google page)
(1) Backup the current template.
(2) Add code to "head" tag.
In the "head" tag, link to the Javascript and CSS files in google prettify code
<link href="" rel="stylesheet" type="text/css"/>
<script src="" type="text/javascript"/>

(3)Customize the "pre" tag
pre {
margin: 5px 20px;
border: 1px dashed #666;
padding: 5px;
background: #f8f8f8;
white-space: pre-wrap; /* css-3 */
white-space: -moz-pre-wrap; /* Mozilla, since 1999 */
white-space: -pre-wrap; /* Opera 4-6 */
white-space: -o-pre-wrap; /* Opera 7 */
word-wrap: break-word; /* Internet Explorer 5.5+ */

(4)Add onload="prettyPrint()" to your document's body tag
- <body>
+ <body onload='prettyPrint()'>

(4)Save the new template.

Step 2: Format the code
(1) Download WebCodeFormatter which is a small Java app which replaces all the critical characters in your code with the appropriate unicode notations which is safer.
(2) Convert the source code and then copy it to your blog post.
(3) Write your code within
<pre class="prettyprint">
source code

1. "How to publish source code in"
2."Posting source code on Blogger"

C++ I/O Tip 1: How to skip comments

#include <limits> // for std::numeric_limits
#include <string> // for std::string
#include <iostream> // I/O
#include <fstream>// file I/O

// Remove C/C++ comments
ifstream& SkipComment(ifstream &strm)
char c;
const int c_MAX_LINESIZE=std::numeric_limits<int>::max();

if(!strm) return strm;


if(c=='#'||c=='%'||(c=='/' && strm.peek()=='/'))
//skip the rest of the line
strm.ignore(c_MAX_LINESIZE, '\n');
return strm;

else if (c=='/' && strm.peek()=='*')
//skip everything in the comment block
c=strm.get(); //skip the first '*'
char last='\0';
else last=c;
return strm;

else if(c!=EOF)

return strm;

//main program
int main(int argc,char **argv)
string IFile="test.dat";
ifstream ifs( IFile.c_str() );
if ( ! ifs ){cerr << "Unable to open " << IFile << " for reading\n";}
return 0;

TSP Bible....

My friend F recommended the following site, where over 300 papers on TSP, VRP are listed.
Nice website