Sunday, May 18, 2008

Trick to track the argument in console

On last week, I was working so hard to put in three features for the search program.

FEATURE LIST:

- Search path. This feature allows dynamically to search the desired path
- List. This is to show out the search result.
- Output. Export search result to text file.

The first two already done. Third feature still in progress.

During this experience project, I found a very interesting issue. How to track the input passing in through the command line? Lets take a look:

void main(int argc, char *argv[], char *envp[])

Initially I was using argc to track how many command was passing in. This is not really a good method of doing this because when passing in more and more options, it duplicate the same task. Refer to the code below:

switch(argc)
{
case 1: // no option was pass in
break;

case 2: // 1st option was pass in
break;

case 3: // 1st and 2nd option was pass in
break;

...
...

case n: // until n number of option was pass in
break;
}

Anyhow I have overcome this issue by tracking down the number of command passing in by using the argv pointer array. If argv[1] is empty, definitely I know that option number 1 is not there. Otherwise there must be an option passing in. For doing that, I just need a while loop to track every element in argv. But how do I know how many option was passing in? By using NULL checker? Actually I was using argc to get the total element in argv. I get this inspiration from MSDN.

Sunday, May 4, 2008

My first day in C++ .Net.

The first application that adopts this technology is my search file console program. The objective of this program is to retrieve all the files by searching through the particular location (including the remote media and network media).

I am having trouble at the first run. I mean every time I start something new, I’ll create a simple “Hello World” program to make sure everything are in place. “Something new” means this my first time in .Net development. The compiler that I am using is Express Edition 2005. At first I create an empty project from the general category. When I build the program by choosing “Start without Debugging” option, everything went fine. But it will prompt a message if I build using “Start Debugging” option.

The contents of the message are as below:

Debugging information for 'XXX.exe' cannot be found or does not match. Binary was not build with debug information.

This is not an error I guess, it just unable to retrieve the debug information. I do not know how to solve this problem, if you read this post and have any suggestion on this, do let me know. Thus I have to recreate the project by choosing “Win32 Console Application” under the Win32 category.

Now, the primary keys to deal with Files are DirectoryInfo, FileSystemInfo, and FileAttributes. The sample below will show all the files (not directory) in C drive.

DirectoryInfo ^di = gcnew DirectoryInfo("C:\\");

for each(FileSystemInfo ^fsi in di->GetFileSystemInfos())
{

if(fsi->Attributes != FileAttributes::Directory)
{
Console::WriteLine(fsi->Name);
}
}