×

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

Header files in C

Header files in C In the C programming language, header files are present, which have an extension of ‘.h’, and it consists of macro definitions, declarations, and so on that are...

4 minutes read.

Distance Vector Routing Protocol Program in c

A distance-vector routing protocol is one of the foremost instructions of routing protocols in pc conversation principle for packet-switched networks. The hyperlink-nation protocol is the alternative foremost class.The Bellman-Ford set...

4 minutes read.

Fibonacci series in C

We have learned about the Fibonacci series in mathematics. For a quick recap, A Fibonacci series is the sequence of numbers following a certain pattern i.e., the next number should...

3 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.

Comma Operator in C

What is a comma operator? The comma operator in the C programming language has the least priority. The comma operator is essentially a binary operator that operates on the first operand...

4 minutes read.

Unformatted input() and output() function in C

Unformatted Input and Output functions take the character, character array, and strings as input. We can read-only character data types with these functions. These functions are already defined in the...

5 minutes read.

Different ways to Declare the Variable as Constant in C

There are a wide range of ways of making the variable as steady Using const keyword: The const catchphrase permits a software engineer to inform the compiler that a specific variable should...

5 minutes read.

Recursion in C

Recursion is a programming technique that allows the programmer to call the function within the same function. The function which calls the same function is known as recursive function but a...

1 minute read.

Heap Sort in C

In this tutorial, we will learn about heap sorting in C language, but before going to Heap sort, we have to know the concept of Complete Binary Tree. What is Complete...

8 minutes read.

Use of fflush(stdin) in C

Use of fflush(stdin) in C Usually, fflush() is only used for the output stream. The purpose is to clean (or flush) the output buffer and transfer the buffered data into...

2 minutes read.

Derived Data Types in C

A variable in a program occupies some space in the computer's memory where some value is stored. Each variable in C has an associated data type. A value to be...

4 minutes read.

C vs Java Strings

String in C In C, we can define a string as a bunch of characters. A character array is distinguished from a string by the presence of the special character '\0'...

5 minutes read.

Purpose of a Function Prototype in C

A function prototype in C is a declaration of a function that specifies the function's name, return type and parameters. It has the following syntax: return_type function_name(parameter_list); For example, the prototype for...

4 minutes read.

Macros in C

In the C programming language, the macro is defined as a segment of the code replaced by the macro defined value at the beginning. ‘#define’ is the directive that defines...

3 minutes read.

Decimal to Binary in C

What is a decimal number? A decimal number is a number represented in the decimal number system. This system of binary conversion uses base 10 to represent numbers, i.e. the digits...

3 minutes 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.

Limitations of Inline Function in C

The compiler is unable to conduct inlining in two instances. It just accepts the inline definitions and produces space for the function in the same way it does for a...

3 minutes read.

How to return a Pointer from a Function in C?

In the C programming language, pointers are variables that hold the address memory location of another variable. We could also input pointers to a function and return pointers from a...

3 minutes read.

Calendar application in C

Introduction to the calendar application We all are familiar with the calendar. It plays a crucial role in our daily life. We run toward the calendar whenever we want to know...

6 minutes read.

Merge and Merge sort with example in C

Assume we have two ordered Integer arrays, a[] and b[]. If we wish to combine them into another ordered array, such as c[], we can use the following straightforward technique. Compare...

3 minutes read.