Movatterモバイル変換


[0]ホーム

URL:


OverIQ.com

  1. Home
  2. C Programming Tutorial
  3. fputs() Function in C

fputs() Function in C

Last updated on July 27, 2020


The syntax offputs() function is:

Syntax:int fputc(const char *str, FILE *fp);

This function is used to print a string to the file. It accepts two arguments pointer to string and file pointer. It writes a null-terminated string pointed bystr to a file. The null character is not written to the file. On success, it returns0. On error, it returnsEOF or-1.

The following program demonstrates how to usefputs() function.

 1 2 3 4 5 6 7 8 91011121314151617181920212223242526
#include<stdio.h>#include<stdlib.h>intmain(){charstr[50];FILE*fp;fp=fopen("myfile2.txt","w");if(fp==NULL){printf("Error opening file\n");exit(1);}printf("Testing fputs() function:\n\n");printf("To stop reading press Ctrl+Z in windows and Ctrl+D in Linux :");while(gets(str)!=NULL){fputs(str,fp);}fclose(fp);return0;}

Expected Output:

12345678
Testing fputs() function:To stop reading press Ctrl+Z in windows and Ctrl+D in Linux :The first lineThe second lineThird line^D

How it works:

In line 6, an array of characterstr of size50 is declared.

In line 7, a structure pointer variablefp of typestruct FILE is declared.

In line 8,fopen() function is called with two arguments namely"myfile2.txt" and"w". On success, it returns a pointer to filemyfile2.txt and opens the filemyfile.txt in write-only mode. On failure or end of file, it returnsNULL.

In lines 10-14, if statement is used to test the value offp. If it isNULL,printf() statement prints the error message and program terminates. Otherwise, the program continues with the statement following the if statement.

In line 16 and 17, twoprintf() statements string"Testing fputs() function: \n\n" and"To stop reading press Ctrl+Z in windows and Ctrl+D in Linux : \n\n" to the console.

In lines 19-22, we have while loop in conjunction withgets() function. The while loop will keep asking for more strings when until it counters an end of file character. Here are two important things to remember about the gets() function:

  1. gets() function converts newline character entered to null character ('\0').
  2. When the end of file character is encounteredgets() returnsNULL.

Here is how while loop works:

When first line"The first line" is entered followed by the newline, thegets() function converts the newline('\n') to null character('\0'). At this point,str contains"The first line\0", which is then written to the file.Thefputs() function is then used to write the string to the file. It is important to note thatfputs() function do not writes the null character'\0' character is to the file. Whengets() function encounters the end of file character the while condition becomes false and control comes out of the loop.

In line 24,fclose() function closes the file pointer.

Difference between puts() and fputs()#

Recall that in earlier chapters we have usedputs() function several times to print the strings to the console. The important difference betweenfputs() andputs() is that, theputs() converts the null character('\0') in the string to the newline ('\n') character whereasfputs() doesn't not.


Load Comments


C Programming Tutorial

Recent Posts

x

[8]ページ先頭

©2009-2025 Movatter.jp