#include <stdio.h> /* For size_t */
/*
Reads at most size characters into the array.
Stops if a newline or EOF is encountered.
The newline is not included in the array.
The array is always terminated by '\0'.
Returns length of the string.
Please note that memory allocation is callers responsibility.
pptr should have space for size+1 characters.
*/
size_t input(char *pptr, size_t size)
{
size_t len = 0;
while(size > len)
{ int ch;
ch = getchar();
if(ch == '\n' || ch == EOF) break;
*(pptr++) = (char)ch;
++len;
}
*pptr = '\0';
return len;
}
Regards,
Jyoti
--
To unsubscribe from this group, send email to
nextgen_engg...@googlegroups.com
For more options, visit this group at
http://groups.google.co.in/group/nextgen_engg?hl=en
To unsubscribe, reply using "remove me" as the subject.
Regards,
Jyoti
Here is a test code with some modifications to the function just to
make clear as to what is happeninng :
http://codepad.org/J267sKdP
1. Run the code on your local machine.
2. You will have to read characters more than the size of pptr to
break the function.
-
Nilesh
-
Nilesh