views:

126

answers:

3

Greetings.

I have a project for my Data Structures class, which is a file compressor that works using Binary Trees and other stuff. We are required to "zip" and "unzip" any given file by using the following instructions in the command line:

For compressing: compressor.exe -zip file.whatever

For uncompressing: compressor.exe -unzip file.zip

We are programming in C++. I use the IDE Code::Blocks and compile using GCC in Windows.

My question is: How do you even implement that??!! How can you make your .exe receive those parameters in command line, and then execute them the way you want?

Also, anything special to have in mind if I want that implementation to compile in Linux?

Thanks for your help!!!

+9  A: 

You may want to look in your programming text for the signature of the main function, your program's entry point. That's where you'll be able to pull in those command line parameters.

I don't want to be more detailed than that because this is apparently a key point of the assignment, and if I ever find myself working with you, I'll expect you to be able to figure this sort of stuff out on your own once you've received an appropriate nudge. :)

Good luck!

Greg D
'argc' and 'argv[]'... I suspected so! >=DI've never worked with the main() arguments before, but... I guess there is where my answer resides. Time to do some research!Thanks Creg! ^^
NeoGlitch
Glad I could help! :)
Greg D
Waw - I like the way you don't want to spoil the guy's lookup-fun!
xtofl
+2  A: 

As I recall, the Single UNIX Specification / POSIX defines getopt in unistd.h to handle the parsing of arguments for you. While this is a C function, it should also work in C++.

GNU GLIBC has this in addition to getopt_long (in getopt.h) to support GNU's extended --style .

R. Bemrose
It's not spec'd in this Q, but if you check NeoGlitch's other question you'll see that he needs platform-independent solutions. getopt isn't ANSI C, so it won't be available on the Window's platform.
Greg D
I just assumed that, since he was using GCC, he was also using something like MinGW, which has unistd.h and getopt.h.
R. Bemrose
Fair enough. :)
Greg D
There's also a getopt for windows: getpot. You can download this from sourceforge. Worked for me.
DaClown
+1  A: 

Lo logré, I gotz it!!

I now have a basic understanding on how to use the argc and argv[ ] parameters on the main() function (I always wondered what they were good for...). For example, if I put in the command line:

compressor.exe -unzip file.zip

Then:

  • argc initializes in '3' (number of arguments in line)
  • argv[0] == "compressor.exe" (name of app.)
  • argv[1] == "-unzip"
  • argv[2] == "file.zip"

Greg (not 'Creg', sorry =P) and Bemrose, thank you guys for your help!! ^^

NeoGlitch
Awesome! :D Glad it worked out!
Greg D