Sunday, May 18, 2008
Trick to track the argument in console
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).
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);
}
}