fgets(), fputs() - Line Input and Output - sample program in C

By: Reema sen Emailed: 1600 times Printed: 2044 times    

Latest comments
By: rohit kumar - how this program is work
By: Kirti - Hi..thx for the hadoop in
By: Spijker - I have altered the code a
By: ali mohammed - why we use the java in ne
By: ali mohammed - why we use the java in ne
By: mizhelle - when I exported the data
By: raul - no output as well, i'm ge
By: Rajesh - thanx very much...
By: Suindu De - Suppose we are executing

The standard library provides an input and output routine fgets that is similar to the getline function"
   char *fgets(char *line, int maxline, FILE *fp)
fgets reads the next input line (including the newline) from file fp into the character array line; at most maxline-1 characters will be read. The resulting line is terminated with '\0'. Normally fgets returns line; on end of file or error it returns NULL. (Our getline returns the line length, which is a more useful value; zero means end of file.)

For output, the function fputs writes a string (which need not contain a newline) to a file:

   int fputs(char *line, FILE *fp)
It returns EOF if an error occurs, and non-negative otherwise.

The library functions gets and puts are similar to fgets and fputs, but operate on stdin and stdout. Confusingly, gets deletes the terminating '\n', and puts adds it.

To show that there is nothing special about functions like fgets and fputs, here they are, copied from the standard library on our system:

   /* fgets:  get at most n chars from iop */
   char *fgets(char *s, int n, FILE *iop)
   {
       register int c;
       register char *cs;

       cs = s;
       while (--n > 0 && (c = getc(iop)) != EOF)
           if ((*cs++ = c) == '\n')
               break;
       *cs = '\0';
       return (c == EOF && cs == s) ? NULL : s;
   }

   /* fputs:  put string s on file iop */
   int fputs(char *s, FILE *iop)
   {
       int c;

       while (c = *s++)
           putc(c, iop);
       return ferror(iop) ? EOF : 0;
   }
For no obvious reason, the standard specifies different return values for ferror and fputs.

It is easy to implement our getline from fgets:

   /* getline:  read a line, return length */
   int getline(char *line, int max)
   {
       if (fgets(line, max, stdin) == NULL)
           return 0;
       else
           return strlen(line);
   }

C Home | All C Tutorials | Latest C Tutorials

Sponsored Links

If this tutorial doesn't answer your question, or you have a specific question, just ask an expert here. Post your question to get a direct answer.



Bookmark and Share

Comments(3)


1. View Comment

hiiiiiiii

View Tutorial          By: venkateshwarreddy suravaram at 2010-04-30 23:57:01
2. View Comment

/*for (i=0; i<lim-1 && (c=getchar())!=EOF && c!='\n'; ++i)
s[i] = c;*/
how to write this loop without using && and ||
please reply fast.........


View Tutorial          By: GD at 2012-07-14 12:47:27
3. View Comment

if (fget(y,100,fp2)!=10);
what should be the prototype for this.
plzz reply fast.


View Tutorial          By: ruqsar at 2014-02-10 10:54:32

Your name (required):


Your email(required, will not be shown to the public):


Your sites URL (optional):


Your comments:



More Tutorials by Reema sen
this keyword sample in Java
Using the DriverManager Class vs Using a DataSource Object for a connection
Steps to get a Free SSL certificate for your Tomcat
fgets(), fputs() - Line Input and Output - sample program in C
Address Arithmetic and pointers in C
Constants and escape sequences in C
Implementing Pure Virtual Functions in C++
strcat() and strncat() sample program in C++
paint() sample program to draw a line in J2ME
Creating and Handling JAR files
SequenceInputStream example program in Java
FilenameFilter - sample program in Java
Transient vs Volatile modifiers in Java
while (1) Loops in C++
Use of 'finally' in Java

More Tutorials in C
Sum of the elements of an array in C
Printing a simple histogram in C
Sorting an integer array in C
Find square and square root for a given number in C
Simple arithmetic calculations in C
Command-line arguments in C
Calculator in C
Passing double value to a function in C
Passing pointer to a function in C
Infix to Prefix And Postfix in C
while, do while and for loops in C
Unicode and UTF-8 in C
Formatting with printf in C
if, if...else and switch statements in C with samples
Statements in C

More Latest News
Most Viewed Articles (in C )
Using memset(), memcpy(), and memmove() in C
UNIX read and write system calls sample program in C
Constants and escape sequences in C
Table Lookup - hashtab - example program in C
fgets(), fputs() - Line Input and Output - sample program in C
A Storage Allocator sample program in C
Using realloc() Function in C
ERRNO.H Header File in C
Writing The First C program
Formatting with printf in C
Calculator in C
Passing double value to a function in C
Printing a simple histogram in C
Getting Started with C
File Copying in C
Most Emailed Articles (in C)
Conditional Expressions in C
fgets(), fputs() - Line Input and Output - sample program in C
Symbolic Constants using #define in C
#if, #elif, #ifndef, #ifdef in C (Conditional Inclusion)
Address Arithmetic and pointers in C
Character Pointers and Functions in C
Initialization of Pointer Arrays in C
Pointers to Structures example program in C
Using printf function in C
A Storage Allocator sample program in C
Using free() Function in C
The C Character Set
Writing The First C program
Formatting with printf in C
while, do while and for loops in C