C
CBFalconer
stathis said:.... snip ...
How about this one:
#include <stdio.h>
int main(void)
{
int ch;
unsigned int count=0;
char buf[1024],*ptr=buf;
while ((ch=getchar())!='\n')
{
if (++count>=sizeof(buf))
break;
*ptr++=ch;
}
*ptr='\0';
printf("%s\n",buf);
return 0;
}
IMO it is clearer here to use indices and let the compiler optimize
to pointers if it so wishes. Write for clarity, not efficiency.
Use blanks! Always consider EOF.
#include <stdio.h>
int main(void)
{
int ch;
unsigned int count = 0;
char buf[1024];
while (('/n' != (ch = getchar())) && (EOF != ch)) {
if (count >= sizeof buf - 1) break;
buf[count++] = ch;
}
buf[count] = '\0';
printf("%s\n", buf);
return 0;
}
This still doesn't handle the flushing (or other treatment) of
overlong lines. This doesn't matter here because the program
exits, but would be of concern if this was part of an interactive
process.
--
"If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson
More details at: <http://cfaj.freeshell.org/google/>
Also see <http://www.safalra.com/special/googlegroupsreply/>