Which kind of loop do I use, do or while?



Hi, I was wondering if someone could give me some advice. I'm using
C, so the constructs are those of that language, but the question is
not really language specific.

In my program, I want to process either the standard input (stdin) or
the list of files on the command line. I have two ways of doing this,
and I'm asking if there is any advantage to doing it one way or the
other.

Method 1: (using C pseudo-code)
FILE *fp = stdin;
char *filename = "standard input";
do {
if (there are arguments) {
assign filename to argument, shift args over (argv++);
open file, assign fp to FILE pointer;
Check for error while opening file
If there was an error, skip to next iteration (continue);
}
} /* Otherwise there are no files: Assemble stdin (default) */
process(filename, fp, flags);
} while (argv[0] != NULL);

The alternative method would be not to enter the loop if there are no
files:
Method 2:
int error = 0;
FILE *fp;
char *filename;
if (there are no files) {
process("standard input", stdin, flags)
} else {
while (there are files left) {
Assign filename, advance argv;
Open filename, assingn fp;
Check for error while opening file (of course);
If there are errors, skip to next iteration (continue);
process(filename, fp, flags);
}
}

I apologize if the format of the question is not proper for this
group.

-- Marty Amandil
.



Relevant Pages

  • Which programming method is better?
    ... but it is the language I am ... I want to process either the stdin or the list of files ... char *filename; ... process("standard input", stdin, flags) ...
    (comp.lang.c)
  • Re: Which kind of loop do I use, do or while?
    ... Amandil wrote: ... C, so the constructs are those of that language, but the question is ... } /* Otherwise there are no files: Assemble stdin ... process("standard input", stdin, flags) ...
    (comp.programming)
  • Re: Which kind of loop do I use, do or while?
    ... C, so the constructs are those of that language, but the question is ... FILE *fp = stdin; ... process("standard input", stdin, flags) ... exit - this is a religious matter:) ...
    (comp.programming)
  • Re: UTF-8 practically vs. theoretically in the VFS API (was: Re:
    ... > encoding to convert from. ... It's the user who made the filename, ... > purpose was translating to a non-latin language. ... There is absolutely no need to know the charset until the point when the ...
    (Linux-Kernel)
  • Re: Which programming method is better?
    ... process("standard input", stdin, flags) ... and not a filename argument. ... preincrement argv first. ... I'd also go with const char* instead of char*, ...
    (comp.lang.c)