Untitled Document

Files


Opening and closing of data files:


Files are the area in secondary device where the data can be stored.
The first step is to establish a buffer area when working with a stream oriented data files, where information is temporarily stored while it is transmitted between computers memory and data files.

Because of the concept of buffer area the information can be written or read from data files more rapidly. Otherwise the speed could not be achieved.

The buffer area is established by writing the statement FILE *ptr where FILE is a special structure type that establishes the buffer area and ptr is a pointer variable that indicates the beginning of the buffer area. The structure type FILE is defined within the header file stdio.h.

The pointer ptr is often referred to as stream pointer or simply as a stream.



Note:
Use upper case letters to write FILE

A data file must then be opened before it can be created or processed. This associates the file name with the buffer area.
It also specifies how the data file will be utilized i.e., read only, write only, append or read/write etc.
The library function fopen() can be used to open the file in different modes.



Syntax:
ptr = fopen (file name, file type);
Where file name and file type are strings that represent the name of the data file and the manner in which the data file will be utilized respectively.
The name chosen for the file name must be consistent with the values for naming files as

determined by the operating system of the computer. The file type must be one of the strings r, w, a, r+, w+, a+
r opens an existing file for reading only.
w opens a new file for writing only, if a file with specified file name exists, it will be destroyed and a new file is created.

A new file will be created if a file with the specified file name does not exist.

r+ opens an existing file for both reading and writing. If a file with the given name doesn't exist it will not be created newly.
w+ opens a new file for both reading and writing.
If a file with the specified file name currently exists it will be destroyed and a new file will be created in its place.

a+ opens an existing file for reading and appending.
A new file will be created if the file with the specified file name does not exist.
The fopen() function returns a pointer to the beginning of a buffer area associated with the file. A null value is returned if a file can't be opened i.e., when the given file is not found.
Finally a data file must be closed at the end of the program.
This can be accomplished with the library function fclose().


The syntax:
fclose (pointer variable);


Example
:
fclose (ptr);
The pointer variable associated with the respective file.


Creating a data file:

A data file must be created before it can be processed. This data file can be created in two ways.
One way is to create the file directly using a text editor or a word processor.
The other is to write a program that enters information into the computer and then writes it out into the data file.
If the data file consists of individual characters the library functions getchar() and putc() can be used to recieve the data from the keyboard and to write it out into the data file.


The syntax:
putc (character to be written into the data file, ptr);

Note:
ptr is the pointer variable associated with the respective file buffer


/*The following program accepts characters from the keyboard and writes into the data file tst.dat*/
# include<conio.h>
#include <stdio.h>
void main ( )
{
FILE *ptr;
char c;
printf ("Enter the characters \n");
ptr = fopen("tst.dat", "w");
do
{
putc (c=getchar( ),ptr);
} while (c != EOF);
fclose (ptr);
getch();
}


Output:

Contents of the file tst.dat:



Read a data file and display its contents:

The library function getc() will read the individual characters from data file and library function putchar() will display them onto the screen.


The Syntax:

c=getc (ptr);
Where c is a character variable and ptr is the pointer variable associated with the file buffer.


/*The following example which reads the characters from the data file tst.dat and displays the contents on the monitor */
#include <stdio.h>
#include <conio.h>
void main()
{
FILE *ptr;
char c;
if ((ptr=fopen("tst.dat", "r"))==0)
printf("File cannot be opened \n");
else
{
do
{
putchar (c = getc(ptr));
} while(c!= EOF);
fclose (ptr);
}
getch();
}


Output:



Program to
    a) Read a line of characters from the keyboard and write it on to a file.
    b) Read a line of characters from the file in which it is written and display it on the screen.

# include <stdio.h>
#include <conio.h>
void main ( )
{
FILE *ptr;
char c;
printf ("Enter the characters\n");
ptr = fopen ("tst.dat", "w");
do
{
putc(c = getchar(),ptr);
} while (c!= EOF);
fclose (ptr);
printf("\nEntered characters written in the file\n\n\n");
printf("Characters are read from the file and displayed on the screen\n");v ptr = fopen("tst.dat", "r");
do
{
putchar (c=getc(ptr)); } while (c!= EOF);
fclose (ptr);
}

Output:



Note:
After typing line of characters, to signify the EOF, press Ctrl key and holding it down, press z.

Before opening a file in different mode you should close the file. So after the file was opened in write mode, before opening it in the read mode it was closed. It is always a good practice to close the file at the end of the program.
More functions used to perform input and output operations on files:

Data files containing records can be processed using the library functions fscanf() and fprintf(). Thus fscanf() function permits formatted data to be read from data file associated with particular strings and fprintf() permits formatted data to be written to the data file. The actual format specifications are same as those used with the scanf() and printf() functions.



/*C Program to read a text file and convert lowercase to uppercase letters*/
#include<stdio.h>
#include <conio.h>
#include <ctype.h>
void main( )
{
FILE *ptvar;
int i;
char a[100], c;
clrscr( );
i=0;
ptvar=fopen("text1.dat","w");
printf ("Enter the text \n");
do
{
putc(c = getchar( ), ptvar);
} while (c!=EOF);
fclose (ptvar);
ptvar=fopen("text1.dat","r");
do
{
c= getc(ptvar);
a[i]=c;
i++;
}while (c!=EOF);
a[i]='\0';
fclose(ptvar);
i=0;
while (a[i]!='\0')
{
(isupper(a[i]))? (a[i]=tolower(a[i])): (a[i]=toupper(a[i]));
i++ ;
}
printf("\nThe resultant string is\n %s",a);
getch( );
}


Output



/*C Program to read a text file and accumulate the occurrence of each character in two dimensional array and output the frequency*/
#include <stdio.h>
#include <conio.h>
#include <ctype.c>
void main( )
{
FILE *ptvar;
char c;
int j, a=0, l=0, k=0,w;
static int b[26][26];
clrscr();
ptvar=fopen("text1.dat","w");
printf ("Enter the string : \n");
do
{
c = getchar( );
putc(toupper(c),ptvar);
if (c== '\n' || c==EOF)
{
l++;
}
}while(c!=EOF);
fclose (ptvar);
flushall( );
ptvar=fopen("text1.dat","r");
while((c=getc(ptvar))!=EO
{
while(c == '\n')
{
a++;
break;
}
k = c-65;
b[a][k]=b[a][k]+1;
}
printf("Frequency of characters is \n");
for(w=0; w<26;w++)
{
printf("%c ", (65+w));
}
printf("\n");
for(w=0; w<l-1;w++)
{
for(j=0; j<26;j++)
{ printf("%d ", b[w][j]);
}
printf("\n");
}
fclose(ptvar);
getch( );
}


Output:





User Defined Functions << Previous
Next >> Pointers

Our aim is to provide information to the knowledge seekers.

Support us generously

comments powered by Disqus
click here


click here






Natural Natural Natural Footer1