UNIT -5
UNIT -5
FILES
• A file can be used to store a large volume of permanent/persistent data.
• File ends with end of file(EOF) marker.
• When a file is opened,It provide communication channel between file
and programs.
• Opening a file returns a pointer to a FILE structure.
Example:
stdin---standard input(keyboard)
stdout---standard output(screen)
stderr—standard error(screen)
File
• File management functions:
fopen-attributes(“a” or “a+” or “w” or”w+”)
---opening and creation of a new file.
Read(fscanf or fgets)
Write(fprintf or fputs)
Moving to a specific location in a file(fseek,rewind)
Closing a file(fclose)
Built in function for file handling
• fopen()---creating a file or opening an existing file
• fclose()---closing a file
• fprintf()---writing a block of data to a file.
• fscanf()----Reading a block data from a file
• getc()-------Read a single character from a file
• putc()------writes a single character to a file
• fgets()-----Get a string/line from a file
• fputs()------writes a string/line to a file
• getw()-------reads an integer from a file
• putw()-------writing an integer to a file
• fseek()-------set the position of a file pointer to a specified location
• ftell()--------returns the current position of a file pointer
• rewind()------Sets the file pointer at the beginning of a fiel
File attributes
Procedure to Create a file
• Step1:create a file pointer
syntax: FILE * fileptr;
EX: FILE * my ptr;
Step 2:Assign a file for file pointer
SYNTAX: fileptr=fopen(“filename.txt”,”openmode”);
If open fails,Null returned
Ex: myptr=fopen(“INPUT.TXT”,”w”);
Step 3: write the content of a file using write functions
syntax: fputs(string,fileptr);
EX: fputs(“hello welcome t a file concepts”,myptr);
Step 4: close syntax: fclose(fileptr);
fclose(myptr):
Program to Create a file
• #include<stdio.h>
void main()
{
FILE *f;
f=fopen(“INPUT>TXT”,”w”);
fputs(“ hello everyone”);
fclose(f);
}
Procedure to a read a file
• Step 1: Create a filepointer
syntax: FILE*fileptr;
FILE *myptr;
Step 2: Assign a file for filepointer
fileptr=fopen(“filename.txt”,”openmode”);
myptr=fopen(“INPUT.TXT”,”r”);
Step 3: Read the contents from a file using any one of read functions
fgets(char *s,int n,fileptr);
fgets(s,25,myptr);
Step 4: printf(“%s”,s);
Step 5: fclose(fileptr):
fprintf() function
• It is used to write set of characters into file.
• It sends formatted output to a file.
• Syntax:
fprintf(File *stream,const char *format[argument})
Example
FILE * f;
f=fopen(“INPUT.TXT”,”w”)
fprintf(f,”hai welcome to BCC\n”);
fscanf()
• It is used to read set of characters from file.
• It reads a word from the file and returns EOF at the end of the file.
• SYNTAX:
fscanf(FILE *stream,const char * format[,argument,….])
Example:
FILE * f,
char s[25];
f=fopen(“INPUT.TXT”,”r”);
while(fscanf(f,”%s”,s)!=EOF)
{
printf(“%s”,s);
}
Program to read from a file using
fscanf()
• #include<stdio.h>
void main()
{
FILE *f;
char s[25];
int i=0;
f=fopen(“INPUT.TXT”,”r”);
while(fscanf(f,”%s”,s)!=EOF)
{
printf(“%s”,s);
i++;
}
printf(“\n number of words in this file is%d”,i);
Fclose(f);
}
fputc() function
• It is used to write a single character into a file.
• It outputs a character to a stream.
• Syntax:
• fputc(int c,FILE * stream);
• Example:
FILE * f;
f=fopen(“INPUT.TXT”,”w”);
putc(‘H’,f);
fputc(“\n”,f);
fclose(f);
Program to write a file using putc &
fputc
#include<stdio.h>
void main()
{
FILE *F;
F=fopen(“INPUT.TXT”,”w”);
putc(“H”,f);
fputc(‘\n’,f);
putc(“A”,f);
fputc(‘\n’,f);
putc(‘I’,f);
fclose(f);
}
Read from a file using fgetc &getc
#include<stdio.h>
void main()
{
FILE * f;
char s;
f=fopen(“INPUT.TXT”,”r”)
while((s=fgetc(f)!=EOF)
{
printf(“%c”,s);
}
fgetc() function
• It returns a single character from the file and returns EOF at the end of the fie.
• Syntax: int getc(FILE * stream)
• example
• FILE *f;
char s;
f=fopen(“INPUT.TXT”,”r”);
while((s=fgetc(f))!=EOF)
{
printf(“%c”,s);
}
fclose(f);
}
TYPES OF FILE PROCESSING
• SEQUENTIAL ACCESS
Data is kept in sequential order if we want to read the last record of
the file , We need to read all records before that records so it takes
more time.
All files are sequential files.
Random Access:
Data can be read and modified randomly. If we want to read the last
record we can read it directly.
It takes less time when compared to sequential file.
Program to find the average of
numbers stored in sequential access
file
#include<stdio.h>
void main()
{
FILE *f;
int m1,m2,m3;
float avg;
while(fscanf(f,”%d%d%d”,&m1,&m2,&m3)!=EOF)
{
avg=(m1+m2+m3)/3.0;
Printf(“\n%d+%d+%d average is%f”,m1,m2,m3,avg)
}
fclose(f);
}
Random File Access
• Random Access file Processing
fseek()
ftell()
rewind()
fseek()
• It is used for seeking the pointer position in the file at the specified
byte.
• Syntax:
• fseek(filepointer,displacement, pointerposition);
EX: fseek(p,10L,0)
Program for Random Access file
ftell(),rewind()
rewind()
It is used to move the file pointer to the beginning of the given file.
Syntax: rewind(fptr);
Program for Random Access in file
• #include<stdio.h>
void main()
{
FILE * fp;
int cp,n;
char ch;
fp=fopen(“RACC.Txt”,”r”);
if(fp==NULL)
printf(“file cannot be opened”);
else
{
cp=ftell(fp);
ch=fgetc(fp));
printf(“ value %d is %c\n”,cp,ch);
fseek(fp,7,0);
cp=ftell(fp);
ch=fgetc(fp);
printf(“value at %d is %c\n”,cp,ch);
}
Sequential access file
• A sequential access file is a type of file where data is stored and
accessed sequentially. Its meaning that you read or write data
sequentially, one after the other. This contrasts with random access
files, where you can directly access any piece of data within the file.
Sequential access file
#include<stdio.h>
#include<stdlib.h>
int main()
{
FILE * file;
char filename[]=“example.txt”;
char data[100];
file=fopen(filename,”w”);
if(file==NULL)
{
printf(“error opening file for writing\n”);
return 1;
}
• //write the data to the file
printf(“ enter data to write to file”);
fgets(data,sizeof(data),stdin);
fprintf(file,”%s”,data);
fclose(file);
printf(“data has been written to the file\n”);
//open file in a read mode
file=fopen(filename,”r”);
if(file==NULL)
{
printf(“ error opening file for reading”);
return 1;
}
//read data from the file
printf(“ contents of the file\n”);
fgets(data,sizeof(data),file);
printf(“%s”,data);
fclose(file);
return 0;
}
R a n d o m a c c e s s fi le
• RANDOM access files are the files in which the user or programmer can move the file pointer randomly that
can do read or write operations.
• ftell()--It is used to return the current position of the pointer in the given file
• n=ftell(filepointer); (it should be used in long integer)
Ex: FILE * ptr;
long n;
----
n=ftell(ptr);
--------
rewind()--to reset the position of the pointer to be the beginning of the given file.
rewind(filepointer)
EX: FILE * ptr;
long n;
-----
rewind(ptr)
n=ftell(ptr);
• fseek()
• it is used to move the file pointer to any desired location within this file.
• fseek( pointer variable,offset,position)
• 0beginning of the file
• 1current position
• 2 end of the file
fseek(ptr,0L,0)
fseek(ptr,4L,0)
fread() read the data a specified number of bytes from a file.
Syntax: fread(&var,sizeof(var),1,fptr);
Ex:fread(&ch,sizeof(ch),1,ptr)
fwrite() write the data into file.
Syntax: fwrite(&var,sizeof(var),1,fptr)
Ex: fwrite(&ch,sizeof(ch),1,ptr);
Pos=ftell(fp);
#include<stdio.h> printf(“ position of the pointer %d”,ptr);
printf(“content in the file”);
main() char ch;
{ while(fread(&ch,sizeof(ch),1,fp)==1)
{
FILE*fp; printf(“%c”,ch);
int pos; }
printf(“position of the pointer %d”,ftell(fp));
fp=fopen(“file.txt”,”r”); rewind(fp);
printf(“ position of the pointer”,ftell(fp));
if(!fp) fseek(fp,6,0);
{ while(fread(&ch,sizeof(ch),1,fp)==1)
{
printf(“error in opening file\n”); printf(“%c”,ch);
return 0; }
fclose(fp)
} }
Command line arguments
• Any input value passed through
command prompt at the time of
running a program
• main() -entry point of the
program
• Formal parameters so that
program may accept it.
• DOS
• The program must be run from
command prompt.
• Two arguments: • Compile and run
i) argc: integer variable programs are compiled and run
Receives the number of command on command prompt.
line arguments that the user has
entered.
ii) argv: an array of strings
the actual command line
arguments are stored.
• #include<stdio.h>
main(int argc,char *argv[])
{
int i;
printf(“numbers of arguments:%d”,argc);
for(i=0;i<argc;i++)
{
printf(“%s\n”,argv[i]);
}
}
STORAGE CLASS AND VISIBILITY
The storage classes define visibility(scope) and the life time of any function/variable within a C program.