Open, read, write, close and define a file operations - C-Tutorial

Latest

Thursday, 10 November 2016

Open, read, write, close and define a file operations

FILES


            A file is a collection of information that is stored at a particular area on the disk.

            File stream is a sequence of bytes, which is used to read and write data on files.

            File name is a string of characters that make up a valid name for the operating system.  It may contain two parts as a primary name and an optional period with an extension.

            Syntax             :           PrimaryPart . SecondaryPart
Where,
            Primary part specifies name of the file and secondary part specifies extension to locate type of the file.

            Example:         output.dat         (Data File)
                                    Sum.c              (C Language File)
                                    ex1.bat        (Batch File)
                                    sample.exe         (Executable File)                     etc.,

            Basic operations performed on the files are:   

1.                  Defining (or) Naming a file
2.                  Opening a file
3.                  Reading data from a file
4.                  Writing data into a file
5.                  Closing a file.


Defining and Opening a File

            All files should be declared as type FILE, which is a defined data type by the compiler.  The general format of declaring and opening a file is:

Syntax    :        FILE *FilePointer;                                                     /* Declaration */

                        FilePointer = fopen(“FileName”, “Mode”);              /* Opening */

Where,
Ø  FilePointer defines as a “pointer to the data type FILE”.
Ø  fopen() function receives two arguments – FileName and Mode.

o   FileName is name of the file to be opened.  FileName in C language can also contain path information.  The path specifies the drive and/or director where the file is located.
o   Mode specifies the type of job to be performed on the file.  Mode can be any one of the following.

MODE

MEANING
r
Open the file for reading only
w
Open the file for writing only
a
Open the file for appending data

§  When the file is opened in read mode, if it exists, the file is opened with current contents safe; otherwise, an error occurs.
§  When the file is opened in write mode, if it exists, the file is opened with current contents are deleted; otherwise, a file with specified name is created by the compiler.
§  When the file is opened in append mode, if it exists, the file is opened with current contents safe and the file pointer placed at the end of the file; otherwise, a file with specified name is created by the compiler.


Reading data from a file

            Simplest input statement used for reading data from a file is getc() function.

            getc() library function is used to read a character from the file that has been opened in read mode.  The general format of getc() function is:

Syntax             :           ch = getc(FilePointer);

Where,
            ch is character type variable.
Here,
            File Pointer is opened in read mode, and the function read the data character by character from the file and assigned to the variable ch.  Whenever the end of the file has been reached, getc() function will return and end-of-file marker EOF.

Writing data into a file

Simplest output statement used for writing data into the file is putc() function.

putc() library function is used to write a character into the file that has been opened in write mode.  The general format of putc() function is:

Syntax:            putc(ch, FilePointer);

Where,
            ch is character type variable.
Here,
            FilePointer is opened in write mode and the function writes the data of ch into the file.

Closing a File

            When all operations are completed over files, it is better to close the files explicitly by calling fclose() function; when we want to open the same file in a different mode.  For this, use the syntax as:


            Syntax:            fclose(FilePointer);

No comments:

Post a Comment