What Is a Potential Error That Can Occur When a File Is Opened for Reading?
File Direction & Common File Opening Errors In C-Programming
The file handling in C can be broadly divided into ii types
- Loftier level
- depression level
- High level file treatment is managed past library functions while depression level file handling is washed past system calls.
- The high level file treatment is commonly used since it is easier to manage and hides most of the details from the programmer.
How data are getting store in a file:
There are two ways of storing data in files, Binary format and Text format.
- In text format, data is stored every bit lines of characters where each line terminated with a new line("\northward"). Text files are in human readable form & they can be created & read using whatsoever text editor.
- In the binary format information are stored on the same way as it is represented in the computer memory.(Unix based systems does not make whatever stardom betwixt binary file and text file).Binary files are not in the human being readable grade ,they can exist created & read by specific programs only.
Steps For File Performance:
The steps for file operation in C Programming are:
- Open A file
- Read the file or write data in the file
- Close the file.
Opening a File:
A file must be opened before doing any I/O operations on that file.This will establish a connection betwixt the plan & the file.
- A structure named FILE which is defined in side stdio.h contains all the data about the file like name,status,buffer size,current position,EOF (Terminate Of File) status etc.All these data are hidden from the developer & operating system takes care about this.
- A file arrow is a pointer to a structure of type FILE.When e'er a file is opened a construction of type FILE associated with it and a file pointer that points to this structure identifies this file.
- Syntax: FILE *fopen(const char *filename,const char *mode);
- Information technology takes two things as argument proper name of the file to be opened & mode that decides which operations (read,write,append etc).
- on success fopen() returns a arrow of file type that points to first character of the file and on failure it returns NULL.
Dissimilar modes in which file can be opened are,
- "westward"(write mode)-If file does non present information technology creates a new file & opens it. If file is already present it will erased the previous contains & open the file.
- "r"(read style)-If file exists it volition open the file without erasing contains of the file.
- "a"(append fashion)-If file exists it will open up the file & will add together information in the file. If file does not exits then information technology volition create new file & will add data in it.
- "due west+"(Write+read)-works same way as write mode.
- "r+"(read+write)-works same way as read but we can write & modify existing data.
- "a+"(append+read)-works same as append mode as well nosotros can read information.
Errors in Opening a File:
Errors in Opening a File May Occur Due To Various Reasons some are
- if nosotros try to open a file in read or update fashion i.due east "r+" mode and the file does not exist or we don't have read permission for the file.
- If we endeavour to create a file & there is no deejay space available or we don't take write permission .
- If we try to create a file that already exists and we don't accept permission to delete that file.
- operating arrangement limits the number of files that can be opened at a time and we are trying to open more files than that number.
- If a file already bachelor & we opened it in "w" mode and then previous data volition become erased without showing any error.
- we should not use single quotes for the mode because information technology is a string not character. "west"-valid 'w'-invalid.
Functions Used To Read Data From File/Write Data In The File:
- Functions such as fputc(),putw(),fputs() etc are used to write data in a file.
- Similarly fgetc(),getw(),fgets() atc are used to read information from the file.
Closing a File:
The files that are opened using fopen() must be closed when no more operations are to be performed. fclose() function is used to close a file.
syntax:fclose(FILE *fp);
Afterwards endmost the file the connection betwixt file & plan is cleaved.
End Of File:
While a function reading file it should know where the file ends and then that it can stop reading there.When the EOF is reached the operating system sends an EOF betoken to the program .EOF is divers inside stdio.h & its value is -1.
Write a program to display the total number of alphabets & numeric characters in a file.
let a file text.txt is present in"c:\cprog\file\text.txt" path.The file contains can be seen in beneath screenshot.
In the file text.txt we accept to count the full number of alphabets are present & full number of numeric characters are present.
#include <stdio.h> #include<stdlib.h> void main() { FILE *fp; //File pointers int n1,n2; char ch; n1=n2=0; fp=fopen("c:\cprog\file\text.txt","r");//opening file in read way if(fp==Nothing) { printf("File not available"); go out(1); } while((ch=fgetc(fp))!=EOF) { if(((ch>='a')&&(ch<='z'))||((ch>='A')&&(ch<='Z'))) { n1++; } else { if((ch>='0'&&ch<='ix')) { n2++; } } } printf("No of alphabets are=%d\n",n1); printf("No of numeric charecters are=%d\north",n2); } Output:
Take Fun With C-Programming! 😉
Related Articles
Source: https://5balloons.info/file-management-common-file-opening-errors-in-c-programming/
0 Response to "What Is a Potential Error That Can Occur When a File Is Opened for Reading?"
Post a Comment