Thursday 14 February 2013



In case of any file input-output error or erroneous command line arguments, it is possible to terminate program execution. This can be done through the exit() function as shown in the following example:

if( argc!= 3)
{
printf("Invalid arguments\n");
exit();
}

An integer may also be passed as a parameter to the exit() function. In UNIX, this value is stored in the environmental variable "$?", and should be in the range 0 - 255.

Posted by Unknown


The stdin, stdout and stderr are names used to refer to the standard input device (keyboard) and standard output and error device (VDU). These are actually FILE type pointers, defined in the file stdio.h, and can be used with file input-output functions.

For example, the following statement:

fputc (c, stdout);

writes the value of the char type variable c onto the VDU.

The standard input, output and error files are not opened within C programs, since the operating system makes these devices available for all programs.

Posted by Unknown


In case fopen() is unsuccessfull in opening a file, it send s back a null(zero) value called NULL. NULL is defined in the file stdio.h.

For example, the following statement.

if((fp = fopen ("a.dat", "r")) == NULL)

can be used to test whether the file a.dat has been opened successfully or not.

Posted by Unknown

Closing Files

Each file is closed by one fclose() statement as shown in the following code:

#include<stdio.h>
main()
{
:
:
fclose(ptr1);
fclose(ptr2);
}

The above code is used to close files.

fclose() statement is used to close files.

Posted by Unknown


In C, character input-output functions from files are simple extensions of the corresponding functions for input-ouput from/to the terminal. So, there are funcitons such as:

fgetc()
fputc()

The only additional parameter of both functions is the appropriate file pointer, so that the file to be used for input-output is known.

The following code performs the actual copying of contents of a.dat to b.dat:

#include<stdio.h>
main()
{
char c;
:
:
while((c=fgetc(ptr1))!= EOF)
fputc(c, ptr2);
}

The fgetc() function reads one character at a time from a file, assigns it to a character variable, and moves the file pointer to the next character.

fgetc() actually returns an integer type of value, which is type cast into a character type before being assigned to c.

Posted by Unknown


The C statements that would do this are:

#include<stdio.h>
main()
{
FILE *ptr1, *ptr2;
ptr1 = fopen ("a.dat", "r");
ptr2 = fopen ("b.dat", "w");
----
----
}

The function fopen() opens a file in the appropriate access mode.

When a file is opened, certain information regarding that file automatically gets stored in different variables. These variables are collectively classified as the data type FILE. fopen() returns a pointer to this FILE type data. Hence, the following declaration is required for defining the two pointers:

FILE *ptr1, *ptr2;

The definition of the data type FILE is provided in a standard header file called stdio.h, which is therefore included in this program.

The parameters r and w in the fopen() statements indicate the mode of access to these files.

C allows a number of modes in which a file can be opened.


Posted by Unknown


The treatment of files in C is very simple, unlike that of other programming languages where there are special in-built and often rigid file structures and file-handling routines. C treats file input-output in much the same way as input-output from/ to the terminal, and provides file input-output functions, very similar to those for input-output from/to the terminal.

The simplicity of the input-output in C lies in the fact that it essentially treats a file as a stream of characters and accordingly allows input-output in streams of characters. Functions are available for single character as well as multiple character input-output from/to files.

This simplicity has the advantage that the programmer can read and write onto a file as he/she wishes to.

This post uses the example of a file-copy program, which copies the contents of a file called a.dat to a file called b.dat to explain file input-output.

The steps involved in copying are:

1. Opening both files, one to read from, the other to write to.

2. Reading one character from the file a.dat, writing it onto b.dat, until the end of a.dat.

3. Closing both files.

Posted by Unknown
Powered by Blogger.