Advertisement Remove all ads

What is File? What Are Different Functions Available to Read Data from File? Specify the Different Modes in Which Files Can Be Opened Along with Syntax. - Structured Programming Approach

What is file? What are different functions available to read data from file? Specify the different modes in which files can be opened along with syntax.

Advertisement Remove all ads

Solution

• For file handling or accessing the contents of file, there are certain predefined functions available in the C programming language.
• An important thing required to access files is the "FILE pointer". This pointer is used to point to the values stored in the file. A file pointer is hence to be created for accessing the files. The syntax for creating a file pointer is as given below: FILE *<identifier for pointer>; For e.g. FILE *fp;
• Hence in every program we write in this section to access files, we will use this kind of pointer declaration. This pointer is used to point the data to be accessed in the file i.e. whenever a data is read or written in the file, it is from the location pointed by the file pointer "fp".
• File operations are as follows:
1. fopen(): This function is used to open a file to be accessed in the program.
• The file to be opened is to be passes as a string parameter to the function and also the mode of opening the file is to be passed as the string to the function.
• Hence the syntax of the function with parameters is as given below : <file pointer identifier> = fopen("<file name>", " <mode of opening the file>")
• For e.g. fp=fopen("test.txt","w"); This example statement opens the file "test.txt" in write mode and the pointer used along with the function to read/write is the file pointer "fp".
• The various modes in which a file can be opened are as listed below :
I. "r" indicates that the file is to be opened indicates in the read mode.
II. "w" indicates that the file is to be opened indicates in the write mode. When a file is opened in write mode the data written in the file overwrites the previously stored data in the file.
III. "a" indicates that the file is to be opened in the append mode. In this mode the data written into the file is appended towards the end of the already stored data in the file. The earlier stored data remains as it is.
IV. "w+" indicates that the file is to be opened in write and read mode (v) "r+" indicates that the file is to be opened in read and write mode.
• . fclose(): This function is used to close the file opened using the file pointer passed to the function.

• The syntax with parameters to call this function is as given below : fclose(<file pointer identifier>);
• For e.g. fclose(fp);
3. fputc(): This function is used to put a character type data into the opened file using the fopen() function, pointed by a file pointer.
• The syntax to call this function along with the parameters to be passed is as shown below : fputc(<char type data>, <file pointer identifier>);
• For e.g. : fputc(c,fp); This example will store the character value of the char type data variable "c" into the opened file and pointed by the file pointer fp, at the position pointed by the pointer fp in the file.
4. getc(): This function is used to get a character from the file pointed by the corresponding file pointer passed to the function.
• It is exactly opposite the fputc() function. This function brings the character from the file opened and pointed by the file pointer variable passed to the function.
• The syntax of the function call with the parameters to be passed is as given below :getc(<file pointer identifier>);
• For e.g. getc(fp);

Concept: Character Set in C-Programming
  Is there an error in this question or solution?
Advertisement Remove all ads
Advertisement Remove all ads
Advertisement Remove all ads
Share
Notifications

View all notifications


      Forgot password?
View in app×