Linux and UNIX Man Pages

Linux & Unix Commands - Search Man Pages

fgets(3s) [ultrix man page]

gets(3s)																  gets(3s)

       gets, fgets - get a string from a stream

       #include <stdio.h>

       char *gets(s)
       char *s;

       char *fgets(s, n, stream)
       char *s;
       int n;
       FILE *stream;

       The routine reads a string into s from the standard input stream stdin.	The string is terminated by a newline character, which is replaced
       in s by a null character.  The routine returns its argument.

       The routine reads n-1 characters, or up to a newline character, whichever comes first, from the stream into the string s.  The last charac-
       ter read into s is followed by a null character.  The routine returns its first argument.

       The routine deletes a newline, while keeps it.

       The and routines return the constant pointer NULL upon end of file or error.

See Also
       ferror(3s), fread(3s), getc(3s), puts(3s), scanf(3s)


Check Out this Related Man Page

gets(3C)						   Standard C Library Functions 						  gets(3C)

gets, fgets - get a string from a stream SYNOPSIS
#include <stdio.h> char *gets(char *s); char *fgets(char *s, int n, FILE *stream); DESCRIPTION
The gets() function reads bytes from the standard input stream (see intro(3)), stdin, into the array pointed to by s, until a newline char- acter is read or an end-of-file condition is encountered. The newline character is discarded and the string is terminated with a null byte. If the length of an input line exceeds the size of s, indeterminate behavior may result. For this reason, it is strongly recommended that gets() be avoided in favor of fgets(). The fgets() function reads bytes from the stream into the array pointed to by s, until n-1 bytes are read, or a newline character 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() and gets() functions may mark the st_atime field of the file associated with stream for update. The st_atime field will be marked for update by the first successful execution of fgetc(3C), fgets(), fread(3C), fscanf(3C), getc(3C), getchar(3C), gets(), or scanf(3C) using stream that returns data not supplied by a prior call to ungetc(3C) or ungetwc(3C). RETURN VALUES
If end-of-file is encountered and no bytes have been read, no bytes are transferred to s and a null pointer is returned. For standard-con- forming (see standards(5)) applications, if the end-of-file indicator for the stream is set, no bytes are transferred to s and a null pointer is returned whether or not the stream is at end-of-file. If a read error occurs, such as trying to use these functions on a file that has not been opened for reading, a null pointer is returned and the error indicator for the stream is set. If end-of-file is encoun- tered, the EOF indicator for the stream is set. Otherwise s is returned. ERRORS
Refer to fgetc(3C). ATTRIBUTES
See attributes(5) for descriptions of the following attributes: +-----------------------------+-----------------------------+ | ATTRIBUTE TYPE | ATTRIBUTE VALUE | +-----------------------------+-----------------------------+ |Interface Stability |Standard | +-----------------------------+-----------------------------+ |MT-Level |MT-Safe | +-----------------------------+-----------------------------+ SEE ALSO
lseek(2), read(2), ferror(3C), fgetc(3C), fgetwc(3C), fopen(3C), fread(3C), getchar(3C), scanf(3C), stdio(3C), ungetc(3C), ungetwc(3C), attributes(5), standards(5) SunOS 5.10 15 Oct 2003 gets(3C)
Man Page

11 More Discussions You Might Find Interesting

1. Programming


does anyone knows how to accept a command from a user.. i was wondering to use fgets(), but got no idea how to start it... (4 Replies)
Discussion started by: skanky
4 Replies

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

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

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

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

6. Solaris

/usr/lib/ symbol __nsl_fgetspent_r: referenced symbol not found

deleteing post (0 Replies)
Discussion started by: dshakey
0 Replies

7. Programming

Counting the words in a file

Please find the below program. It contains the purpose of the program itself. /* Program : Write a program to count the number of words in a given text file */ /* Date : 12-June-2010 */ # include <stdio.h> # include <stdlib.h> # include <string.h> int main( int argc, char *argv ) {... (6 Replies)
Discussion started by: ramkrix
6 Replies

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

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

10. UNIX for Dummies Questions & Answers

Help with malloc()

Good day! I'm a newbie in C. I'm trying to get an unlimited input from the user using malloc then printing the inputs after the user presses enter. My code works, but there's a warning that I don't know how to fix. Please help me. Thank you. Here's my code: #include <stdio.h> #include... (6 Replies)
Discussion started by: eracav
6 Replies

11. Programming

Correct way to read data of different formats into same struct

I was wondering what is the correct way to read in data "one-part-per-line" as compared with "one-record-per-line" formats into the same structure in C? format1.dat: Zacker 244.00 244.00 542.00 Lee 265.00 265.00 456.00 Walter 235.00 235.00 212.00 Zena 323.00 215.45 ... (12 Replies)
Discussion started by: yifangt
12 Replies