0% found this document useful (0 votes)
14 views

Files

Uploaded by

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

Files

Uploaded by

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

Files

File is a set of records that can be accessed through the set of library functions.
There are two types of files, namely
Sequential file
Random Access file

Sequential File:-
•In this type records are kept sequentially.
•If we want to read the last record of the file , then we need to read all the
records before that record.
Random Access File:-
•In this type data can be read and modified randomly.
•In this type , if we want to read the last records of the file, we can read it
directly.
•It takes less time as compared to sequential file
‘C’ supports a number of functions that
have the ability to perform basic file
operations such as
• Naming a file
• Opening a file
• Reading data from a file
• Writing data to a file
• Closing a file
• File handling is one of the most important
parts of programming. In C, we use a structure
pointer of a file type to declare a file:
• FILE *fp;
File Handling functions
C provides a number of build-in function to perform basic file operations:
• fopen() - create a new file or open a existing file
• fclose() - close a file
• getc() - reads a character from a file
• putc() - writes a character to a file
• fscanf() - reads a set of data from a file
• fprintf() - writes a set of data to a file
• getw() - reads a integer from a file
• putw() - writes a integer to a file
• fseek() - set the position to desire point
• ftell() - gives current position in the file
• rewind() - set the position to the beginning point
Opening a file

• The fopen() function is used to create a file or


open an existing file.
• There are many modes for opening a file:
r - open a file in read mode
w - opens or create a text file in write mode
a - opens a file in append mode
r+ - opens a file in both read and write mode
a+ - opens a file in both append and read mode
w+ - opens a file in both write and read mode
Closing a file
• file must be closed as soon as all operations on
it have been completed.
• In order to close a file that has been opened we
use the function fclose(). The general format of
closing a file is
fclose (file-pointer);
Ex: File *fp;
fp = fopen (“demo.txt “,”r”)
fclose (fp);
Random Accesses to Files of Records.
• There is no need to read each record
sequentially, if we want to access a particular
record. C supports these functions for random
access file processing.

• fseek()
• ftell()
• rewind()
fseek():

This function is used to move the file position to a desired location within
the file. It takes the following form.
fseek (file-ptr, offset, position);

file-ptr is a pointer to the file concerned.


offset is a number or variable of type long . The offset specifies the number to
reposition the file pointer towards the backward or forward direction.
The position can take one of the following three values.

Integer Value Constant Meaning


0 SEEK_SET beginning of file
1 SEEK_CUR current position
2 SEEK_END end of file.
Ex:
1) fseek( p,10L,0)

0 means pointer position is on beginning of the file,from this


statement pointer position is skipped 10 bytes from the
beginning of the file.
2) fseek( p,5L,1)

1 means current position of the pointer position.From this


statement pointer position is skipped 5 bytes forward from the
current position.
3) fseek(p,-5L,1)

From this statement pointer position is skipped 5 bytes


backward from the current position.
ftell()
• It takes a file pointer and returns a
number of type long, that corresponds to the
correct positionIt takes the following form
n =ftell(fp);
• ‘n’ would give the relative offset (in bytes) of
the current position. This means that ‘n’ bytes
have already been read (or) written.
rewind()
• It takes a file pointer and resets the position to
the start of the file.
For example, the statement
rewind (fp);
n = ftell(fp);
would assign 0 to n because the file position has
been set to the start of the file by rewind.
Write to a text file

#include <stdio.h>
#include <stdlib.h>
int main()
{
int num;
FILE *fptr; // use appropriate location if you are using MacOS or Linux
fptr = fopen("C:\\program.txt","w");
if(fptr == NULL)
{
printf("Error!");
exit(1);
}
printf("Enter num: ");
scanf("%d",&num);
fprintf(fptr,"%d",num);
fclose(fptr);
return 0;
}
Read from a text file
int main()
{
int num;
FILE *fptr;
if ((fptr = fopen("C:\\program.txt","r")) == NULL)
{
printf("Error! opening file"); // Program exits if the file
pointer returns NULL.
exit(1);
}
fscanf(fptr,"%d", &num);
printf("Value of n=%d", num);
fclose(fptr);
return 0;
}
Ex pgm on fseek()
#include <stdio.h>
void main(){
FILE *fp;

fp = fopen("myfile.txt","w+");
fputs("This is javatpoint", fp);

fseek( fp, 7, SEEK_SET );


fputs("sonoo jaiswal", fp);
fclose(fp);
}
// C Program to demonstrate the use of fseek()
#include <stdio.h>

int main()
{
FILE *fp;
fp = fopen("test.txt", "r");

// Moving pointer to end


fseek(fp, 0, SEEK_END);

// Printing position of pointer


printf("%ld", ftell(fp));

return 0;
}
Output:
81
• The file test.txt contains the following text:

"Someone over there is calling you. we are


going for work. take care of yourself."
#include <stdio.h>
int main ()
{
FILE *fp;
fp = fopen("file.txt","w+");
fputs("This is tutorialspoint.com", fp);
fseek( fp, 7, SEEK_SET );
fputs(" C Programming Language", fp);
fclose(fp);
return(0);
}
#include <stdio.h>
int main ()
{
FILE *fp;
char data[60];
fp = fopen ("test.c","w");
fputs("Fresh2refresh.com is a programming tutorial website", fp);
fgets ( data, 60, fp );
printf(Before fseek - %s", data);

// To set file pointet to 21th byte/character in the file


fseek(fp, 21, SEEK_SET);
fgets ( data, 60, fp );
printf("After SEEK_SET to 21 - %s", data);

// To find backward 10 bytes from current position


fseek(fp, -10, SEEK_CUR);
fgets ( data, 60, fp );
printf("After SEEK_CUR to -10 - %s", data);

// To find 7th byte before the end of file


fseek(fp, -7, SEEK_END);
fgets ( data, 60, fp );
printf("After SEEK_END to -7 - %s", data);

// To set file pointer to the beginning of the file


fseek(fp, 0, SEEK_SET); // We can use rewind(fp);

fclose(fp);
return 0;
}
OUTPUT:
Before fseek – Fresh2refresh.com is a
programming tutorial website
After SEEK_SET to 21 – a programming tutorial
website
After SEEK_CUR to -10 – sh.com is a
programming tutorial website
After SEEK_END to -7 – website

You might also like