Using fgets() to store string in memory

A

August Derleth

Are you *sure* it solves the OP problem? Please engage your brain
*before* replying ;-)

Dan, it solves the general problem in a rather elegant way and I think you
know it.

Given your beef with the silent truncation behavior of fgets(), fggets()
should be a breath of fresh air: Automatic reallocation with a noisy
failure if it fails to grab any more core. You need to call free() on the
pointer you sent in, but, as you commonly say, it's simply a matter of
"engaging the brain".

"Engage the brain" indeed. If it were any simpler, it would be C++. ;-)
 
D

Dan Pop

In said:
It solves his problem in that it allows him to get a string from
the operator without worrying about the length of that string, and
elides any final '\n'.

But this is not his *stated* problem. That's why I asked you to engage
your brain before replying ;-)

Dan
 
D

Dan Pop

In said:
I might miss something here. Why is fgets useless? I can see that using a
number of other library functions the same results can be achieved as with
fgets, but I still do not see why it is badly designed. From a API
development point of view I would say it covers everything needed to be
simple but complete in it's functionality. I'm just curious to the reason
for the comment.

I have addressed this issue countless times, in this very newsgroup, so
Google is your friend.

Basically, after writing the code using fgets in a bullet-proof manner,
you realise that a getc-based solution would have been simpler.

If truncation of superlong lines is what you want, scanf is your friend.
Every time I've challenged someone to do that with fgets in a bullet-proof
manner, I've got a broken solution, missing one failure mode or another.

If truncation is not an option, a solution that can handle arbitrary
long lines via dynamic input buffer reallocation is needed and fgets
doesn't even come close.

Dan
 
D

Dan Pop

In said:
You are innately evil, and are attempting to corrupt the innocent
youths of the world :)

I was merely giving him what he was asking for:

What's the syntax for using fgets() to store a string in memory? I
understand that fgets() can solve the problem of storing a string
that has more characters than the size of the declared array.
^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^^^^^^^^^^^^

Unless I'm missing something, gets() works far better than fgets() for
this purpose ;-)

Dan
 
R

Richard Bos

August Derleth said:
Given your beef with the silent truncation behavior of fgets(), fggets()
should be a breath of fresh air:
"Engage the brain" indeed. If it were any simpler, it would be C++. ;-)

Ok, this tells me I should never use the ggets package: if it were any
simpler, it would be an over-designed, over-complicated, under-
understood monstrosity, with delusions of grandeur. Think how unusable
it must be now...

Richard
 
A

August Derleth

Ok, this tells me I should never use the ggets package: if it were any
simpler, it would be an over-designed, over-complicated, under-
understood monstrosity, with delusions of grandeur. Think how unusable
it must be now...

Heh. Very funny. ;-)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top