Hi everyone,
I am a newbie in C programming.
I have to make an implementation of gets() function from stdio.h, but I am not sure where to start.
Please advice, any help is welcome.
Thank you,
Brian.
8 replies to this topic
#1
Posted 10 April 2010 - 02:01 AM
|
|
|
#2
Posted 10 April 2010 - 05:31 AM
Start with a specification of what gets() is supposed to do.
#3
Posted 10 April 2010 - 05:41 AM
And don't forget to make your version trash the program's memory like gets() does :)
Visit Grandpa's Forums, a social networking forum, with family-oriented arcade games, blogs, discussion forums, and photo albums.
#4
Posted 10 April 2010 - 07:30 AM
WingedPanther: gets() - is a standard C function included in stdio.h - my task is to write a c implementation for it.
Ancient Dragon: thanks for the useless advice...
Ancient Dragon: thanks for the useless advice...
#5
Posted 10 April 2010 - 07:34 AM
prakakat said:
WingedPanther: gets() - is a standard C function included in stdio.h - my task is to write a c implementation for it.
Ancient Dragon: thanks for the useless advice...
Ancient Dragon: thanks for the useless advice...
Not entire useless -- gets() will trash your program's memory slicker than snot. But I understand why your teacher assigned that problem as a learning experience to teach loops and getchar().
Visit Grandpa's Forums, a social networking forum, with family-oriented arcade games, blogs, discussion forums, and photo albums.
#6
Posted 10 April 2010 - 07:43 AM
WingedPanther said:
Start with a specification of what gets() is supposed to do.
It's supposed to do standard gets operations, i.e.:
The gets() function shall read bytes from the standard input stream, stdin, into the array pointed to by s, until a <newline> is read or an end-of-file condition is encountered. Any <newline> shall be discarded and a null byte shall be placed immediately after the last byte read into the array.
#7
Posted 10 April 2010 - 07:46 AM
Ancient Dragon said:
Not entire useless -- gets() will trash your program's memory slicker than snot. But I understand why your teacher assigned that problem as a learning experience to teach loops and getchar().
I understand that it is a buggy function and people advice to use fgets() instead. I gotta write that thing under a very short deadline, so I would really appreciate if you could help, thanks.
I understood that I need to specify what my function does first, then I shoudl write it. Should I use pointers in my implementation though?
#8
Posted 10 April 2010 - 07:52 AM
you could use pointers, but you don't have to (except the function's parameter will be a pointer). Just write a loop that calls getchar() and add the character to the input buffer. And don't forget to null terminate the string when done. See prakakat's post, it tells you everything you need to do.
Visit Grandpa's Forums, a social networking forum, with family-oriented arcade games, blogs, discussion forums, and photo albums.
#9
Posted 10 April 2010 - 08:11 AM
prakakat said:
It's supposed to do standard gets operations, i.e.:
The gets() function shall read bytes from the standard input stream, stdin, into the array pointed to by s, until a <newline> is read or an end-of-file condition is encountered. Any <newline> shall be discarded and a null byte shall be placed immediately after the last byte read into the array.
The gets() function shall read bytes from the standard input stream, stdin, into the array pointed to by s, until a <newline> is read or an end-of-file condition is encountered. Any <newline> shall be discarded and a null byte shall be placed immediately after the last byte read into the array.
1 user(s) are reading this topic
0 members, 1 guests, 0 anonymous users


Sign In
Create Account

Back to top









