I wrote a white space stripper program. I looked around on the internet, and couldn’t find one. So I wrote my own, and thought I’d share it with other’s.
My motivation in using this is to look at files after the PreProcessor looks at them. So it merely takes out the blank lines made of pure newline characters. Blocks of spaces and tabs are all left in.
Usage:
Run the application (console app) and follow the instructions. Easy enough?
By the way, I’m including all of the project files. (VC++ .net 2002)(VC 7.0 project files)
If anyone finds anything to fix, by all means don’t keep it a secret.
You want to be very careful with fixed length buffers when it comes to unbound I/O - basically don’t use 'em as their prime targets for code exploits. Either use a heap based approach or parse the file char by char.
If you where doing this in Managed C++ it might be:
#include "stdafx.h"
#using <mscorlib.dll>
using namespace System;
using namespace System::IO;
int _tmain()
{
try
{
TextReader* r = File::OpenText(Environment::GetCommandLineArgs()[1]);
String* s = 0;
while((s = r->ReadLine()) != 0)
if(s->Length > 0)
Console::WriteLine(s);
}
catch(Exception* e)
{
Console::WriteLine(e);
}
return 0;
}
Just call it with the .i file as the first parameter and redirect the output, i.e.:
The above example isn’t complete. You could add a “/?” option to print usage and you should probably check the number of args is at least 2, but it does avoid the cardinal sin of fixed length buffers.
Nice. That’s why I uploaded the source files, because I believe that my method wasn’t all that great. I knew my fixed length buffer was old school, but I didn’t know how to do it otherwise.
Your methods seem to utilize Managed C++. It kind of looks like .NET, is it?
I just implimented your method using a .NET console app. I just plugged your code into main from a new project wizard, and it worked like magic. Amazing!!
I wonder now if you had that code laying around, or you thought it up on the spot? Also I didn’t know about the redirection operator. That is nice. I guess it goes to show, no matter how much you know, you can always learn more.
This .NET stuff appears to work well. I wonder what resources are best for learning it. Is a book best? Or are the help files any good? Or maybe some website?
This is the sort of thing scripting languages (Perl especially) excel at - I would be very suprised if there was a big speed difference in this case. And so much simpler!
Let the record show, m lord that the defendant thought it up on the spot, but then hes been writing .NET code (in its various forms) for 6 years.
If you are completely new to .NET I would suggest you learn C# first. Learning C# will let you focus on the tao of .NET without worrying about all the details, use it to get a feel for the .NET runtime and go off exploring through .NETs rich Framework Class Libraries. A good book to get you started would be Programming C# by Jesse Liberty, OReilly Press, I think this is in its third edition now. Also get the “C# Cookbook” by Stephen Teilhey and Jay Hilyard, O’Reilly again. The 'cookbook is good because it doesn’t get bogged down in WinForms, or ASP.NET but contains common “how do I?” solutions that you can try out in a console application - just what you need to bootstrap your working knowledge of .NET
Then when youve done a bit of .NET with C# (a couple of weeks is enough) and want to throw C++ back into the mix then you could consider Managed C++. However, you should know that Microsoft has a much better C++ to .NET language binding coming, namely C++/CLI (CLI here means Common Language Infrastructure, which is what .NET goes by in its standardised form). C++/CLI is in beta right now and you can get hold of the Visual Studio 2005 beta compilers from http://msdn.microsoft.com. C++/CLI is really like Managed C++ 2.0 or rather Managed C++ Done Right.
This thread has been automatically closed as it remained inactive for 12 months. If you wish to continue the discussion, please create a new thread in the appropriate forum.