Opening and closing of a data file in C

Many applications require that information be written or read from an auxiliary storage device. Such information is stored on the device in the form of a data file. Thus, a data file allows us to store information permanently and to access and alter that information whenever necessary. There are two types of data files, one is stream-oriented (or standard) data files and second is system-oriented (or low-level) data files.

When working with a stream - oriented data file, the first step to establish a buffer area, where information is temporarily stored while being transferred between computers memory and the data file. The buffer area is established by writing

FILE * Pointer var;

Where, FILE is a special structure type that establishes the buffer area and pointer variable indicates the beginning of buffer area. The FILE is defined in a, file "stdio.h".
A data file must then be opened before it can be created and processed. The library function 'fopen' is used to open a file. The syntax of fopen is

pintervar = fopen (filename, mode)

where, filename is name of datafile which you wish to open and mode indicates the mode in which this file will get open up. The modes in which the user can open a file are :

Modes Meaning

  • "r" Open an existing file for reading only.
  • "w" Open a new file for writing. If already exist then create new one by deleting old one.
  • "a" Open an existing for appending.
  • ( To append means add at the end).
  • "r + " Open an existing file both for reading and writing.
  • "w + " Open a new file both for reading and writing.
  • "a + " Open an existing file both for reading and appending.

The fopen function returns a pointer to the beginning of buffer area associated with the file. A NULL value is returned if the file cannot be opened, for ex., when an existing data file cannot be found.

A data file opened using fopen must be closed at the end of the program. A library function fclose is used for this purpose.
The syntax for fclose is :

fclose (pointervar);

Some applications involved the use of data files to store block of data, where each block consist of a fixed number of continuous bytes. Each block will generally represent a complex data structure such as a structure or a array. For such application, it may be desirable to read the entire block from the data file or write the entire block to the data file, rather than reading and writing a individual component.

The library functions fread and fwrite are used for this purpose. These function are often referred to as unformatted read & write functions. Similarly, data file of this type are referred to as unformatted data file.

In syntax of each of these function requires four arguments:

1) a pointer to a data block,
2) the size of data block
3) the number of data blocks being transferred and
4) the pointer variable attached to the file.

The syntax for fwrite is :

fwrite(ptr, sizeof (block), no of data blocks, pointerver)
Ex. fwrite(&bank, sizeof (record), 1, fs);

FILE *fs, *ft;
char ch;
if((fs= fopen("c :\\biol.c", "rt")) == NULL)
puts("File not found."); exit(0);
if((ft = fopen("c:\\bio1 .d", "w")) = = NULL)
puts("Target file cannot be created.");
while((ch = getc (fs))!= EOF)
putc(ch, ft);
puts("File copied successfully.");

Related Links :

No comments:

Post a Comment

If you face any Problem in viewing code such as Incomplete "For Loops" or "Incorrect greater than or smaller" than equal to signs then please collect from My Web Site CLICK HERE

More Useful Topics...


History Of C..

In the beginning was Charles Babbage and his Analytical Engine, a machine
he built in 1822 that could be programmed to carry out different computations.
Move forward more than 100 years, where the U.S. government in
1942 used concepts from Babbage’s engine to create the ENIAC, the first
modern computer.
Meanwhile, over at the AT&T Bell Labs, in 1972 Dennis Ritchie was working
with two languages: B (for Bell) and BCPL (Basic Combined Programming
Language). Inspired by Pascal, Mr. Ritchie developed the C programming

My 1st Program...

void main ()
clrscr ();
printf ("\n\n\n\n");
printf ("\t\t\t*******Pankaj *******\n");
printf ("\t\t\t********************************\n");
printf ("\t\t\t\"Life is Good...\"\n");
printf ("\t\t\t********************************");
getch ();

Next Step...


void main ()
clrscr ();
printf ("\n\n\n\n\n\n\n\n");
printf ("\t\t\t --------------------------- \n\n");

printf ("\t\t\t | IGCT, Info Computers, INDIA | \n\n");
printf ("\t\t\t --------------------------- ");

getch ();