View Single Post
  #2   (View Single Post)  
Old 11th November 2011
ocicat ocicat is offline
Administrator
 
Join Date: Apr 2008
Posts: 3,318
Default

Quote:
Originally Posted by Daffy View Post
The "problem" is that it excecutes only if there is only 1 wav file inside the directory.
Okay, you have two avenues to choose from:
  • Pass in all relevant filenames as command-line arguments.
  • Read the directory contents programmatically from within the code.
As for an example of the first, consider the following:
Code:
$ cat test.c
#include <stdio.h>
int main(int argc, char *argv[])
{
  int i;
  for (i = 0; i < argc; i++)
    printf("%d:\t%s\n", i, argv[i]);
  return 0;
}
$ gcc test.c
$ a.out
0:      a.out
$ a.out 1 2 three
0:      a.out
1:      1
2:      2
3:      three
$
For the second approach, study readdir(2). I would recommend working with processing command-line arguments first. It's more flexible.

Writing lots of simple programs (less than 20 lines...) like what I have indicated above is very important as you teach yourself how to answer your own questions. This is the sign of a good programmer -- one who thinks through the problem, identifies what they know, what they don't know, & does the research needed to answer unknowns themselves.
Quote:
char argv[512]; // create array to pass lame switches
While this definition is perfectly legal, it does not follow convention. The names of the two variables passed to main() are typically argc & argv. Limiting use of these names to this special case makes maintenance easier for the next guy who inherits the code. Although you may not pass this code on to anyone else, thinking about the ramifications of maintenance will serve you well.

Once you have mastered The C Programming Language, I would recommend C Traps and Pitfalls next. Andrew Koenig is also one of the original AT&T elite who penned seminal books on the subject. C Traps and Pitfalls points out where C inconsistencies frequently bite newbies & experienced professionals alike.
Reply With Quote