Welcome -
Guest!
Login
/
Register Now
|
Search:
Home
Articles
Forum
Interview FAQ
Activities
News
Videos
Poll
Links
People
Groups
C Programming Articles
Submit Article
Home
»
Articles
»
C Programming
»
Beginners
RSS Feeds
File Management
Posted By:
Niamh Hughes
Category:
C Programming
Views:
8742
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:
Previous Post:
Program to print addition of two matrices using pointers
Next Post:
Handling of Character Strings
Didn't find what you were looking for? Find more on
File Management
Or get
search suggestion and latest updates
.
Niamh Hughes
author of File Management is from London,
United Kingdom
.
View All Articles
Related Articles and Code:
STUDENT DETAILS MANAGEMENT USING INDEXED FILE
Student profile management
Inventory Management System
C++ Hotel Management Project
Project of Student information management
School management system
Book Store Management
Mini Project School Management Application
Library Management System
Library Inventory Management Project
Inventory Management System
School management system
Program of inventory management and demonstrating interactive input and output
Employee database management system
Script to make File and Directory Management Operations menu based : Display Current directory, List Directory, Make Directory, Change Directory etc
Write a menu driven shell script for Copy a file, Remove a file, Move a file
Shell script to perform string replacement in a file and view the content of file
Shell Script combine file in one file horizontally and vertically
PROGRAM TO READ THE STUDENT DETAILS FROM THE STUDENT INFORMATION FILE USING FILE READ OPERATION
PROGRAM FOR READ DATA FROM INPUT-FILE AND MOVE INTO OUTPUT-FILE
Other Interesting Articles in C Programming:
Program to add the corresponding index value of two arrays and result is stored in the 3rd arra
Program to create user defined fork function and create 2 child process, print their process ids
NEWTON'S DIVIDED DIFFERENCE INTERPOLATION
Program to reconstruct a binary search tree
Program of histogram using self-scheduling and locking the whole bin
PROGRAM TO REVERSE THE GIVEN STRING PARALLELY
FUNCTION TABULATED AT EQUAL INTERVAL USING FORWARD DIFFERENCE BETWEEN TWO VALUE
Program of an EVALUATION OF AN EXPRESSIONS
PROGRAM TO FIND ANSWER OF THE FOLLOWING SERIES. 1 + 2 + 6 + 24 + 120 + ......
Program to interchange the Small and Capital Letters and Unload the TSR on occurance of '@' character
Illustration of static variable
Program to implement a circular queue as a linked list
Program to implement an array
Program to convert expression in postfix form to prefix form
Program to simulate a dictionary using linked list
Detection of errors in scanf input
Program of histogram using loop splitting and locking the each bin with a different lock
Program that prints triangle in reverse pattern
PROGRAM FOR SECANT METHOD OF PARTICULAR EQUATION IS log(x)-cos(x)
PROGRAM TO PRINT ALL THE PERFECT NUMBERS BETWEEN 1 AND 2000
Please enter your Comment
*
*
Comment should be atleast 30 Characters.
Please put code inside [Code] your code [/Code].
Please login to post comment
No Comment Found, Be the First to post comment!
C Programming
View All
Homework Help
Data File Structure
Projects
Beginners
BIOS Programming
TSR Programming
Interrupt Programming
Mouse Programming
Keyboard Programming
Parallel Port Programm...
Parallel Processing Pr...
Computer Graphics
Mathematics Program
Numerical Methods
Interview FAQ
Assembly Language
Artificial Intelligence
C++ Programming
Visual C++
OOAD
Cobol
Java
SQL Server
Asp.net MVC
Rest and WCF Services
Entity Framework
Knockout.Js
Unix / Linux / Ubuntu
Networking
OOPs Concept
HTML
Dos
SQL
System Analysis & Design
Gadgets
Internet
CSS
Javascript
.Net Framework
Asp.net
C#
VB.Net
Python
Perl
Oracle
Software Engineering
RDBMS Terms
AJAX Framework
Design Pattern
UML
WPF
WCF
SEO
PowerShell
Visual Studio
WWF
BizTalk Server
Azure
General
Testing
Online Certifications
PHP
My SQL
LinQ
Project Management
Silverlight
XML
MS Office
Windows OS
DHTML
Sharepoint