Jump to content

C - gets() function implementation help

- - - - -

  • Please log in to reply
8 replies to this topic

#1
prakakat

prakakat

    Newbie

  • Members
  • Pip
  • 4 posts
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.

#2
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
  • Location:Upstate, South Carolina
  • Programming Language:C, C++, PL/SQL, Delphi/Object Pascal, Pascal, Transact-SQL, Others
  • Learning:Java, C#, PHP, JavaScript, Lisp, Fortran, Haskell, Others
Start with a specification of what gets() is supposed to do.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#3
Ancient Dragon

Ancient Dragon

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 400 posts
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
prakakat

prakakat

    Newbie

  • Members
  • Pip
  • 4 posts
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...

#5
Ancient Dragon

Ancient Dragon

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 400 posts

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...

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
prakakat

prakakat

    Newbie

  • Members
  • Pip
  • 4 posts

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
prakakat

prakakat

    Newbie

  • Members
  • Pip
  • 4 posts

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
Ancient Dragon

Ancient Dragon

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 400 posts
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
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
  • Location:Upstate, South Carolina
  • Programming Language:C, C++, PL/SQL, Delphi/Object Pascal, Pascal, Transact-SQL, Others
  • Learning:Java, C#, PHP, JavaScript, Lisp, Fortran, Haskell, Others

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.
So, you need a loop, and a check for a newline character. Code that much to start with.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users