[go: up one dir, main page]

0% found this document useful (0 votes)
47 views4 pages

File Management

Files allow storing large amounts of related data. C supports basic file operations like naming, opening, reading, writing, and closing files. There are two approaches to file I/O in C - low-level using system calls and high-level using standard I/O functions. Common functions include fopen() to open a file, fclose() to close, getc()/putc() to read/write single characters, and fprintf()/fscanf() to read/write groups of mixed data types from/to files.

Uploaded by

Naseer Ahmed
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
47 views4 pages

File Management

Files allow storing large amounts of related data. C supports basic file operations like naming, opening, reading, writing, and closing files. There are two approaches to file I/O in C - low-level using system calls and high-level using standard I/O functions. Common functions include fopen() to open a file, fclose() to close, getc()/putc() to read/write single characters, and fprintf()/fscanf() to read/write groups of mixed data types from/to files.

Uploaded by

Naseer Ahmed
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 4

FILE MANAGEMENT

A file is a place on the disk where a group of related data is stored. Files can handle large volume of data which is not
possible with console oriented I/O operations. C supports a number of functions that have the ability to perform basic
file operations, which include
 Naming a file
 Opening a file
 Reading data from a file
 Writing data to a file
 Closing a file
There are two distinct ways to perform file operations in C. The first one is low level I/O uses UNIX system calls. The
second method is referred to as the high level I/O operations and uses functions in C’s standard I/O library.

Function name operation

fopen() Creates a new file for use


Opens an existing file for use
fclose() Closes a file which has been opened for use
getc() reads a character from a file
putc() writes a character to a file
fprintf() writes a set of data values to a file
fscanf() reads a set of data values from a file
getw() reads an integer from a file
putw() writes an integer to a file
fseek() sets the position to a desired point in the file
ftell() gives the current position in the file
rewind() sets the position to the beginning of the file
Defining and opening a file:- to store data in a file we must specify Filename, Data structure, and purpose to
the operating system. A file name is string of characters, Data structure of a file is defined as FILE, all files should be
declared as type FILE before they are used.
General format for declaring and opening a file
FILE *fp
fp=fopen(“filename”,”mode”);
fp is variable, which points to the datatype FILE. The second statement opens the file name filename and assigns an
identifier to the FILE type pointer fp. This pointer contains all the information about the file. The “mode” specifies
the purpose of opening a file, r mode means open the file for reading only, w open the file for writing only.
a open the file for appending data to it. Both filename and mode are specified as strings and should be enclosed in
double quotation marks.
When trying to open a file, one of the following things may happen.
1. When the mode is ‘writing’, a file with the specified name is created, if the file does not exist. The contents
are deleted, if the file already exists.
2. When the purpose is ‘appending’, the file is opened with the current contents safe. A file with the specified
name is created if the file does not exist.
3. If the purpose is ‘reading’, and if it exists, then the file is opened with the current contents safe; otherwise an
error occurs.
For eg
FILE *p1,*p2;
p1=fopen(“dataFile”,”r”);
p2=fopen(“resultFile”,”w”);
datafile is opened for reading and resultfile is opened for writing. If resultfile already exists, its contents are deleted
and the file is opened as a new file. If datafile does not exist, an error will occur.
CLOSING A FILE
A file must be closed as soon as all operations on it have been completed. To close a file fclose() function is used and it
takes filepointer as parameter.
fclose(file_pointer);
FILE *p1,*p2;
p1=fopen(“dataFile”,”r”);
p2=fopen(“resultFile”,”w”);
-------
-------
fclose(p1);
fclose(p2);
The getc and putc Functions: They are the simplest file I/O functions, which handles one character at a time. If a
file opened with mode w and file pointer fp1, then the statement
putc(c,fp1);
writes the character contained in the character variable c to the file associated with file pointer fp1. Similarly getc is
used to read a character from a file that has been opened in read mode. For eg
c=getc(fp2);
the getc will return end-of-file(EOF) marker, when th end of the file has been reached.
#include<stdio.h>
Main()
{
fILE *f1; char c;
f1=fopen(“input”,”w”);
while((c=getchar())!=EOF)
putc(c,f1);
fclose(f1);
f1=fopen(“input”,”r”);
while((c=getc(f1))!=EOF)
printf(“%c”,c);
fclose(f1);
}
The getw and putw functions: the getw and putw are integer-oriented functions. they are used to read and write
integer values. The general forms of getw and putw are
putw(integer,fp);
eg program
#include<stdio.h>
#include<conio.h>
void main()
{
FILE *file,*ef,*of;
int i,number;
clrscr();
printf("\t \n Contents of Data file \n");
file=fopen("c:/even_odd","wt");
for(i=1;i<=30;i++)
{
scanf("%d",&number);
if(number==-1)
break;
putw(number,file);
}
fclose(file);
file=fopen("c:/even_odd","r");
ef=fopen("c:/odd","w");
of=fopen("c:/even","w");
while((number=getw(file))!=EOF)
{
if(number%2==0)
putw(number,ef);
else
putw(number,of);
}
fclose(file);
fclose(ef);
fclose(of);
//file=fopen("c:/even_odd","w");
ef=fopen("c:/even","r");
of=fopen("c:/odd","r");
printf("contents of odd file\n");
while((number=getw(of))!=EOF)
{
printf("%4d",number);
}
printf("\n");
printf("Contents of even file \n");
while((number=getw(ef))!=EOF)
{
printf("%4d",number);
}
fclose(of);
fclose(ef);
getch();
}
The fprintf and fscanf functions: fprintf and fscanf functions can handle a group of mixed data
simultaneously, and they are similar to printf and scanf except that they work on files. The first argument of these
functions is a file pointer which specifies the file to be used. The general form of fprintf is
fprintf(fp,”control string”,list);
where fp is a file pointer associated with a file that has been opened for writing. The control string contains output
specifications for the items in the list. The list may include variables, constants and strings. Eg
fprintf(f1,”%s %d %f”,name,age,7.5);
the general format of fscanf is
fscanf(fp,”control string”,list);
the statement would cause the reading of the items in the list from the file specified by fp,
eg
fscanf(f2,”%s %d”,item,&quantity);
fscanf also returns the number of items that are successfully read. When the end of file is reached, it returns the value
EOF.
#include<stdio.h>
void main()
{
FILE *fp;int n;
int i;
clrscr();
//myformat='%2d\n';
fp=fopen("c:/new/num.txt","wt");
for(i=1;i<10;i++)
{
fprintf(fp,"%d",i);
//putw(i,fp);
}
fclose(fp);
fp=fopen("c:/new/num.txt","rt");
for(i=1;i<10;i++)
{
fscanf(fp,"%d",n);
printf("%d\n",n);
}
fclose(fp);
getch();
}
Error Handling during I/O operations
It is possible that an error may occur during I/O operations on a file. The error situations include
1. Trying to read beyond the end-of-file mark.
2. Device overflow
3. Trying to use a file that has not been opened
4. Trying to perform an operation on a file, when the file is opened for another type of operation
5. Opening a file with an invalid filename
6. Attempting to write to a write protected file.
The feof function can be used to test for an end of file condition.
if(feof(fp))
printf(“End of data \n”);
would display the message “End of data”. On reaching the end of file condition the ferror function reports the status
of the file indicated. The ferror() function returns nonzero integer if an error has been detected otherwise it returns
zero otherwise. The statement
if( ( ferror(fp)!=0)
printf(“An error has occurred”);
if a file cannot be opened for some reason, then the function returns a null pointer.
if ( fp==NULL)
Printf(“File could not be opened\n”);

You might also like