20. C-Header Files

C-PreProcessor: Previous                                                              Next: C R/W one Line
Header files are predefined files which consist declaration and prototypes of all predefined functions.
Eg: header file stdio.h contains all library functions related to input/output operations.
Eg: it contains definition and prototype for printf( ) and scanf( ) etc.
Some common header files and the functions are:
Header files                                             Some Function
ctype.h                                                     isalpha( ), isdigit( ), islower( ), isupper( ), isspace etc.
string.h                                                    strcat( ), strcpy( ), strcmp( ) etc.
math.h                                                     div( ), pow( ), tan( ), sqrt( ) etc.
Macros v/s functions: -
  • Functions reduces the size of code i.e. File size but decreases the speed of a program. In macros, file size increases.
  • Functions are called by the compiler at the time of execution.in.macros,the actual code of macros replaces at the pace of calling.
  • When the functions are called, the compiler jumps to the function's definition. In macros compilers are need not to get jumped at definition section.
  • The definition of functions are replaced by compiler after execution of main().the definition of macro are replaced by compiler before execution of main().
File input/output: -
Basic concept of file: File is a collection of related records and records are collection of related fields, fields can be empno, empname, salary etc. And in case of student fields can be rollno, name, marks etc.
Generally files are used to store information in a standard format for easy and fast retrieval of data. Information can be read or write on hard disk as well as floppy disk.

Characteristics of files: -
  • They occupy space in memory.
  • The name of file must be unique because files are always identified by their names.
  • The extension of files describes the type of file for Eg ‘.docx’ extension is for word`s file or like in C, the extension of source code is ‘.c’
  • Files can be stored temporarily as well as permanently in a compiler system.
Some operations that are associated with files are: -
  1. Opening a file
  2. Reading a file
  3. Writing a file
  4. Closing a file
Opening a file: - before, the actual operations is to be performed a file must be opened. Opening process creates an interaction and understanding between o/s of using system and C program.
fopen( ) is used to open a file. It takes two arguments:
  • Valid file name
  • Access mode
Access mode: it indicates the mode in which file is need to open.
Access modes are following:
R:             To open a file for reading purpose only.
W:            To open a file for writing.
A:            To open a file in append mode i.e. Write only at the end of the file.
W+:          To open a file for reading and writing.
A+:          To open a file for reading and appending.
R+:           To open a file for reading and writing.
Syntax:    fopen(filename, access mode)
fopen( ) perform three task related with an opening file:
  • Searches a file with the file name given
  • If found a file then loads it from disk.
  • Sets pointers according to an access mode.
Access node is not a character, it is a string that`s why written in double quotes (" ").
To open a file, a data type named 'FILE' is used it declares a pointer variable for a file name.
The process is as follows:
FILE * new_file;
new_file = fopen(“First.dat”, “w”);
fopen( ) returns the address of a opened file if file is found. The function fopen( ) returns pointer to structure of type FILE. When a file is not searched or fopen( ) is unable to open a file by any reason then it returns NULL.
The main difference between printf( ) and fprintf( ) is, to write formatted date on specified file, fprintf( ) is used. While printf( ) function is used to write something on output screen. fprintf( ) function takes two arguments: First, the pointer variable of the file name in which string has to be written. Second, the string is double quotes.
Syntax:    fprintf(file pointer, “string”);
Eg:           #include
                void main( )
                                FILE *fpt;
                                ftp = fopen(“first.dat”, “w”);
                                fprintf(ftp, “File is Opened”);

Whenever a file is opened, after reading or writing from a file, the file is need to close.
fclose() function is used to close an opened file. Closing a file frees memory occupied by file structure pointer. This takes one argument i.e. File pointer.
Syntax: fclose(filepointer);
The return data type of this function is integer i.e. It returns 0 if file is successfully closed otherwise returns 1.
Reading and writing data to a file: -
For performing reading and writing operations on a file, the file is need to be opened on disk.
Reading and writing can be performed in following ways:
  1. To read and write one character at a time from a file.
  2. To read and write one line at a time from a file.
  3. To read and write one block of a data at a time from file.
  1. Reading/writing one character at a time: - we have a function to open and close a file in same way for reading/writing one character at a time four functions are used:
  2. getc()
  3. putc()
iii.            fgetc()
  1. fputc()
  1. getc(): - this function is used to read a single character from a specific file and returns EOF. This function reads a single character at a time and character is need to be stored in an int type variable.
Syntax: variable = getc(file pointer)
It takes single argument that is file pointer variable.
  1. fgetc(): - this function is used to read a single character from a specified file, it works in same way as getc() works. But difference is that the getc() is a macro while fgetc() is a function. fgetc() is a library function which is defined in stdio.h header file.
Eg:           write a program to show use of fgetc( ) and getc( )
                                void main( )
                                                FILE *fileptr;
                                                FILE *sfileptr;
                                                char fname[30];
                                                int charcter;
                                                printf(“Enter File Name : ”);
                                                scanf(“%s”, fname);
                                                if((fileptr = fopen(fname, “r”)) = = NULL)
                                                                printf(“File is not Found”);
                                                                exit( );
                                                                printf(“The content of file is : ”);
                                                                while((character = getc(fileptr)) != EOF)
                                                if((sfileptr = fopen(first.dat, “r”)) = = NULL)
                                                                printf(“Enable to open a second file”);
                                                                exit( );
                                                                while((character = fgetc(sfileptr)) != EOF)
program including fgetc and getc

  1. putc(): - this function is used to write a single character into a specified file. This function takes two arguments: First, character variable that contains character is to be written. Second, file pointer. It returns character and if an error occurs then it returns EOF.
Syntax: putc(character, file pointer)
This function is need to write in conditional loop because file always contains a set of characters.
  1. fputc(): - this function is also used to write a single character into a specified file. This function takes two arguments:
First, character variable that contains character is to be written. Second, file pointer. It returns character and if an error occurs then it returns EOF.
Syntax : fputc(character, file pointer)
The only difference between putc() and fputc() is that putc() is a macro, while fputc() is an function defined in stdio.h library.
Eg:           Use of putc( ) function.
                void main( )
                                FILE *file;
                                char character[200];
                                char file_name[30];
                                char terminate;
                                int i, character2, maximum;
                                printf(“Enter File Name : ”);
                                scanf(“%s”, file_name);
                                file = fopen(file_name, “w”);
                                if(file = = NULL)
                                                exit( );
                                                printf(“Enter a termination Character : ”);
                                                scanf(“%c”, terminate);
                                                printf(“Now a lines for Writing and Terminated with %c”, terminate);
                                                i = 0;
                                                while(character2 = getchar())
                                                                character[i++] = character2;
                                character[i++] = ‘\0’;
                                maximum = i;
                                for(i = 0; i
                                                putc(character[i], file);

Next is to learn Reading / Writing one line in C Programming.

C-PreProcessor: Previous                                                              Next: C R/W one Line