Programming Assignment - 2
Programming Assignment - 2
1. Introduction to Files in C:
A File is a collection of data stored in the secondary memory. So far data was entered
into the programs through the keyboard. So Files are used for storing information that can be
processed by the programs. Files are not only used for storing the data, programs are also stored
in files. In order to use files, we have to learn file input and output operations. That is, how data
is read and how to write into a file.
A Stream is the important concept in C. The Stream is a common, logical interface to the
various devices that comprise the computer. So a Stream is a logical interface to a file. There are
two types of Streams, Text Stream and Binary Stream. A Text File can be thought of as a stream
of characters that can be processed sequentially. It can only be processed in the forward
direction.
2. Using Files in C:
To use a file four essential actions should be carried out. These are,
a. Declare a file pointer variable.
b. Open a file using the fopen() function.
c. Process the file using suitable functions.
d. Close the file using the fclose() and fflush() functions.
Examples:
/* Program to check whether the file exist or not */
void main()
{
FILE *fp;
char *x;
clrscr();
printf("\n enter the file name : ");
gets(x);
fp=fopen(x,"r");
if(fp==NULL)
{
printf("The file ---%s--- is not found in the present directory",x);
}
else
{
printf("The file ---%s--- is found in the present directory",x);
}
fclose(fp);
getch();
}
Two functions are used to read the character from the file and write it into another file. These
functions are getc() & putc() functions.
Its prototype is,
int getc(FILE *file_pointer);
putc( char const_char, FILE *file_pointer);
Examples:
/* Read a string into a text file */
void main()
{
FILE *fp;
char text[80];
int i;
clrscr();
fp=fopen("Sample.txt","w");
printf("\n Enter the text : ");
gets(text);
for(i=0;i<strlen(text);i++) // Read a stream of charcters
putc(text[i],fp);
if(fp!=NULL)
printf("\n The string copied into a text file ");
fclose(fp);
getch();
}
/* read and write the content of the file using fprintf() and fscanf() functions */
void main()
{
FILE *fptr;
char name[20];
int age;
float salary;
clrscr();
/* open for writing */
fptr = fopen("abc.txt", "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 = %f\n", salary);
getch();
fclose(fptr);
}
7. File of Records:
Most C program files may be binary files, which can logically be divided into fixed-length
records. Each record will consist of data that conforms to a previously defined structure. In C, This
structure can be formed using a struct data type. The records are written into disk sequentially. This
happens because as each record is written to disk, the file position indicator is moved to the bye
immediately after the last byte in the record just written. Binary files can be written sequentially to the
disk or in a random access manner.
Example:
/* program to copy the content from one file to another file using fseek() function. */
void main()
{
/*
File_1.txt is the file with content and,
File_2.txt is the file in which content of File_1
will be copied.
*/
FILE *fp1, *fp2;
char ch;
int pos;
clrscr();
if ((fp1 = fopen("File_1.txt","r")) == NULL)
{
printf("\nFile cannot be opened");
return;
}
else
{
printf("\nFile opened for copy...\n ");
}
fp2 = fopen("File_2.txt", "w");
fseek(fp1, 0L, SEEK_END); // file pointer at end of file
pos = ftell(fp1);
fseek(fp1, 0L, SEEK_SET); // file pointer set at start
while (pos--)
{
ch = fgetc(fp1); // copying file character by character
fputc(ch, fp2);
}
getch();
fcloseall();
}
Example:
int main()
{
char file[80];
/* prompt for filename to delete */
printf(“File to delete :”);
gets(file);
/* delete the file */
if(remove(file)==0)
printf(“removed %s”,file);
else
printf(“file cannot be removed”);
}
Step 2: Save the above function using [ .h ] extension. and compile the code.
Example:
above function can be save as myhead.h.
Step 3: Write the main program, include our own header file in the program. Now main function
automatically includes the function available in the header file.
Example:
#include<stdio.h>
#include ”myhead.h”
void main()
{
int num1=10,num2=20,num3;
` num3=add(num1,num2);
printf(“\n Sum of two numbers is : %d”,num3);
}
Note : When running the program, header file and the program must be present in the same folder.