File Management in C Language
Please keep this BLOG RUNNING. Wait for the ads to load, then Click Skip add at upper right.
Title: FILE MANAGEMENT IN C LANGUAGE
PART
A - File management in C
v Introduction
·
C supports a
number of functions that have the ability to perform basic file operations,
which include:
1.
Naming a file
2.
Opening a file
3.
Reading from a
file
4.
Writing data into
a file
5.
Closing a file
v File
operation functions in C:
Function Name
|
Operation
|
fopen()
|
Creates a new file for use
Opens a new 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 a integer from a file
|
putw()
|
Writes an integer to the 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 begining of the file
|
v Defining and
opening a file:
·
The general
format of the function used for opening a file is
SYNTAX:
FILE *fp;
fp=fopen(“filename.ext”,”mode”);
o
The first statement declares the variable
fp as a pointer to the data type FILE. File is a structure that is defined in
the I/O Library.
o
The second statement opens the file named filename.ext and assigns an
identifier to the FILE type pointer fp. This pointer, which contains all the
information about the file, is subsequently used as a communication link
between the system and the program.
The
second statement also specifies the purpose of opening the file.
o The mode
does this job. The modes are:
mode
|
Meaning
|
r
|
Opens the file
for reading. If the file doesn't exist, fopen() returns NULL.
|
w
|
Opens the file
for writing. If a file of the specified name doesn't exist, it is created. If
a file of the specified name does exist, it is deleted without warning, and a
new, empty file is created.
|
a
|
Opens the file
for appending. If a file of the specified name doesn't exist, it is created.
If the file does exist, new data is appended to the end of the file.
|
r+
|
Opens the file
for reading and writing. If a file of the specified name doesn't exist, it is
created. If the file does exist, new data is added to the beginning of the
file, overwriting existing data.
|
w+
|
Opens the file
for reading and writing. If a file of the specified name doesn't exist, it is
created. If the file does exist, it is overwritten.
|
a+
|
Opens a file
for reading and appending. If a file of the specified name doesn't exist, it
is created. If the file does exist, new data is appended to the end of the
file.
|
EXAMPLE:
FILE *p1, *p2;
p1=fopen(“data.txt”,”r”);
p2=fopen(“results.txt”,”w”);
ANALYSIS:
o
In these
statements the p1 and p2 are created and assigned to open the files data and
results respectively the file data is opened for reading and result is opened
for writing.
o
In case the
results.txt file already exists, its contents are deleted and the files are
opened as a new file. If data.txt file does not exist error will occur.
v Closing a
File
·
The input output
library supports the function to close a file; it is in the following format. SYNTAX:
fclose(file_pointer);
·
A file must be
closed as soon as all operations on it have been completed.
·
This would close
the file associated with the file pointer.
EXAMPLE:
…
FILE
*p1 *p2;
p1=fopen (“Input”,”w”);
p2=fopen
(“Output”,”r”);
…
…
fclose(p1);
fclose(p2)
ANALYSIS:
o
The above program
opens two files and closes them after all operations on them are completed,
once a file is closed its file pointer can be reversed on other file.
v getc and
putc functions (Reading and Writing)
·
The getc and putc functions are analogous to getchar and putchar
functions and handle one character at a time.
·
The putc function writes the character
contained in character variable c to the file associated with the pointer.
·
Similarly getc function is used to read a
character from a file that has been open in read mode.
SYNTAX:
putc(c,fp1);
c=getc(fp2).
EXAMPLE:
#include< stdio.h >
main()
{
file *f1;
printf(“Data input output”);
f1=fopen(“Input”,”w”); /*Open the file
Input*/
while((c=getchar())!=EOF) /*get a
character from key board*/
putc(c,f1);
/*write a character to input*/
fclose(f1); /*close the file input*/
printf(“nData outputn”);
f1=fopen(“INPUT”,”r”);
/*Reopen the
file input*/
while((c=getc(f1))!=EOF)
printf(“%c”,c);
fclose(f1);
}
ANALYSIS:
o
The program shows
the use of file operations.
o
The data entered
through the keyboard and the program writes it. Character by character, to the
file input.
o
The end of the
data is indicated by entering an EOF
character, which is control-z. the file input is closed at this signal.
v The getw and
putw functions (Reading and Writing)
·
These are
integer-oriented functions. They are similar to getc and putc functions and are
used to read and write integer values.
·
These functions
would be usefull when we deal with only integer data. The general forms of getw and putw are:
SYNTAX:
putw(integer,fp);
getw(fp);
PART
B - File Oriented Program
v A demonstration
of using file management function
- The program will accept 5 integer numbers from the user and will write
it to the DATA file.
-
If the entered number is even, it will be written into the EVEN file.
-
if the entered number is odd, it will be written into the ODD file.
#include<
stdio.h >
main()
{
FILE *f1,*f2,*f3;
int number, I;
printf(“Write Contents of the
DATA file: \n”);
f1=fopen(“DATA”,”W”);
for(I=1;I<
5;I++){
scanf(“%d”,&number);
if(number==-1)
break;
putw(number,f1);
}
fclose(f1);
f1=fopen(“DATA.txt”,”r”);
f2=fopen(“ODD.txt”,”w”);
f3=fopen(“EVEN.txt”,”w”);
while((number=getw(f1))!=EOF)
{ /* Read from data file*/
if(number%2==0)
putw(number,f3); /*Write to even file*/
else
putw(number,f2); /*write to odd file*/
}
fclose(f1);
fclose(f2);
fclose(f3);
f2=fopen(“ODD”,”r”);
f3=fopen(“EVEN”,”r”);
printf(“\nContents
of the ODD file: \n”);
while(number=getw(f2))!=EOF)
printf(“%d%d”,number);
printf(“nnContents
of the even file”);
while(number=getw(f3))!=EOF)
printf(“%d”,number);
fclose(f2);
fclose(f3);
}
SAMPLE OUTPUT:
Write Contents of the DATA file:
5
2
1
3
8
Contents of the ODD file:
5
1
3
Contents of the EVEN file:
2
8
Workshop
This Workshop provides quiz questions
to help you solidify your understanding of the material covered, and exercises
to provide you with experience in using what you've learned.
1) Can
I use drives and paths with filenames when using remove(), rename(), fopen(), and the other file
functions?
_________________________________________________________________________
_________________________________________________________________________
_________________________________________________________________________
2) Can
I read beyond the end of a file?
_________________________________________________________________________
_________________________________________________________________________
_________________________________________________________________________
3) What
happens if I don't close a file?
_________________________________________________________________________
_________________________________________________________________________
_________________________________________________________________________
PART C - PROGRAM DEVELOPMENT - File management
function oriented program
Conversion PROGRAM (10pts)
1.
The program will accept five (5) integer numbers from 1 to 5 (Set01)
and 6-10 (Set02) from the user and will write the entered numbers into
S1INPUT.txt (Set01) and S2INPUT.txt (Set02).
2.
The following will be conversion:
1 = A 6 = F
2 = B 7 = G
3 = C 8 = H
4 = D 9 = I
5 = E 10 = J
The
program will then write the converted values into S1OUTPUT.txt (Set01) and
S2OUTPUT.txt Set02).
3. NOTE: The program must check the user input, if out of ranged; it must
not accept the value and should prompt the user.
Sample OUTPUT for S1:
Enter
Five integer numbers from 1-5:
5
2
1
1
3
Contents of S1Input.txt Contents of S2Output.txt
5 E
2 B
1 A
1 A
3 C
Please keep this BLOG RUNNING. Wait for the ads to load, then Click Skip add at upper right.
Title: FILE MANAGEMENT IN C LANGUAGE
No comments:
Post a Comment