×

File Handling in C

File Handling is used to open, read, write, search and close file.  C files I/O functions handles data on secondary storage device. File handling function: There are varioushandling functions in C.
Function Operation
fopen() It is used to create a file.
fclose() It is used to create close an existing file.
getc() It is used to read a character from a file.
putc() It is used to write a character in file.
fprintf() It is used to write set of data in file.
fscanf() It is used to read set of data from file.
getw() It is used to read an integer from a file
putw() It is used to write an integer in file.

C fopen() function The fopen() function is used to open a file.The given syntax of file opening is. Syntax
FILE *fopen( const char * filePath, const char * mode );
Let us consider an example
#include<stdio.h>
int main()
{
    FILE *fp;
    fp = fopen("fileName.txt","w");
    return 0;
}
Here is filePath declare a variable as a pointer to the data type FILE. There are different type of modes that help us to access the file in the fopen() function.

Mode Description
r It is used to open a text file in read mode.
W It is used to open a text file in write mode.
a It is used to open a text file in append mode.
r+ It is used open a text file in read and write mode.
w+ It is used open a text file in read and write mode.
a+ It is used open a text file in read and write mode.
rb It is used open a binary file in read mode.
wb It is used open a binary file in write mode.
ab It is used open a binary file in append mode.
rb+ It is used open a binary file in read and write mode.
wb+ It is used open a binary file in read and write mode.
ab+ It is used open a binary file in read and write mode.

