
- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Explain fgetc() and fputc() functions in C language
File is collection of records or is a place on hard disk, where data is stored permanently.
Operations on files
The operations on files in C programming language are as follows −
- Naming the file
- Opening the file
- Reading from the file
- Writing into the file
- Closing the file
Syntax
The syntax for opening a file is as follows −
FILE *File pointer;
For example, FILE * fptr;
The syntax for naming a file is as follows −
File pointer = fopen ("File name", "mode");
For example,
fptr = fopen ("sample.txt", "r"); FILE *fp; fp = fopen ("sample.txt", "w");
fgets( ) and fputs( ) functions
fgets( ) is used for reading a string from a file.
The syntax for fgets() function is as follows −
fgets (string variable, No. of characters, File pointer);
For example,
FILE *fp; char str [30]; fgets (str,30,fp);
fputs( ) function is used for writing a string into a file.
The syntax for fputs() function is as follows −
fputs (string variable, file pointer);
For example,
FILE *fp; char str[30]; fputs (str,fp);
Program
Following is the C program for using fgets() and fputs() functions −
#include <stdio.h> int main(){ FILE *fptr = fopen("sample.txt","w"); fputs("TutorialPoints\n",fptr); fputs("C programming\n",fptr); fputs("Question & Answers",fptr); fclose(fptr); fptr = fopen("sample.txt","r"); char string[30]; while(fgets(string,30,fptr)!=NULL){ printf("%s",string); } fclose(fptr); return 0; }
Output
When the above program is executed, it produces the following result −
TutorialPoints C programming Question & Answers
- Related Questions & Answers
- fgetc() and fputc() in C
- fgetc() function in C Programming Language
- Explain the functions putw() and getw() in C language
- Explain unformatted input and output functions in C language
- Explain Memory allocation functions in C language
- Explain important functions in math.h library functions using C language
- Explain putc() and getc() functions of files in C language
- Explain scope rules related to the functions in C language
- Explain monolithic and modular programming in C language
- Explain C tokens in C Language
- Explain C Error handling functions
- Explain about link and definition section in C language
- Explain pointers and one-dimensional array in C language
- Explain pointers and two-dimensional array in C language
- Explain Squeeze Function C language
Advertisements