Question

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

Was it helpful?

Solution 3

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!! ^^

OTHER TIPS

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!

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 .

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top