[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]
Re: A simple C doubt
Try this function getstr. I hope there is no buffer overflow...
unsigned char *dst;
if ((dst=(unsigned char *)malloc(1)) == NULL) return 0;
while((c = getchar()) != '\n')
if ((dst=(unsigned char *)realloc(dst, strlen(dst)+3)) == NULL)
sprintf(dst, "%s%c", dst, c);
int main(int argc, char *argv)
if ((str = getstr()) == NULL)
printf("Error: No memory free!\n"); exit(1);
printf("Text = %s\n", str);
On Wed, 20 Aug 2003 01:39:50 -0300
Igor Cabral Corr <firstname.lastname@example.org> wrote:
> Hi people!
> How can I try to read a line from stdin (in fact, a file piped into stdin) and know if the read line is empty or not?
> I want to read, for example:
> How can I read these 4 lines? Using something like
> for (int i=0; i<4; i++) scanf("%s", line);
> the empty line will be skipped, and I don´t know that. Someone knows an easy way to do it?
> PS.: I know how many lines I want to read.
> Igor Cabral Corrêa
http://www.againsttcpa.com/ - Don't let them take YOUR RIGHTS!