C fclose() function: The fclose() function is used to close a file . Following is the syntax of fclose() function Syntax:
char fclose( FILE *fp);
Example
#include<stdio.h>
int main(){
    FILE *fp;
    fp = fopen("fileName.txt","w");
    fprintf(fp, "%s", "Hello we are learning C langauge  from TutorialAndExample
.com");
    fclose(fp);
    return 0;
}
  • The above example will call the < filename> name.
  • The wis used to open for writing, and if the file does not exist then new file will be created.
  • The fprintf function writes text on the file.
  • The fclosefunction closed the file and reassesses the memory stream.
C getc() function In C language, getc() function is  library function that is used to read a character from a file. Synatx:
int getc( FILE * stream );
Example
#include<stdio.h>
int main(){
  FILE *fp = fopen("fileName.txt", "r");
  int ch = getc(fp);
  while (ch != EOF){
    /* To display the contents of the file on the screen */
    putchar(ch);
    ch = getc(fp);
  }
 if (feof(fp))
     printf("\n Reached the end of file.");
  else
     printf("\n Something gone wrong.");
  fclose(fp);
  getchar();
  return 0;
}
Cfputc() function In C language, fputc() function is used to write a character to the file. Syntax:
int fputc( int c, FILE * stream );
#include <stdio.h>
int main(){ 
   FILE *fp; 
   /* file open */
   fp = fopen("myfilee.txt", "w");   
   /* writing single character into file */
   fputc('a',fp);
   fclose(fp); 
}
C fprintf()function: The C printf() function is used  to write the characters into file. Syntax:
int fprintf(FILE *stream, const char *format [, argument, ...])
Example
#include <stdio.h>
main(){ 
   FILE *fp; 
   fp = fopen("myfilee.txt", "w"); 
   fprintf(fp, "Helloworld  file by fprintf...\n"); 
   fclose(fp);
}
C fscanf()function: The C fscanf() is used to read the characters from file. It reads a word from the file and returns EOF at the end of file. Syntax:
int fscanf(FILE *stream, const char *format [, argument, ...])
Example
#include <stdio.h>
int main(){ 
   FILE *fp; 
   char buff[255]; 
   fp = fopen("myfilee.txt", "r"); 
   while(fscanf(fp, "%s", buff)!=EOF){ 
   printf("%s ", buff ); 
   } 
   fclose(fp); 
}

C fputs() function C fputs() function is used to write a line of characters into file  and return outputs string to a stream. Syntax:
int fputs(const char *s, FILE *stream)
Example
#include<stdio.h>
int main(){ 
FILE *fp; 
fp=fopen("myfilee.txt","w"); 
fputs("Hello You are Good Programmer .",fp); 
fclose(fp); 
return 0;
}

C fgets() function The fgets() functionis used to read a line of character from file and get string from a stream. Syntax: char* fgets(char *s, int n, FILE *stream)  Example
#include
int main(){
FILE *fp;
char text[200];
fp=fopen("myfilee.txt","r");
printf("%s",fgets(text,100,fp));
fclose(fp);
return 0;
}
Output :
Hello You are Programmer

Related Topics

How to Find Square Free Numbers in C?

Introduction Square-free number program is a typical interview and academic question in C Programming. In this section, we will define square-free integers, construct algorithms and C program to print the nth...

11 minutes read.

C Array

An array is a collection of elements that are used to hold the fixed number of values of the same type. We cannot change the size and type of array...

2 minutes read.

Addition of Matrix in C

The mathematical operation of includingor greater matrices is called the addition of matrices. A matrix is a square series of numbers, symbols, words, letters, and different things. In this article,...

3 minutes read.

Range of int in C

In this session we know about the rage of the int in C language with the suitable examples and the programs. Firstly, we can learn the range of int: The int is...

3 minutes read.

Find out Power without using POW function in C

Introduction: In this discussion, we will discuss how we find out power without using the POW function in C. In this article, you'll understand how to compute integer powers in...

3 minutes read.

7 Best IDEs for C/C++ Developers in 2024

As we all know that in programming languages, C is known as the building block which cannot be contradicted, and C++ is the prolong kind of C and it can...

4 minutes read.

Fee Management System in C

The concept is to split every operation into its very own characteristic. Software is made from all of the capabilities mixed with transfer cases. An example of the capabilities may...

5 minutes read.

memcpy() in C

Introduction: Here we discuss about the memcpy() function in C. The memcpy() function is also called the Copy Memory Block function. Used to create a copy of a specific drawing...

4 minutes read.

Difference between while and for loop in C

While Loop The syntax that can be used for the ‘while’ loop in the C programming language is mentioned below: while (test expression or termination condition)  {   // the main body of the...

6 minutes read.

Error handling in C

Error handling in C: The C programming standard does not provide direct convenience for handling the errors. However, being a system programming language, it definitely will give access to handling...

4 minutes read.

Variables in C

A variable can be defined as a name allocated to a storage space that can be manipulated by our programs. Every variable in C arbitrates about the overall layout and...

5 minutes read.

String Handling functions in C

String :- The String is the collection of characters. Every String ends with the null character, and the String is enclosed in the double quotations .ie, "javaTpoint". If we see any...

5 minutes read.

Selection sort in C

In the C standard, the selection sorting technique exists where the smallest among the unsorted elements of the array is selected at each time and is then inserted into its...

3 minutes read.

Multi-dimensional Arrays in C

Multi-dimensional Arrays in C The C programming allows the concept of multi-dimensional arrays. The data within the multidimensional arrays are stored in the tabular form, that is, in a row significant...

3 minutes read.

Top Array Keywords in C

Sorting array in C Types of array in C Array of structure in C How do you initialize an array in C Array declaration in C Array of strings in C Multidimensional array in C Reverse array...

1 minute read.

C program to compare the two strings

C program to compare the two strings Strings can be compared either by using the string function or without using string function. First, we will look at how we can compare the strings with...

4 minutes read.

Runtime vs compile time in C

In the C programming language, the often used terms in every step consist of compile time and runtime. The compile time is referred to the source code that will be...

4 minutes read.

Associativity of Operators in C

What is an Associativity operator? Two operators with equal priority are connected by employing their association when they are present in an expression. There are two different types of associativity: left to rightright...

4 minutes read.

Pre-increment and Post-increment in C

The rich set of operators is supported by the c language. In c there are several operators used. The mathematical operations in the programming language are done with the operators...

4 minutes read.

Break, continue and goto statement in C

Break statement Break statement is used to break the process of a loop (while, do while and for) and switch case. Syntax: break; Example 1 while(test Expression) { // codes if(condition for break){ break; } // codes } Example 2 For(int it, condition,...

1 minute read.