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

By: Reema sen Emailed: 1728 times Printed: 2329 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
goto and labels in C
Find square and square root for a given number in C
Open, Creat, Close, Unlink system calls sample program in C
Using calloc() Function in C
perror() Function - example program in C
Character Arrays in C
scanf and sscanf sample program in C
lseek() sample program in C
Sorting an integer array in C
Arrays sample program in C
register Variables in C
Arrays of Structures example program in C
union example program in C
fgets(), fputs() - Line Input and Output - sample program in C
Most Emailed Articles (in C)
Listing Files and Directories sample program in C
Arithmetic Operators in C
Relational and Logical Operators in C
Increment and Decrement Operators in C
register Variables in C
File Inclusion in C
Pointers and Function Arguments in C
Pointers and Arrays in C
Character Pointers and Functions in C
Pointer Arrays and Pointers to Pointers in C
Multi-dimensional Arrays in C (Explained using date conversion program)
Initialization of Pointer Arrays in C
The Birth and history of C Programming Language
Basics of C
Getting Started with C