Tuesday, November 13, 2018

C Language : File Handling for B.C.A.,P.G.D.C.A. and all IT Students


--------------------------------------------------------------------------------------------------------------------------
Prepared By : Uday Shah (HOD -IT)
Contact No  : 7600044051
E-Mail : ruparleducation@gmail.com



Concept of Data File

Many applications require that information be written to or read from an auxillary storage device. Such information is stored on the storage device in the form of data file. Thus, data files allow us to store information permanently and to access later on and alter that information whenever necessary. In C, a large number of library functions is available for creating and processing data files. There are two different types of data files called stream-oriented (or standard) data files and system oriented data files.




I/O Operation  or OPENING AND CLOSING A DATA FILE

  • We must open the file before we can write information to a file on a disk or read it.
  • Opening a file establishes a link between the program and the operating system.
  • The link between our program and the operating system is a structure called FILE which has been defined in the header file “stdio.h”.
  • Therefore, it is always necessary to include this file when we do high level disk I/O.
  • When we use a command to open a file, it will return a pointer to the structure FILE.
  • Therefore, the following declaration will be there before opening the file, FILE *fp each file will have its own FILE structure.
  • The FILE structure contains information about the file being used, such as its current size, its location in memory etc.
  • Let us consider the following statements,
  • FILE *fp; fp=fopen(“Sample.C,” “r”);
  • fp is a pointer variables, which contains the address of the structure FILE which has been defined in the header file “stdio.h”.
  • fopen() will oepn a file “sample.c” in ‘read’ mode, which tells the C compiler that we would be reading the contents of the file.
  • Here, “r” is a string and not a character. When fopen() is used to open a file then, it searches on the disk the file to be opened.
  • If the file is present, it loads the file from the disk into memory. But if the file is absent, fopen() returns a NULL.
  • To read the file’s contents from memory there exists a function called fgetc(). This is used as: s=fgetc(fp);
  • This fgetc() is used within an indefinite while loop, for end of file. End of file is signified by a special character, whose ascii value is 26.
  • While reading from the file, when fgetc() encounters this Ascii special character, instead of returning the characters that it has read, it returns the macro EOF.
  • The EOF macro has been defined in the file “stdio.h”.
  • When we finished reading from the file, there is need to close it.
  • This is done using the function fclose() through the following statement: fclose(fp);
  • This command deactivates the file and hence it can no longer be accessed using getc().
  • ‘C’ provides many different file opening modes which are as follows:
  • 1. “r” Open the file for reading only.
  • 2. “w” Open the file for writing only.
  • 3. “a” Open the file for appending (or adding) data to it.
  • 4. “r+” The existing file is opened to the beginning for both reading and writing.
  • 5. “w+” Same as "w" except both for reading and writing.
  • 6. “a+” Same as "a" except both for reading and writing.

Command Line Arguments


It is possible to pass some values from the command line to your C programs when they are executed. These values are called command line arguments and many times they are important for your program especially when you want to control your program from outside instead of hard coding those values inside the code.
The command line arguments are handled using main() function arguments where argc refers to the number of arguments passed, and argv[] is a pointer array which points to each argument passed to the program.
Syntax:

int main(int argc, char *argv[])

Here  argc  counts  the number of arguments on the command line and argv[ ] is a pointer array which holds pointers of type char which points to the arguments passed to the program.
#include <stdio.h>
#include <conio.h>
 
void  main(int argc, char *argv[])
{
          int i;
          if( argc >= 2 )
          {
                    printf("The arguments supplied are:\n");
                    for(i = 1; i < argc; i++)
                    {
                                         printf("%s\t", argv[i]);
                    }
          }
          else
          {
                    printf("argument list is empty.\n");
          }
          getch ();
}

According to above example main function contains two parameters that is argc and argv, argc is in integer and argv is in character pointer variable.
It display all arguments which is passed through command line if there is no any arguments then display appropriate message on screen.
  
Thank You, Best of Luck

Please give your comment and share it....