×
FREE ASSISTANCE FOR THE INQUISITIVE PEOPLE
Tutorial Topics
X
softetechnologies
Reverse the digits of an integer - WBUT MAKAUT MCA 2012 auto and static variables, Sort the character, factorial of a Number
Different function for File Handling and spiting a text file - WBUT MAKAUT MCA 2012 - C Language
547    Arnab De    12/04/2020

What are the different functions used for file handing? Write a C program to create a file contains a series of integer numbers and then reads all numbers of this file and writes all odd numbers to other file called odd and writes all even numbers to a file called.

Some functions are used for file handling in C Language

  • fopen()
  • fprintf()
  • fscanf()
  • fclose()
  • fcloseall()
  • fgetch()
  • fputch()
  • fgets()
  • fread()
  • fwrite()
  • fseekp()
  • fseekg()
  • ftell()
softetechnologies

Now, Algorithm of the programe

Step 1: Open a file pointer in write mode for craete a file of series of numbers (numbers.txt).

Step 2: read the numbers from keyboard and put into numbers.txt. press 0 to stop the series.

Step 3: Closed the said file pointer.

Step 4: Open the same file again but in read mode.

Step 5: Open another two file (odd.txt) and (even.txt) in write mode.

Step 6: Read the numbers from first file one by one and check it even or odd. If it is even the put in in even.txt otherwise put it into odd.txt

Step 7: Closed all file pointers using fcloseall() function.

#include<stdio.h>

void main()
{
	FILE *num,*odd,*even;
	int n=-1;
	num=fopen("numbers.txt","w");
	while(n!=0)
	{
		printf("Enter A Number (0 [Zero] to stop): ");
		scanf("%d",&n);
		if(n==0){break;}
		fprintf(num,"%d \n",n);
	}
	fclose(num);
	num=fopen("numbers.txt","r");
	odd=fopen("odd.txt","w");
	even=fopen("even.txt","w");
	while(fscanf(num,"%d",&n)!=EOF)
	{
		if(n%2==0)
		{
			fprintf(even,"%d\n",n);
		}
		else
		{
			fprintf(odd,"%d\n",n);
		}
	}
	fcloseall();
}

Data of number files (numbers.txt)

1
2
3
4
5
6
7
8
9
10
25
29
34
65
88
93

Data of odd files (odd.txt)

1
3
5
7
9
25
29
65
93

Data of odd files (even.txt)

2
4
6
8
10
34
88

Reverse the digits of an integer - WBUT MAKAUT MCA 2012 auto and static variables, Sort the character, factorial of a Number
softetechnologies
Author Details
Arnab De
I have over 16 years of experience working as an IT professional, ranging from teaching at my own institute to being a computer faculty at different leading institute across Kolkata. I also work as a web developer and designer, having worked for renowned companies and brand. Through tutorialathome, I wish to share my years of knowledge with the readers.
Enter New Comment
Comment History
No Comment Found Yet.
Albert Einstein
Learn from yesterday, live for today, hope for tomorrow.
Albert Einstein
2707
34.21
Today So Far
Total View (Lakh)
softetechnologies
01/01/2018     33611
28/06/2017     29530
02/08/2017     27024
26/05/2018     26987
15/05/2017     23239
06/07/2017     21773
01/08/2017     21320
25/06/2018     20150
21/04/2018     18620
14/07/2017     16470
softetechnologies