File Handling
#1

[attachment=9700]
File Handling in C
Goals

By the end of this unit you should understand …
… how to open a file to write to it.
… how to open a file to read from it.
… how to open a file to append data to it.
… how to read strings from a file.
… how to write strings to a file.
What is a File?
A file is a collection of related data that a computers treats as a single unit.
Computers store files to secondary storage so that the contents of files remain intact when a computer shuts down.
When a computer reads a file, it copies the file from the storage device to memory; when it writes to a file, it transfers data from memory to the storage device.
Buffers
A buffer is a “special work area” that holds data as the computer transfers them to/from memory.
Buffers help to synchronize data the physical devices with the program.
The physical requirements of the devices can deliver more data for input than a program can use at any one time. The buffer handles the overflow data until a program can use it.
Moreover, the buffer also holds data until it is efficient to write that data to the storage device for output.
File Information Table
A program requires several pieces of information about a file, including the name the OS uses for it, the position of the current character, etc.
C uses a structure called FILE (defined in stdio.h) to store the attributes of a file.
Streams
In C, we input/output data using streams. We can associate a stream with a device (i.e. the terminal) or with a file.
C supports two types of files
Text Stream Files
Binary Stream Files
Text Streams & Binary Streams
Text streams consist of sequential characters divided into lines. Each line terminates with the newline character (\n).
Binary streams consist of data values such as integers, floats or complex data types, “using their memory representation.”
Today, we’ll concentrate solely on text streams …
Files & Streams
A file is an “independent entity” with a name recorded by the operating system.
A stream is created by a program.
To work with a file, we must associate our stream name with the file name recorded by the OS.
Steps in Processing a File
1. Create the stream via a pointer variable using the FILE structure:
FILE* spData;
2. Open the file, associating the stream name with the file name.
3. Read or write the data.
4. Close the file.
System-Created Streams
C automatically creates three streams that it opens and closes automatically for us in order to communicate with the terminal:
stdin
stdout
stderr
We cannot re-declare these streams in our programs.
Standard I/O Functions in C
File Open
The file open function (fopen) serves two purposes:
It makes the connection between the physical file and the stream.
It creates “a program file structure to store the information” C needs to process the file.
Syntax:
fopen(“filename”, “mode”);
More On fopen
The file mode tells C how the program will use the file.
The filename indicates the system name and location for the file.
We assign the return value of fopen to our pointer variable:
spData = fopen(“MYFILE.DAT”, “w”);
spData = fopen(“A:\\MYFILE.DAT”, “w”);
More On fopen
File Open Modes
More on File Open Modes
Reading from a File
Example
Coping the content of one file to another
Writing to a File
Example
Reply
#2

[attachment=10682]
FILE HANDLING
• PROBLEM:-if we need same data to be processed again and again,we have to enter it again and again and if volume of data is large,it will be time consuming process.
• SOLUTION:-data can be permanently stored,a program can read it directly at a high speed.
• The permanent storage space on a disk is called FILE.
• FILE is a set of records that can be accessed through the set of library functions.
FILE TYPES
1.SEQUENTIAL FILE:-in this data is kept permanently.if we want to read the last record of file then we need to read all the records before that record.it means if we wan to read 10th record then the first 9 records should be read sequentially for reaching to the 10th record.
2.RANDOM ACCESS FILE:- in this data can be read and modified randomly.if we want to read the last record of the file,we can read it directly.it takes less time as compared as sequential file.
HOW TO DEFINE A FILE
Before using a file in a program,we must open it,which establishes a page link between the program and the operating system.A file must be defined as:-
FILE *fp;
data structure file pointer
where FILE is the data structure which is included in the header file,STDIO.H and fp is declared as a pointer which contains the address of a character which is to be read.
Operation on a file
fp = fopen (“ABC.DOC”, “r”);
Here fp =file pointer
fopen= function to open a file
ABC.DOC=name of the file
r=mode in which file is to be opened
fopen is a function which contains two arguments:-
1. File name ABC.DOC
2. Mode in which we want to open the file.
3. Both these are of string types.
fopen():-
• This function loads the file from disk to memory and stores the address of the first character to be read in the pointer variable fp.
• If file is absent,it returns a NULL pointer variable.
fclose(fp):- the file which is opened need to be closed and only can be done by library function fclose.
File opening modes
1. Read mode®:- purpose to read from the file.if file exists,it sets up a pointer which points to first char in it and if it does not exist,it returns NULL.
2. Write mode(w):-purpose to write in the file.if file exists,its contents are overwritten.if it does not exist new file is created.
3. Append mode(a):-purpose is to add data in already existing file.if it does not exist new file is created.
4)Read +(r +):-it is an extension to read mode.the operations possible on this mode are to read existing,writing new contents and modifying existings contents.it returns NULL if file does not exist.
5)Write +(w +):-it is similar to write mode, mode.the operations possible on this mode are to write new contents and modify contents already written.
6)Append +(a +):- it is similar to append mode,the operations possible on this mode are to read existing contents,add new contents at the end of file but cannot modify existing contents.
Reply

Important Note..!

If you are not satisfied with above reply ,..Please

ASK HERE

So that we will collect data for you and will made reply to the request....OR try below "QUICK REPLY" box to add a reply to this page
Popular Searches: file handling in c seminar, contents of gmpls, borwell contents, file handling in c pdf, ppt on file handling mode in c, what is file handling on computers, ppt file handling in c,

[-]
Quick Reply
Message
Type your reply to this message here.

Image Verification
Please enter the text contained within the image into the text box below it. This process is used to prevent automated spam bots.
Image Verification
(case insensitive)

Possibly Related Threads...
Thread Author Replies Views Last Post
  ZFS FILE SYSTEM project topics 1 3,121 08-12-2012, 02:07 PM
Last Post: seminar details
  Minimizing File Download Time in Stochastic peer-to-peer networks electronics seminars 7 7,285 15-03-2012, 12:32 PM
Last Post: seminar paper
  AS2 protocol for file transfers Electrical Fan 1 2,906 24-01-2012, 10:09 AM
Last Post: seminar addict
  File Transfer Protocol seminar surveyer 2 2,441 20-01-2012, 10:06 AM
Last Post: seminar addict
  ccsds file delivery protocol full report project reporter 1 1,466 07-01-2012, 03:40 PM
Last Post: project uploader
  FILE AND RECORDS MANAGEMENT SYSTEM seminar class 0 1,271 22-02-2011, 02:48 PM
Last Post: seminar class
  HANDLING THE INTER-VEHICULAR COMMUNICATIONS CHALLENGE - A SURVEY seminar class 0 1,027 18-02-2011, 10:02 AM
Last Post: seminar class
  DISTRIBUTED FILE SYSTEMS ppt seminar surveyer 0 2,728 24-01-2011, 05:24 PM
Last Post: seminar surveyer
  FPACK-A FILE UTILITY seminar surveyer 0 888 24-12-2010, 04:03 PM
Last Post: seminar surveyer
  Different types of file system in Operating system computer science crazy 0 3,161 13-11-2010, 11:43 AM
Last Post: computer science crazy

Forum Jump: