
"Code is never completed ... it can always be improved." ... it's one of the most difficult aspects in many projects to say "yes, that does what we want and we should go for a release now rather than continuing to develop until ... until it's so late that we've missed the boat / added too much complexity to make the program useful and viable". Anyone who's managed a software project may be very familiar with this sort of decision, and how hard it is to make.
A similar thing happens with examples on our web site ... "Wouldn't it be nice if this example also showed xxx" we'll say to ourselves, or "should it also check for yyyy". But, alas, the result of adding in xxx, yyyy and zzzzz while we're at it means that the example is far less followable in showing the basic fundamental elements.
Yesterday, I recalled an example that reads a web server log file in a C program and stores pertinent elements into a growing array (via realloc) of structures. (See
[source]). But the code included the opening of the log file as follows:
fh = fopen("ac_20100630","r");
Now - that's all well and good, except that the program is fixed to a specific log file, and exited with a nasty
segmentation fault if the file did not exist. Discussion ensued, and we modified the code to:
fh = fopen(*++argv,"r");
if (! fh) {
printf("Oh Shdear ... no file\n");
return (1);
}
Better? Yes. Perfect? No! it would have been a good idea to echo out the name of the file we failed to open, give
an excuse the reason, such as "no such file" or "not readable", etc ... all of which would have contributed to flannel which would render the original example far too dilute in what it intended to show.
A good approach that I've often taken is what I describe as "build up examples", where I keep a whole series of versions / snapshots showing how code has developed; here's such a series from yesterday, looking at handling the command line inputs in C which provided a very good and interesting example of handling of character strings, individual characters, and the arrangement of data into somewhat more complex layouts.
Step 1,
Step 2,
Step 3,
Step 4
There's a further "build up" example showing the first use of structs (structures) in C ... and that's here:
Step 1,
Step 2,
Step 3
And even building up examples like that, we sometimes produce some interesting sideshoot examples -
[here] is one that looks at the thorny issue of string comparison; written during the course, although it largely duplicates
[this example] from the course.
(written 2011-01-06, updated 2011-01-08)
Associated topics are indexed under
C210 - C and C based languages - File Handling [3386] Adding the pieces together to make a complete language - C - (2011-08-11)
[2572] The what and why of C pointers - (2010-01-13)
[2571] Reading and writing files in C - (2010-01-12)
[2002] New C Examples - pointers, realloc, structs and more - (2009-01-20)
C206 - C and C based languages - Character Strings [3718] Splitting a record into individual data values in C - (2012-05-04)
[3593] Chars, char arrays and strings in C. Some early cautions and pitfalls. - (2012-01-26)
[3146] Strings in C - (2011-01-25)
[3144] Setting up arrays in C - fixed size at compile time, or dynamic - (2011-01-24)
[2844] Learning about Regular Expressions in C through examples - (2010-06-30)
[2843] String functions in C - (2010-06-30)
[1338] Handling Binary data in Tcl (with a note on C) - (2007-09-09)
C209 - C and C based languages - Structures and Unions [3145] Structures v Structure Pointers in C. How, which, why. - (2011-01-25)
[2573] C Structs - what, how and why - (2010-01-13)
[1669] What are Unions (C programming) - (2008-06-08)
[1584] Using Structs and Unions together effectively in C - (2008-03-21)
[1572] C - structs and unions, C++ classes and polymorphism - (2008-03-13)
[1478] Some new C programming examples - files, structs, unions etc - (2007-12-19)
Some other Articles
Car Parking in Melksham - thoughts on the proposed scheme, and wider thoughts tooThe familiy is defunct. Long live the family.C++ - putting the language elements together into a programC++ objects - some short, single file demonstrationsWhen is a program complete?New year, new C CourseLearning to write good programs in C and C++ - separating out repeated codeTransWilts - some things to see and doArrays of arrays - or 2D arrays. How to program tables.Opportunities for Melksham - new businesses in the town