Using fgets() to store string in memory

P

Peter Mount

Hello

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.

Thanks

Peter Mount
(e-mail address removed)
 
D

Dan Pop

In said:
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.

Nonsense! Read the specification of fgets() in your favourite C book
instead of "understanding" things.

BTW, IMHO, fgets() is a perfectly useless library function, invented by
someone who skipped the design phase.

Dan
 
A

Al Bowers

Peter said:
Hello

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.

It can solve the problem of attempting to read a stream that may
be more characters than the size of the declared array (as opposed
to function gets).

The synopsis of the function is:
#include <stdio.h>
char *fgets(char * restrict s, int n,
FILE * restrict stream);


Question 7.1 of the faq can over one possible use of the function where
the stream is stdin. http://www.eskimo.com/~scs/C-faq/q7.1.html

Use a c.l.c googles search or a web search for online resource of the
function. Seek offline resources including the Standard. A newsgroup
discussion is probably not the best resource for a comprehensive
discussion or descripition of the function.
 
D

Dan Pop

It can solve the problem of attempting to read a stream that may
be more characters than the size of the declared array (as opposed
to function gets).

Come to think of it, it is gets() that actually solves the OP problem ;-)

Dan
 
D

Daniel Rudy

And somewhere around the time of 05/19/2004 07:17, the world stopped and
listened as Peter Mount contributed the following to humanity:
Hello

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.

Thanks

Peter Mount
(e-mail address removed)

char line[100];

int main()
{
printf("Prompt: ");
fgets(line, sizeof(line), stdin);
printf("String: %s\n", line);
}
 
C

CBFalconer

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

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'. He will have to observe the published
calling parameters, and consider how he treats the pointer to the
line after use. He might have to actually read the header and/or
the usage examples.
 
C

CBFalconer

Dan said:
.... snip ...

Come to think of it, it is gets() that actually solves the OP
problem ;-)

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

Frits Germs

Dan Pop said:
In <[email protected]> "Peter Mount"

Nonsense! Read the specification of fgets() in your favourite C book
instead of "understanding" things.

BTW, IMHO, fgets() is a perfectly useless library function, invented by
someone who skipped the design phase.

Dan

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

Malcolm

Peter Mount said:
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.
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

int main(void)
{
char buff[256];

printf("Enter a string\n");
if( fgets(stdin, buff, sizeof buff) == 0)
{
printf("Input error\n");
exit(EXIT_FAILURE);
}
ptr = strchr(buff, '\n');
if(!ptr)
{
printf("Line too long\n"):
exit(EXIT_FAILURE);
}
*ptr = 0;
printf("Your string ***%s***\n", buff);
exit(0);
}
 
A

Al Bowers

Malcolm said:
Peter Mount said:
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.


#include <stdio.h>
#include <string.h>
#include <stdlib.h>

int main(void)
{
char buff[256];

printf("Enter a string\n");

puts("Enter a string");

or printf("Enter a string: ");
fflush(stdout);
if( fgets(stdin, buff, sizeof buff) == 0)

The synopsis of the function is:
#include <stdio.h>
char *fgets(char * restrict s, int n,
FILE * restrict stream);
{
printf("Input error\n");
exit(EXIT_FAILURE);
}
ptr = strchr(buff, '\n');

What is ptr?
if(!ptr)
{
printf("Line too long\n"):
exit(EXIT_FAILURE);
}
*ptr = 0;
printf("Your string ***%s***\n", buff);
exit(0);
}

Why did you post such garbage? This code? is an example
of why the OP should not use a newsgroup for a comprehensive
question on how to code with the function fgets.
 
M

Malcolm

Frits Germs 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.
Imagine we have the following

char buff[N];

....

gets(buff);

this is fine until someone enters aline of N or more characters, when you
get a buffer overflow and undefined behaviour.

Now the temptation is to replace this with

fgets(buff, N, stdin);

However this solution is if anything worse than what has gone before. With
gets() we had undefined behaviour - which probably means a crash with the
message "segmentation fault", with fgets() we have wrong behaviour - silent
truncation of the string, so "message to court martial to be put into
execution pending decision of civilian court" becomes "sentence of court
martial to be put into execution pending decision of civilian". No compiler
can warn about or correct wrong behaviour, because it has no insight into
the purpose of the program.

fgets() can of course be used correctly, but most programmers aren't capable
of doing so - witness the reply to my post showing the correct way to check
for overflow conditons.
 
A

Alan Balmer

Frits Germs 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.
Imagine we have the following

char buff[N];

...

gets(buff);

this is fine until someone enters aline of N or more characters, when you
get a buffer overflow and undefined behaviour.

Now the temptation is to replace this with

fgets(buff, N, stdin);

However this solution is if anything worse than what has gone before. With
gets() we had undefined behaviour - which probably means a crash with the
message "segmentation fault", with fgets() we have wrong behaviour - silent
truncation of the string,

The truncation need not be silent. I have code which reads and parses
lines from configuration files which have a specified maximum length.
Over-length lines are reported as such and the input is flushed to the
next newline.
 
M

Malcolm

Alan Balmer said:
The truncation need not be silent. I have code which reads and
parses lines from configuration files which have a specified
maximum length.
Over-length lines are reported as such and the input is flushed to
the next newline.
It is possible to use fgets() correctly. From experience this isn't commonly
done, even by experienced programmers.
By the time you have used strchr() to check for the trailing newline, and
gobbled input to prevent the next function reading a partial line, it is
probably easier to write your own fgetline() function based on fgetc().
 
A

Alan Balmer

It is possible to use fgets() correctly. From experience this isn't commonly
done, even by experienced programmers.
By the time you have used strchr() to check for the trailing newline, and
gobbled input to prevent the next function reading a partial line, it is
probably easier to write your own fgetline() function based on fgetc().
I've done it that way, too (in fact the current implementation uses
fgetc(), after ([OT]) testing to see that the efficiency was about the
same on the target platform.) Not much different - the gobble part is
the same either way, and there are ways of detecting the full buffer
without using strchr().
 

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

Forum statistics

Threads
473,756
Messages
2,569,535
Members
45,007
Latest member
OrderFitnessKetoCapsules

Latest Threads

Top