Sponsored Content
Full Discussion: fgets()
Top Forums Programming fgets() Post 27512 by hell666 on Tuesday 3rd of September 2002 02:06:16 AM
Old 09-03-2002
If you are planning to read from the 'stdin' [Keyboard];
Code:
fgets(l_str, 255, stdin);

 

6 More Discussions You Might Find Interesting

1. Programming

Problem with fgets and rewind function ..

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

Question about NULL Character & fgets()

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

[C] fgets problem with SIGINT singlal!!!

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

fgets problems

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

fgets problems newline

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

fgets read file line with "\n" inside

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(3)						   BSD Library Functions Manual 						  FGETS(3)

NAME
fgets, gets -- get a line from a stream LIBRARY
Standard C Library (libc, -lc) SYNOPSIS
#include <stdio.h> char * fgets(char * restrict str, int size, FILE * restrict stream); char * gets(char *str); DESCRIPTION
The fgets() function reads at most one less than the number of characters specified by size from the given stream and stores them in the string str. Reading stops when a newline character is found, at end-of-file or error. The newline, if any, is retained, and a '' charac- ter is appended to end the string. The gets() function is equivalent to fgets() with an infinite size and a stream of stdin, except that the newline character (if any) is not stored in the string. It is the caller's responsibility to ensure that the input line, if any, is sufficiently short to fit in the string. RETURN VALUES
Upon successful completion, fgets() and gets() return a pointer to the string. If end-of-file or an error occurs before any characters are read, they return NULL. The fgets() and gets() functions do not distinguish between end-of-file and error, and callers must use feof(3) and ferror(3) to determine which occurred. ERRORS
[EBADF] The given stream is not a readable stream. The function fgets() may also fail and set errno for any of the errors specified for the routines fflush(3), fstat(2), read(2), or malloc(3). The function gets() may also fail and set errno for any of the errors specified for the routine getchar(3). SEE ALSO
feof(3), ferror(3), fgetln(3) STANDARDS
The functions fgets() and gets() conform to ANSI X3.159-1989 (``ANSI C89'') and IEEE Std 1003.1-2001 (``POSIX.1''). The IEEE Std 1003.1-2008 (``POSIX.1'') revision marked gets() as obsolescent. CAVEATS
The following bit of code illustrates a case where the programmer assumes a string is too long if it does not contain a newline: char buf[1024], *p; while (fgets(buf, sizeof(buf), fp) != NULL) { if ((p = strchr(buf, ' ')) == NULL) { fprintf(stderr, "input line too long. "); exit(1); } *p = ''; printf("%s ", buf); } While the error would be true if a line longer than 1023 characters were read, it would be false in two other cases: 1. If the last line in a file does not contain a newline, the string returned by fgets() will not contain a newline either. Thus strchr() will return NULL and the program will terminate, even if the line was valid. 2. All C string functions, including strchr(), correctly assume the end of the string is represented by a null ('') character. If the first character of a line returned by fgets() were null, strchr() would immediately return without considering the rest of the returned text which may indeed include a newline. Consider using fgetln(3) instead when dealing with untrusted input. SECURITY CONSIDERATIONS
Since it is usually impossible to ensure that the next input line is less than some arbitrary length, and because overflowing the input buf- fer is almost invariably a security violation, programs should NEVER use gets(). The gets() function exists purely to conform to ANSI X3.159-1989 (``ANSI C89''). BSD
May 13, 2010 BSD
All times are GMT -4. The time now is 04:33 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy