Hi, need some help with this one. Here is the question:

Compose an algorithm for a program that creates a list of words encountered when reading a series of text files. Write a program to open, and subsequently close, each input file, terminating should an attempt to open any file fail. Derive the file names from the command line.

I've heard of the "argc" and "argv" features to do this, but i have never used them before and have no idea how they work. Is someone able to point me in the right direction?

All help is greatly appreicated.

The main function has two parameters: argc and argv. The names are irrelevant, as you can change them to be whatever you want, but argc and argv are the most common. argc means argument count, and it's an integer value specifying how many arguments were received. argv means argument vector, and it's an array of strings containing the actual arguments. The size of the array is specified by argc. argv is always terminated with a null pointer, and that null pointer is located at argv[argc] . Assuming argc is greater than 0, argv[0] is always the name of the program, which is an unspecified value, and can be an empty string. The arguments you pass explicitly begin at argv[1] .

With all of this in mind, it's easy to treat argc and argv as just another array and size pair:

#include <stdio.h>

int main ( int argc, char *argv[] )
{
  int i;

  if ( argc > 0 )
    printf ( "Running program '%s'\n", argv[0] );

  for ( i = 1; i < argc; i++ )
    printf ( "Argument %d: '%s'\n", i, argv[i] );

  return 0;
}

When running your program from the command line, you specify arguments to main by typing them after the program name. For example, when you run a program without arguments like this:

$ myprog

You would give it four arguments (on top of the program name) like this:

$ myprog this is a test

Each word in "this is a test" constitutes a single argument. Typically you surround arguments with embedded whitespace with double quotes to force them into a single argument. The following would provide three arguments (excluding the program name), where "a test" is the third:

$ myprog this is "a test"

This should give you a start toward providing file paths from the command line.

Comments
That's not indicative of bad advise. This is a solid, instructive example.
For using cplusplus code tags in a c forum
This article has been dead for over six months. Start a new discussion instead.