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



Amandil wrote:
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

use the do if your loop must run at least once. use a while if it may not run at least once. use a for if you know exactly how many times it will loop.
.



Relevant Pages

  • 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)
  • filenames with spaces and list in a for loop
    ... Second attempt was to double quote the "`ls -1A $1`", which generates ONE huge argument to the for loop. ... Unfortunately, the list also generates a newline every 80 characters in the $LIST, so once in a while I was left with each_item being set to something like "\nfilename". ... At the first occurrence of $each_item in the function I would get something like <filename> not found. ... what if I have a directory with more than 65000 characters worth? ...
    (comp.unix.shell)
  • Re: long running perl programs & memory untilization
    ... >> It does setup stuff, and then goes into a loop. ... # reads in temporary capture file adds timestamp, ... # Argument 1 is filename that the labled image should be stored as ... my $grab = $_; ...
    (comp.lang.perl.misc)
  • Re: reading strings from terminal and writing them to a file
    ... Everything is more difficult when using Common Lisp, ... # append this as a line to the file named by $filename. ... Way, way, way, clearer than the Ruby gobbledygook. ... The language has multiple ...
    (comp.lang.lisp)
  • Re: DTS query result to file - can I loop it?
    ... How to loop through a global variable Rowset ... How can I change the filename for a text file connection? ... The values for @MyType are stored in a database table so a cursor ... > existing DTS package or will I have to create a new package to handle each ...
    (microsoft.public.sqlserver.dts)