You are here:

C/Common errors while handling files in C.

Advertisement


Question
can u plz tell me wat r the diffrent types of errors occurred in file handling of c language??

Answer
Hi dear shweta!

Common errors while handling files in C.

We are porn to mistakes and not the computer; it’s just a servant to satisfy what we have ordered.

Folowing are the common mistakes / error that every one can encounter while handling files in C (almost in all classical programming languages)

Opening a file which doesn’t exist at all?

We should make sure before performing any read , write operation over a file , it should be available in the disk and more over it should opened.

foo = fopen(“sample.txt” ,”r”);

the above function call will return a reference or pointer to the file “sample.txt” if the file either if the doesn’t exits , or for some reasons it may fail to open the requested file. In such cases it would return a null pointer reference to the variable ‘foo’ , for any subsequent R/W call to the designated file using pointer ‘foo’ will result in error .

so you must ensure the file opened before enquiring the file contents as

fp = fopen(“Sample”,”r”);
if(fp == NULL)
{
  Printf(“File open failed “);
           Return; /* stop the progress of the programme */
}
The above mentioned is the best practice while dealing with file, in some occasions we where really not aware of the existence of the file but we don’t want to crash our programme while trying to work with files , in such cases following strategy will help you out

FILE * foo;
foo =  fopen(“sample.txt”,”r+”); /* returns null if “sample.txt” doesn’t exist */
if( foo == NULL)
{
     foo = fopen(“sample.txt”,”w+”); /* open a new file for reading / writing  */
     if(foo == NULL)
     {
           printf(“Error opening/ Creating file \n”);
           return;
      }
}
Note: Specifying an invalid directory will in turn fails to open a requested file
Eg: foo = fopen(“v:\\abc.txt”,”r”) ; /* assumes there is no such directory in your comp */
2. Trying to performing an un supported operation.
Assume that you have opened your file in Read mode; this could trigger error if you try to perform a write operation to the same file (which wasn’t supported in read mode)

3. Trying to read beyond the EOF.
Without knowing the actual length of the file, we should not instruct the file pointer to perform seek operation, this may result in locating the file pointer beyond the EOF.

Assume that you file is ‘n’ bytes long if you try to seek to n+1 the location will result in exception.

I hope this would help you , if you require some advanced stuffs , please feel free to get back to me

Thanks and Regards!
Prince M. Premnath  

C

All Answers


Answers by Expert:


Ask Experts

Volunteer


Prince M. Premnath

Expertise


I'm sure that I can solve any doubts in Turbo C ,Graphics Programing ,Mouse, Hardware Programming ,File System ,Interrupts, BIOS handling , TSR Programming , General Concepts in C Language, handling inline Assembly statements

Experience

Research over 6+ Years

Organizations
CG-VAK Softwares and Exports Limited

Education/Credentials
Masters in Computer Applications

©2012 About.com, a part of The New York Times Company. All rights reserved.