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



On Feb 27, 7:38 pm, Amandil <mazwo...@xxxxxxxxx> 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

Don't do it all in main(). Create small functions
to do the work:

if ( argc > 1 ) {
for ( i=1;i < argc; i++ ) {
HandleFileNamed( argv[i] );
}
}
else {
while ( string=GetNextNameFromStdin() ) {
HandleFileNamed( string );
}
}

--
Fred Kleinschmdit
.



Relevant Pages

  • Re: How to suppress autotext US date format after typing current y
    ... The date drop down merely reflects the language at the cursor ... American designed template to be truly UK English. ... I have all the usual settings corrected to ... the date format Word forces on you is ...
    (microsoft.public.word.docmanagement)
  • Re: OOP php user system
    ... never actually programmed proper OO code yet. ... Python's my first language and this sort of thing works ... design, even in Python. ... Jerry, Jerry, Jerry! ...
    (comp.lang.php)
  • Re: QDE (Quick Date Entry)
    ... Also if you want to show the results in the default date setting format ... me an advantage of querying the registry settings. ... This is always a decision between flexibility ... > learn a form of syntax for a very simple display language. ...
    (microsoft.public.excel.worksheet.functions)
  • Re: Rene is a hypocrite (OK, what else is new?)
    ... > language to it, you will need to add N translators to convert from the new ... > intermediate format, and one to convert the other way around. ... it would be nice to automatically translate MASM32 includes into ... requires a bit of manual intervention after conversion). ...
    (alt.lang.asm)
  • Re: Is C99 the final C? (some suggestions)
    ... that's not what I am proposing. ... that the preprocessor know anything more about the C language at all. ... > The %x format specifier mechanism is perhaps not a good way to do this, ... I highly doubt that operator overloading is one that has ...
    (comp.lang.c)