Вы находитесь на странице: 1из 20

Files

Files
Computer Programming and Utilization

Prof. Munindra Lunagaria


MEFGI, India
Munindra Lunagaria

Introduction

A file is nothing but a collection of records and each line of the data is recorded in
the file.

The record is a group of related data items.

The library functions to access the files are available in stdio.h

Munindra Lunagaria

Streams and File Types

The reading and writing of data is done with streams.

A stream is a file and using physical device like keyboard information is stored in
the file.

Printer and monitor are used to display the stream.

The FILE object uses these devices.

The FILE object contains all the information about a stream like current position,
pointer to any buffer, error and end of file (EOF).

Munindra Lunagaria

File Types
Sequential File: Here, data are arranged sequentially.

To read the last record of the file it is expected to read all the
records before it.
It takes more time to access the records.
Random Access File: In this type, data can be read and
modified randomly without reading the intermediate
records.

Munindra Lunagaria

Opening a File
Opening of file creates a link between operating system and file
functions
FILE *fp;
fp=fopen(data.txt,r);
fopen() performs following tasks:
Searches the disk for opening of file
If file exists, loads the file from disk to memory
If file does not exist, returns NULL
Locates a character pointer pointing to first character of file

Munindra Lunagaria

Reading or Writing a File


The fgetc() function is used to read the contents of the file
ch=fgetc(fp);

The fputc() function is used to write the contents in the file.


ch=fputc(c,fp);
It reads and write the character from and in the current pointer
position and advances the pointer position so that the next character is
pointed.

Munindra Lunagaria

Closing a File
Closing the file enables to wash out all its contents from
RAM buffer, and further link is disconnected from the file.
To close a single file:
fclose(fp);

To close all opened files


fcloseall();
It returns the number of files that have been closed.

Munindra Lunagaria

Write a Program on Opening and Closing the File


# include <stdio.h>
# include <conio.h>
# include <process.h>
void main()
{
FILE *fp;
char c= ;
clrscr();
fp=fopen(data.txt,w);
if(fp==NULL)
{
printf(Can not open file);
exit(1);
}
printf(Write data & to stop
press .:);

Munindra Lunagaria

while(c!=.)
{
c=getche();
fputc(c,fp);
}
fclose(fp);
printf(\n Contents read :);
fp=fopen(data.txt,r);
while(!feof(fp))
printf(%c,getc(fp));
}

OUTPUT :
Write data & to stop press .:
ABCDEFGHIJK.
Contents read: ABCDEFGHIJK.

Text Modes
w (write)
Opens a new file for writing
r (read)
If file is found, loaded to memory; if not, returns NULL
a (append)
Opens a pre-existing file for appending data
w+ (write + read)
If file is found, its contents are destroyed; if not, new file is created
a+ (append + read)
Contents of file can be read and records can be added at the end of
file

r+ (read + write)
Read and write the record in file
Munindra Lunagaria

Write a Program to Open a Pre-existing File and Add


Information at the End of File. Display the Contents of
the File Before and After Appending.

# include <stdio.h>
# include <conio.h>
# include <process.h>
void main()
{
FILE *fp;
char c;
clrscr();
printf(Contents of fi le before appending :\n);
fp=fopen(data.txt,r);
while(!feof(fp))
{
c=fgetc(fp);
printf(%c,c);
}
fp=fopen(data.txt,a);

Munindra Lunagaria

Continue
if(fp==NULL)
{
printf(File can not appended);
exit(1);
}
printf(\n Enter string to append :);
while(c!=.)
{
c=getche();
fputc(c,fp);
}
fclose(fp);
printf(\n Contents of file After
appending :\n);
fp=fopen(data.txt,r);
while(!feof(fp))
{
c=fgetc(fp);
printf(%c,c);
}
}

Munindra Lunagaria

OUTPUT :
Contents of file before appending
:
String is terminated with \0.
Enter string to append :
This character is called as NULL
character.
Contents of file After appending
:
String is terminated with \0.
This character is called as NULL
character

Binary Modes
wb (write)
Opens a binary file in write mode

rb (read)
Opens a binary file in read mode

ab (append)
Opens a binary file in append mode

r+b (read + write)


Opens a pre-existing file in read and write mode

w+b (read + write)


Creates a new file in read and write mode

a+b (append + write)


Opens a file in append mode. If file does not exist, new file is created
Munindra Lunagaria

Program to Use w+ Mode for Writing and Reading of a


File
# include <stdio.h>
# include <conio.h>
# include <process.h>
void main()
{
FILE *fp;
char c= ;
clrscr();
fp=fopen(data.txt,w+);
if(fp==NULL)
{
printf(Can not open file);
exit(1);
}
}

Munindra Lunagaria

Continue
printf(Write data & to stop press . :);
while(c!= .)
{
c=getche();
fputc(c,fp);
}
rewind(fp);
printf(\n Contents read :);
while(!feof(fp))
printf(%c,getc(fp));
}
OUTPUT :
Write data & to stop press .: ABCDEFGHIJK.
Contents read : ABCDEFGHIJK.
Munindra Lunagaria

File I/O
fprintf(): writes contents to the file

fscanf(): reads contents from the file pointed by file pointer


getc(): reads a single character from opened file and moves the file pointer
putc(): writes a single character into a file
fgetc(): reads a character and increments pointer
fputc(): writes the character to file
fgets(): reads string from a file
fputs(): writes string to a file
putw(): writes an integer value to the file

getw(): returns the integer value from a file

Munindra Lunagaria

A Simple Program to Demonstrate the Use of fscanf() and fprintf().


# include <stdio.h>
# include <conio.h>
void main()
{
FILE *fp;
char text[15];
int age;
fp=fopen(Text.txt,w+);
clrscr();
printf(NametAGE\n);
printf(Name\t AGE\n );
scanf(%s %d,text,&age);
fprintf(fp,%s %d, text,age);
printf(Name\t AGE\n );
fscanf(fp,%s %d,text,&age);
printf(%s\t%d\n, text,age);
fclose(fp);
}

Munindra Lunagaria

OUTPUT:
Name
AMIT
Name
AMIT

AGE
12
AGE
12

Other File Operation


fseek(): This function is used to seek the file pointer at specified
position.
fseek(filepointer,offset,position);
Position can be
SEEK_SET
SEEK_CUR
SEEK_END
ftell(): This function is used to tell the current position of file
pointer.
P=ftell(filepointer);
Munindra Lunagaria

Write a Program to Read the Last Few Characters


of the Fiile Using the fseek() Statement
# include <dos.h>
# include <stdio.h>
# include <conio.h>
void main()
{
FILE *fp;
int n,ch;
clrscr();
fp=fopen(text.txt,r);
printf(\nContents of fi le\n);
while((ch=fgetc(fp))!=EOF)
printf(%c,ch);
printf(\n Howr many characters including spaces would you like
to skip ? :);
scanf(%d,&n);

Munindra Lunagaria

Continue
fseek(fp,-n-2,SEEK_END);
printf(\nLast %d characters of a fi le\n,-n-2);
while((ch=fgetc(fp))!=EOF)
printf(%c,ch);
fclose(fp);
}
OUTPUT:
How many characters including spaces would you like to
skip? : 4
Last 5 characters of a file
WORLD

Munindra Lunagaria

Munindra Lunagaria

Вам также может понравиться