On Wed, Jan 29, 2003 at 03:36:44AM +0200, Timo Sirainen wrote: > So it seems. That's probably one reason why people still keep using only > libc stuff - requiring external libraries is annoying, distributing huge > string+whatever libraries with your sources is annoying, and stripping > them down to bare minimals is also annoying. Indeed. What we want is a language compatible with C but with string handling as a standard part -- luckily there is such a beast -- C++ ! Compare the following two programs to read a name and write a greeting. These examples were fairly compelling to me. Matt (excerpted from http://www.research.att.com/~bs/new_learning.pdf ) C++ #include<iostream> / / get standard I/O facilities #include<string> / / get standard string facilities // Published in the May 1999 issue of "The C/C++ Users Journal". All rights reserved int main() { using namespace std; / / gain access to standard library cout << "Please enter your first name:\n"; string name; cin >> name; cout << "Hello " << name << \n; } C: #include<stdio.h> #include<ctype.h> #include<stdlib.h> void quit() / / write error message and quit { fprintf(stderr,"memory exhausted\n") ; exit(1) ; } int main() { int max = 20; char* name = (char*)malloc(max) ; / / allocate buffer if (name == 0) quit() ; printf("Please enter your first name:\n") ; while (true) { / / skip leading whitespace int c = getchar() ; if (c == EOF) break; / / end of file if (!isspace(c)) { ungetc(c,stdin) ; break; } } int i = 0; while (true) { int c = getchar() ; if (c == \n || c == EOF) { / / at end; add terminating zero name[i] = 0; break; } name[i] = c; if (i==max-1) { / / buffer full max = max+max; name = (char*)realloc(name,max) ; / / get a new and larger buffer if (name == 0) quit() ; } i++; } printf("Hello %s\n",name) ; free(name) ; / / release memory return 0; }
This archive was generated by hypermail 2b30 : Wed Jan 29 2003 - 12:47:38 PST