Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 5

PDPU, Gandhinagar File Handling in C Language

Program1: Create the file on the hard disk and store data within that file.

/* Logic:
1) Open physical file on the hard disk (e.g.my1stfl.dat) and assign it to
logical file name e.g. fpw.
2) Accept a character from the user.
3) Repeat step no. a to b until user enters ‘#’ symbol.
a. Write the character in the file.
b. Accept another character from the user.
4) Close the file. (Disconnect logical file from physical file.)
*/

#include <stdio.h>
#include <conio.h>
void main()
{
FILE * fpw = NULL;
char ch;

printf("Enter your details. At the end, put # sign.\n");


fpw = fopen("my1stfl.dat","w");
ch = fgetc(stdin);
while ( ch != '#')
{
fputc(ch,fpw);
ch = fgetc(stdin);
};
fclose(fpw);
printf("\nThe data has been successfully written within file.\n");
getch();
};

Program2:
// Read my1stfl.dat and display the contents on the screen.

/* Logic.
1. Open my1stfl.dat (for reading purpose)
2. read a character from the file.
3. repeat step no. 4 & 5 until end-of-file.
4. put a character (already read) on the monitor.
5. read another character from the file.
6. close the file.
7. stop the process.

*/
#include<stdio.h>
#include<conio.h>
#include <stdlib.h>

void main()
{
FILE * fpr = NULL;
char ch;
clrscr();
if ((fpr = fopen("my1stfl.dat","r"))==NULL)
{
printf("Sorry, I cannot open a file.\n");
getch();
exit(0);
};
ch = fgetc(fpr);
while ( ch != EOF )
{
By Darshit Shah 1
PDPU, Gandhinagar File Handling in C Language

printf("%c",ch);
// fputc(ch,stdout);
ch = fgetc(fpr);
};
fclose(fpr);
getch();
};

Program 3:
// Read my1stfl.dat and copy the content into another file.

/* Logic.
1. Open my1stfl.dat (for reading purpose)
2. Open my2ndfl.dat (for writing purpose)
3. read a character from my1stfl.dat.
4. repeat step no. 5 & 6 until end-of-file.
5. put a character (already read) in the my2ndfl.dat file.
6. read another character from my1stfl.dat.
7. close both files.
8. stop the process.
*/

#include<stdio.h>
#include<conio.h>
#include <stdlib.h>

void main()
{
FILE * fpr, * fpw;
fpr = fpw = NULL;
char ch;
clrscr();
if ((fpr = fopen("my1stfl.dat","r"))==NULL)
{
printf("Sorry, I cannot open a my1stfl.dat file.\n");
getch();
exit(0);
};
if ((fpw = fopen("my2ndfl.dat","w"))==NULL)
{
printf("Sorry, I cannot open a my2ndfl.dat file.\n");
fclose(fpr);
getch();
exit(0);
};
ch = fgetc(fpr);
while ( ch != EOF )
{
printf("%c",ch); // displays a character on the monitor.
fputc(ch,fpw);
ch = fgetc(fpr);
};
fclose(fpr);
fclose(fpw);
printf("File copied successfully.\n");
getch();
};

By Darshit Shah 2
PDPU, Gandhinagar File Handling in C Language

Program 4:
// Read my1stfl.dat and count no.of lines, words, characters, spaces, in it.

#include<stdio.h>
#include<conio.h>
#include <stdlib.h>

void main()
{
FILE * fpr = NULL;
int ln=0,noc = 0,whitespace=0,word=0;

char ch,prev_ch=NULL;
clrscr();
if ((fpr = fopen("my1stfl.dat","r"))==NULL)
{
printf("Sorry, I cannot open a file.\n");
getch();
exit(0);
};
ch = fgetc(fpr);
noc++;
while ( ch != EOF )
{
if (ch == ' ')
{
whitespace++;
if (prev_ch != ' ' && prev_ch != NULL)
word++;
}
else
if (ch == '\n')
{
ln++;
if (prev_ch != ' ' && prev_ch != '\n')
word++;
}
if (prev_ch != ' ' || ch != ' ') // displays only one space.
printf("%c",ch);
prev_ch = ch;
ch = fgetc(fpr);
noc++;
};
if (prev_ch != '\n')
ln++;
if (prev_ch != '\n' && prev_ch != ' ')
word++;

fclose(fpr);

printf("\nThere are %d characters in my1stfl.dat file.\n",noc);


printf("There are %d spaces in my1stfl.dat file.\n",whitespace);
printf("There are %d words in my1stfl.dat file.\n",word);
printf("There are %d lines in my1stfl.dat file.\n",ln);
getch();
};

By Darshit Shah 3
PDPU, Gandhinagar File Handling in C Language

Program 5: Instead of characters, accept one line at a time from the user and write
it into the file.

#include <stdio.h>
#include <conio.h>
#include <string.h>
#include <stdlib.h>

void main()
{
char s[80];
FILE * fpw = NULL;
clrscr();
if ((fpw = fopen("my3rdfl.dat","a")) == NULL)
{
printf("Can't open file.Taking Exit.");
getch();
exit(0);
}
printf("\n enter as many lines as you want. At the end leave blank line.\n");
while (strlen(gets(s)) > 0)
{
fputs(s,fpw);
fputs("\n",fpw);
};
fclose(fpw);
getch();
}

Program 6: Read a content of the file line by line.

#include <stdio.h>
#include <conio.h>
#include <string.h>
#include <stdlib.h>

void main()
{
char s[80];
FILE * fpr = NULL;
clrscr();
if ((fpr = fopen("my3rdfl.dat","r")) == NULL)
{
printf("Can't open file.Taking Exit.");
getch();
exit(0);
}
while (fgets(s,79,fpr) != NULL)
{
printf("%s",s);
};
fclose(fpr);
getch();
}

By Darshit Shah 4
PDPU, Gandhinagar File Handling in C Language

Program 7: Accept a record consisting of Roll No. and name from the user and store it in the file. The program
should terminate when user is not willing to enter more records.

// This program stores a particular record in a file.


#include <stdio.h>
#include <conio.h>
#include <string.h>
#include <stdlib.h>

void main()
{
struct info
{
char name[50];
int no;
};

struct info s;
char ans;

FILE * fpw = NULL;


clrscr();
if ((fpw = fopen("student.dat","w")) == NULL)
{
printf("Can't open file.Taking Exit.");
getch();
exit(0);
}
do
{
printf("\nEnter following Details:\n");
printf("Enter Number. : ");
scanf("%d",&s.no);
flushall();
printf("Enter Name. : ");
gets(s.name);
fprintf(fpw,"%d %s\n",s.no,s.name);
printf("Press Y or y to enter another record.\n");
ans = getche();
} while (ans == 'y' || ans == 'Y');
fclose(fpw);
getch();
}

By Darshit Shah 5

You might also like