09-11-2002
in defense of fgets
Quote:
Originally posted by wolk
why complicate the simple stuff...?
why don't u use :
scanf("%s",&x );
I certainly agree with the sentiment "why complicate the simple stuff?". Still I have to say that fgets is, to me, a much simpler routine to understand than scanf. And even if it wasn't, scanf will do more work than fgets to acquire that string. Finally, the fgets solution will prevent buffer overflow. You will need to use something like:
scanf("%255s", &x);
to render this safe. And if the buffer is not a constant size you will need to dynamically build the format string that you pass to scanf.
scanf is great when you have a complex set of data, but to just read a single string, I would always go with fgets. Even for your second example, I might tend to go with fgets and atoi. But if it was, say, two integers and a string, I would use the scanf routine.
6 More Discussions You Might Find Interesting
1. Programming
Hello Friends,
I got stuck with fgets () & rewind() function .. Please need help..
Actually I am doing a like,
The function should read lines from a txt file until the function is called..
If the data from the txt file ends then it goes to the top and then again when the function is called... (1 Reply)
Discussion started by: user_prady
1 Replies
2. Programming
Assume client send the message " Hello ", i get output such as
Sent mesg: hello
Bytes Sent to Client: 6
bytes_received = recv(clientSockD, data, MAX_DATA, 0);
if(bytes_received)
{
send(clientSockD, data, bytes_received, 0);
data = '\0';... (2 Replies)
Discussion started by: f.ben.isaac
2 Replies
3. Programming
Hi all,
I have this method to read a string from a STDIN:
void readLine(char* inputBuffer){
fgets (inputBuffer, MAX_LINE, stdin);
fflush(stdin);
/* remove '\n' char from string */
if(strlen(inputBuffer) != 0)
inputBuffer = '\0';
}
All work fine but if i... (1 Reply)
Discussion started by: hurricane86
1 Replies
4. Programming
I've been having trouble with reading past the end-of-file in C. Can anyone find my stupid mistake?
This is the minimal code needed to cause the error for me:
FILE *f = fopen(name, "r");
if (!f)
return;
pari_sp ltop = avma;
char line;
while(fgets(line, 1100, f) != NULL)
printf(".");... (23 Replies)
Discussion started by: CRGreathouse
23 Replies
5. Programming
hello,
i'm trying to write a C-program that reads a file line by line.
(and searches each line for a given string)
This file is an special ASCII-database-file, with a lot of entries.
I checked the line with most length, and it was about 4000 characters.
With google i found several... (4 Replies)
Discussion started by: p1cm1n
4 Replies
6. Programming
Hi,
I have a string like this,
char str ="This, a sample string.\\nThis is the second line, \\n \\n, we will have one blank line";
if I want to use strtok() to seperate the string, which token should I use?
I tried "\n", "\\n", either not working.
peter (1 Reply)
Discussion started by: laopi
1 Replies
FGETS(P) POSIX Programmer's Manual FGETS(P)
NAME
fgets - get a string from a stream
SYNOPSIS
#include <stdio.h>
char *fgets(char *restrict s, int n, FILE *restrict stream);
DESCRIPTION
The fgets() function shall read bytes from stream into the array pointed to by s, until n-1 bytes are read, or a <newline> is read and
transferred to s, or an end-of-file condition is encountered. The string is then terminated with a null byte.
The fgets() function may mark the st_atime field of the file associated with stream for update. The st_atime field shall be marked for
update by the first successful execution of fgetc(), fgets(), fgetwc(), fgetws(), fread(), fscanf(), getc(), getchar(), gets(), or scanf()
using stream that returns data not supplied by a prior call to ungetc() or ungetwc().
RETURN VALUE
Upon successful completion, fgets() shall return s. If the stream is at end-of-file, the end-of-file indicator for the stream shall be set
and fgets() shall return a null pointer. If a read error occurs, the error indicator for the stream shall be set, fgets() shall return a
null pointer, and shall set errno to indicate the error.
ERRORS
Refer to fgetc() .
The following sections are informative.
EXAMPLES
Reading Input
The following example uses fgets() to read each line of input. {LINE_MAX}, which defines the maximum size of the input line, is defined in
the <limits.h> header.
#include <stdio.h>
...
char line[LINE_MAX];
...
while (fgets(line, LINE_MAX, fp) != NULL) {
...
}
...
APPLICATION USAGE
None.
RATIONALE
None.
FUTURE DIRECTIONS
None.
SEE ALSO
fopen() , fread() , gets() , the Base Definitions volume of IEEE Std 1003.1-2001, <stdio.h>
COPYRIGHT
Portions of this text are reprinted and reproduced in electronic form from IEEE Std 1003.1, 2003 Edition, Standard for Information Technol-
ogy -- Portable Operating System Interface (POSIX), The Open Group Base Specifications Issue 6, Copyright (C) 2001-2003 by the Institute of
Electrical and Electronics Engineers, Inc and The Open Group. In the event of any discrepancy between this version and the original IEEE
and The Open Group Standard, the original IEEE and The Open Group Standard is the referee document. The original Standard can be obtained
online at http://www.opengroup.org/unix/online.html .
IEEE
/The Open Group 2003 FGETS(P)