Date: Mon, 19 Oct 2009 15:21:26 -0700 From: Gary Kline <kline@thought.org> To: Patrick Mahan <mahan@mahan.org> Cc: FreeBSD Mailing List <freebsd-questions@freebsd.org> Subject: Re: need C help, passing char buffer[] by-value.... Message-ID: <20091019222126.GB12488@thought.org> In-Reply-To: <4ADCAB4F.5040707@mahan.org> References: <20091019013337.GA9522@thought.org> <4ADBFDBA.6040702@pchotshots.com> <20091019170634.GA12371@thought.org> <4ADCAB4F.5040707@mahan.org>
next in thread | previous in thread | raw e-mail | index | archive | help
On Mon, Oct 19, 2009 at 11:09:19AM -0700, Patrick Mahan wrote: > See comments interspaced below - > > Gary, > > Let me restate your problem: You want to read through a file containing tags > delimited by "<>" and to skip these tags if the user has run your command > with > the "-N" flag. > > In C any thing passed by address is "by reference". For a your static > buffer > of 1024 characters: you can pass it by reference as: > > skiptags(buf); /* passes in the starting address of the buffer */ > skiptags(&buf[0]); /* passes in the starting address of the > buffer */ > skiptags(&buf[10]); /* passes int the starting address of the > buffer > at the 11th character position. */ > > Arrays and pointers are always by reference. Individual data types "int", > "char", etc are by value unless passed in as a pointer. I think this is > where > your confusion is around. You've got it exactly right, Patrick. There were no "C" classes in 1978--I taught myself. Obviously, not that well because I have already dreaded pointers. ---Well, usually. > > A couple of things to keep in mind: > > 1. Remember how fgets() works. It is entirely possible that you might > have > tags that span multiple lines. You will need to take that into > account. > > 2. You can manipulate the fixed buffer two different ways: > > A. You can use pointer arithemtic, eg. > > char buf[1024]; > char *cp; > > while ((cp = fgets(buf, sizeof(buf), fp_in))) { > > if (skiptags) cp = skiptag(buf); > > /* If NULL, end of line reached */ > if (!cp) continue; > > } > > char *skiptags(char *buf) > { > char *tp = buf; > > /* find the start of a tag */ > while (*tp != '\0' && *tp++ != '<'); > > /* if no tag is found return start of buffer */ > if (*tp == '\0') > return buf; > > /* Start of tag, find the end of tag */ > while (*tp != '\0' && *tp != '\n' && *tp++ != '>'); > > /* if end of line reached return NULL */ > if (*tp == '\0' || *tp == '\n') > return NULL; > > /* return the next character start after the end tag */ > return ++tp; > } > > B. Using indexing, eg. > > char buf[1024]; > int i, bsize; > > while (fgets(buf, sizeof(buf), fp_in)) { > i = 0; > bsize = strlen(buf); > > if (skiptags) i = skiptag(buf); > > /* If NULL, end of line reached */ > if (i >= bsize) continue; > > } > > int skiptags(char *buf) > { > int c = 0; > > /* find the start of a tag */ > while (buf[c] != '\0' && buf[c] != '<') > c++; > > /* if no tag is found return start of buffer */ > if (buf[c] == '\0') > return 0; > > /* Start of tag, find the end of tag */ > while (buf[c] != '\0' && buf[c] != '\n' && buf[c] != '>') > c++; > > /* if end of line reached return NULL */ > if (buf[c] == '\0' || buf[c] == '\n') > return strlen(buf); > > /* return the next character start after the end tag */ > return ++c; > } > > Both methods should allow you to skip past any tags found in the file > (provided > you handle the case of a tag spanning more than one line). > > > Hope this clears up your confusion and gets you on your way. Your examples help a lot! Everything works except when there are two or more tags on one line such as: <BODY BGCOLOR="#FFFFFF" LINK="#00FFFF" VLINK="#006633"><FONT SIZE="4"> I think I see where is your skiptags--pointer arithematic function--this can be caught. Thanks much! :-) gary > > Patrick > _______________________________________________ > freebsd-questions@freebsd.org mailing list > http://lists.freebsd.org/mailman/listinfo/freebsd-questions > To unsubscribe, send any mail to "freebsd-questions-unsubscribe@freebsd.org" -- Gary Kline kline@thought.org http://www.thought.org Public Service Unix http://jottings.thought.org http://transfinite.thought.org The 7.31a release of Jottings: http://jottings.thought.org/index.php
Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?20091019222126.GB12488>