Logo 
Search:

C Programming Articles

Submit Article
Home » Articles » C Programming » BeginnersRSS Feeds

File Management

Posted By: Niamh Hughes     Category: C Programming     Views: 20329

This article explains about stream, files, buffering and different file operations.

Streams and Files

  • Stream is a consistent interface to devices of varied type like disk drives, terminals, tape drives, screens, keyboards
  • A layer of abstraction is provided 
  • The abstraction, is a stream and actual device is called file
  • A stream behave similarly and largely device independent


Text Streams

  • A text stream is a sequence of characters
  • Standard C allows (but not require) a text stream to be organized into lines terminated by newline character
  • Certain character translation may occur, like converting a newline char to cr/lf sequence
  • There is possible difference between no of chars written and present actually on device

Binary Streams

  • It is a sequence of bytes that have one to one correspondence with external device
  • No character translations may occur, though some padding or null character may be done
  • Number of bytes written always matches no of bytes read
  • It is storing integers, floats etc in their binary format

Buffering 

  • Matching speeds of all the devices is always a problem when attached to stream
  • Ex. Typed chars to be transferred to the disk file not after each keystroke
  • An area of memory called buffer is used 
  • Optimum size of buffer depends on the relative speed of two different devices

Files 

  • A file can be a disk file, a terminal, keyboard, screen, printer, anything like that!
  • File is associated with a stream using an open operation
  • Once the file is open, information exchange is possible between your prog and file
  • A disk file can support random access while a printer generally not
  • All files are not same, though  all streams are same
  • If the file can support position request, it also initializes position indicator to the start of file when opened
  • As each character is read or written to the file, PI gets incremented
  • close disassociates file from stream

Close Operation using exit() function

  • When a file closed which is opened for output, the contents if any, are written to the external device (flushing!)
  • Buffers and stream
  • All files are not closed at the time of abnormal termination or by abort()
  • All files are closed when returning from main() or using exit()

FILE 

  • FILE is a structure which contains the file control information associated with each file
  • FILE defines nine fields that represent the current status of the stream
  • Contains buffer empty/full info, file status flags, file desc, buffer size, current active pointer, stamp for validity checking etc

File Pointer

  • The file pointer is a pointer to struct FILE
  • It that’s why points to information about the file like it’s name, status & current position
  • It, identifies a specific file and is used by associated stream to direct the operation of the IO functions
  • To obtain a file pointer variable use
FILE *fp;


Opening a File


File Modes

Mode Meaning
r Open existing text file for read only
w Create a text file for write only
a Open or create a text file for append
(r/w/a)b All operations with binary file
(r/w/a) (b/t) + All operations with either binary or text file with read and write


Closing a File using fclose() function

  • int fclose( FILE *fp);
  • It closes the stream that was opened using the fopen statement
  • It Writes any data still remains in the disk buffer
  • It also frees the FCB associated with the stream and make it available for reuse
  • Returning zero when successful!
  • The function returns EOF when error occurs
  • When disk is full or removed it can make fclose fail
  • FOPEN_MAX limit is reached, then one may need to close a file to open another
  • Failure to close may lead to lost data, destroyed files, or some intermittent problems
  
Share: 


Didn't find what you were looking for? Find more on File Management Or get search suggestion and latest updates.

Niamh Hughes
Niamh Hughes author of File Management is from London, United Kingdom.
 
View All Articles

 
Please enter your Comment

  • Comment should be atleast 30 Characters.
  • Please put code inside [Code] your code [/Code].

 
No Comment Found, Be the First to post comment!