0% found this document useful (0 votes)
186 views

File Handling Programs

The documents provide C program code examples for performing various file operations: 1. Creating a file and writing data to it including a person's name, age, and salary. 2. Reading the contents of a file by getting each character and printing it. 3. Deleting a specific line from a text file by copying the file contents to a new file except for the line number specified. 4. Replacing a specified line in a text file by reading the original file, writing to a new file, and allowing the user to input a replacement line when the specified line number is reached. 5. Counting the number of lines in a text file by reading each character and incrementing a

Uploaded by

Umesh Kanade
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
186 views

File Handling Programs

The documents provide C program code examples for performing various file operations: 1. Creating a file and writing data to it including a person's name, age, and salary. 2. Reading the contents of a file by getting each character and printing it. 3. Deleting a specific line from a text file by copying the file contents to a new file except for the line number specified. 4. Replacing a specified line in a text file by reading the original file, writing to a new file, and allowing the user to input a replacement line when the specified line number is reached. 5. Counting the number of lines in a text file by reading each character and incrementing a

Uploaded by

Umesh Kanade
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

1. /* C program to create a file called emp.

rec and store


information
about a person, in terms of his name, age and salary.*/
#include <stdio.h>
​void​ main​()
{
FILE ​*f ​ ptr​;
​char​ name​[20];
​int​ age​;
​float​ salary​;
​/* open for writing */
fptr ​=​ ​fopen("emp.rec",​ ​"w");

​if​ ​(​fptr ​==​ NULL​)


​{
​printf("File does not exists ​\n​");
​return;
​}
​printf("Enter the name ​\n​");
​scanf("%s",​ name​);
​fprintf(​fptr​,​ "​ Name = %s​\n​",​ name​);
​printf("Enter the age​\n​");
​scanf("%d",​ & ​ ​age​);
​fprintf(​fptr​,​ "​ Age = %d​\n​",​ age​);
​printf("Enter the salary​\n​");
​scanf("%f",​ & ​ ​salary​);
​fprintf(​fptr​,​ "​ Salary = %.2f​\n​",​ salary​);
​fclose(​fptr​);
}

2​.​ /* C program to illustrate how a file stored


on the disk is read */
#include <stdio.h>
#include <stdlib.h>
​void​ main​()
{
FILE ​*f​ ptr​;
​char​ filename​[15];
​char​ ch​;
​printf("Enter the filename to be opened
\n");
​scanf("%s",​ filename​);
​/* open the file for reading */
fptr ​=​ ​fopen(​filename​,​ ​"r");
​if​ ​(f​ ptr ​==​ NULL​)
​ {
​printf("Cannot open file \n");
​exit(0);
​ }
ch ​=​ ​fgetc(​fptr​);
​while​ ​(c​ h ​!=​ EOF​)
​ {
​printf​ ​("%c",​ ch​);
ch ​=​ ​fgetc(​fptr​);
​ }
​fclose(​fptr​);
}

3.​ /* C Program Delete a specific Line from a Text File */


#include <stdio.h>
​int​ main​()
{
FILE ​*f​ ileptr1​,​ ​*​fileptr2​;
​char​ filename​[40];
​char​ ch​;
​int​ delete_line​,​ temp ​=​ ​1;

​printf("Enter file name: ");


​scanf("%s",​ filename​);
​//open file in read mode
fileptr1 = ​ ​ f​ open(​filename​,​ ​"r");
ch =​ ​ g​ etc(​fileptr1​);
​while​ (​ ​ch !​ =​ EOF​)
{​
​ rintf("%c",​ ch​);
p
ch = ​ ​ g​ etc(​fileptr1​);
​}
​//rewind
r​ ewind(​fileptr1​);
​printf(" ​\n​ Enter line number of the line to be deleted:");
​ canf("%d",​ &
s ​ ​delete_line​);
​//open new file in write mode
fileptr2 = ​ ​ f​ open("replica.c",​ "​ w");
ch = ​ ​ g​ etc(​fileptr1​);
​ hile​ (​ ​ch !​ =​ EOF​)
w
​{
ch = ​ ​ g​ etc(​fileptr1​);
i​ f​ (​ ​ch = ​ =​ '​ ​\n​')
temp​++;
​//except the line to be deleted
i​ f​ (​ ​temp !​ =​ delete_line​)
​{
​//copy all lines in file replica.c
​ utc(​ch​,​ fileptr2​);
p
​}
​}
f​ close(​fileptr1​);
f​ close(​fileptr2​);
r​ emove(​filename​);
​//rename the file replica.c to original name
​rename("replica.c",​ filename​);
​printf("​\n​ The contents of file after being modified are as
follows:​\n​");
fileptr1 ​=​ ​fopen(​filename​,​ ​"r");
ch = ​ ​ g​ etc(​fileptr1​);
​while​ (​ ​ch !​ =​ EOF​)
​{
​ rintf("%c",​ ch​);
p
ch ​=​ ​getc(​fileptr1​);
​}
​fclose(​fileptr1​);
​return​ 0​ ;
}

4.​ /* C Program to Replace a specified Line in a Text File */


#include <stdio.h>

int​ main​(void)
{
FILE ​*f ​ ileptr1​,​ ​*​fileptr2​;
​char​ filechar​[40];
​char​ c​;
​int​ delete_line​,​ temp ​=​ ​1;

​printf("Enter file name: ");


​scanf("%s",​ filechar​);
fileptr1 = ​ ​ f​ open(​filechar​,​ ​"r");
c ​=​ ​getc(​fileptr1​);
​//print the contents of file .
​while​ (​ ​c !​ =​ EOF​)
​{
​ rintf("%c",​ c​);
p
c ​=​ ​getc(​fileptr1​);
​}
​printf(" ​\n​ Enter line number to be deleted and replaced");
​scanf("%d",​ & ​ ​delete_line​);
​//take fileptr1 to start point.
​rewind(​fileptr1​);
​//open replica.c in write mode
fileptr2 = ​ ​ f​ open("replica.c",​ ​"w");
​ ​ g​ etc(​fileptr1​);
c =
​while​ (​ ​c !​ =​ EOF​)
​{
​if​ ​(​c ​==​ ​'n')
​{
temp​++;
​}
​//till the line to be deleted comes,copy the content to
other
​if​ ​(​temp ​!=​ delete_line​)
​{
​putc(​c​,​ fileptr2​);
​}
​else
​{
​while​ ​((​c ​=​ ​getc(​fileptr1​))​ ​!=​ ​'n')
​{
​}
​//read and skip the line ask for new text
​printf("Enter new text");
​//flush the input stream
​fflush(​stdin​);
​putc('n',​ fileptr2​);
​//put 'n' in new file
​while​ (​ (​c =
​ ​ g​ etchar())​ ​!=​ ​'n')
​ utc(​c,​ ​ fileptr2​);
p
​//take the data from user and place it in new file
​fputs("n",​ fileptr2​);
temp​++;
​}
/​ /continue this till EOF is encountered
c ​=​ ​getc(​fileptr1​);
​}
​fclose(​fileptr1​);
​fclose(​fileptr2​);
​remove(​filechar​);
​rename("replica.c",​ filechar​);
fileptr1 = ​ ​ f​ open(​filechar​,​ ​"r");
​//reads the character from file
c = ​ ​ g​ etc(​fileptr1​);
​//until last character of file is encountered
​while​ (​ ​c !​ =​ EOF​)
​{
​printf("%c",​ c​);
/​ /all characters are printed
c ​=​ ​getc(​fileptr1​);
​}
​fclose(​fileptr1​);
​return​ 0​ ;
}
5.​ /* C Program to Find the Number of Lines in a Text File */
#include <stdio.h>

int​ main​()
{
FILE ​*​fileptr​;
​int​ count_lines ​=​ ​0;
​char​ filechar​[40],​ chr​;

​printf("Enter file name: ");


​scanf("%s",​ filechar​);
fileptr ​=​ ​fopen(​filechar​,​ ​"r");
​//extract character from file and store in chr
chr ​=​ ​getc(​fileptr​);
​while​ ​(​chr ​!=​ EOF​)
​{
​//Count whenever new line is encountered
​if​ ​(​chr ​==​ ​'n')
​{
count_lines ​=​ count_lines ​+​ ​1;
​}
​//take next character from file.
chr ​=​ ​getc(​fileptr​);
​}
​fclose(​fileptr​);​ ​//close file.
​printf("There are %d lines in %s in a file​\n​",​ count_lines​,​ filechar​);
​return​ ​0;
}

6.​ /* C Program to Append the Content of File at the end of Another */


#include <stdio.h>
#include <stdlib.h>
main​()
{
FILE ​*​fsring1​,​ ​*​fsring2​,​ ​*​ftemp​;
​char​ ch​,​ file1​[20],​ file2​[20],​ file3​[20];

​printf("Enter name of first file ");


​gets(​file1​);
​printf("Enter name of second file ");
​gets(​file2​);
​printf("Enter name to store merged file ");
​gets(​file3​);
fsring1 ​=​ ​fopen(​file1​,​ ​"r");
fsring2 ​=​ ​fopen(​file2​,​ ​"r");
​if​ ​(​fsring1 ​==​ NULL ​||​ fsring2 ​==​ NULL​)
{​
​perror("Error has occured");
​printf("Press any key to exit...​\n​");
​exit(​EXIT_FAILURE​);
}​
ftemp ​=​ ​fopen(​file3​,​ ​"w");
​if​ ​(​ftemp ​==​ NULL​)
{​
​perror("Error has occures");
​printf("Press any key to exit...​\n​");
​exit(​EXIT_FAILURE​);
}​
​while​ ​((​ch ​=​ ​fgetc(​fsring1​))​ ​!=​ EOF​)
​fputc(​ch​,​ ftemp​);
​while​ ​((​ch ​=​ ​fgetc(​fsring2​)​ ​)​ ​!=​ EOF​)
​fputc(​ch​,​ ftemp​);
​printf("Two files merged %s successfully.​\n​",​ file3​);
​fclose(​fsring1​);
​fclose(​fsring2​);
​fclose(​ftemp​);
​return​ ​0;
}

7. ​/* C Program to List Files in Directory */


#include <dirent.h>
#include <stdio.h>

int​ main​(void)
{
DIR ​*​d​;
​struct​ dirent ​*​dir​;
d ​=​ opendir​(".");
​if​ ​(​d​)
{​
​while​ (​ (​dir ​=​ readdir​(​d​))​ ​!=​ NULL​)
{​
​printf("%s​\n​",​ dir​->​d_name​);
​}
closedir​(​d​);
​}
​return(0);
}

8.​ /* C Program to Find the Size of File using File Handling Function*/
#include <stdio.h>

void​ main​(int​ argc​,​ ​char​ ​**​argv​)


{
FILE ​*​fp​;
​char​ ch​;
​int​ size ​=​ ​0;

fp ​=​ ​fopen(​argv​[1],​ ​"r");


​if​ ​(​fp ​==​ NULL​)
​printf("​\n​File unable to open ");
​else
​printf("​\n​File opened ");
​fseek(​fp​,​ ​0,​ ​2);​ ​/* file pointer at the end of file */
size ​=​ ​ftell(​fp​);​ ​/* take a position of file pointer un size variable */
​printf("The size of given file is : %d​\n​",​ size​);
​fclose(​fp​);
}

9.

You might also like