Sunteți pe pagina 1din 22

FILES IN C

Why files are needed?


•Memory is volatile; when program terminates data is lost.
•In C, file is a place on the physical disk where information is
stored.
•Necessary to store data in a manner that can be retrieved
later.
Data Organization
•All data is stored in binary form.
•How it is stored varies from one OS to another.
Types of Files
•There are two main types of files:
1. Text Files
2. Binary Files
Text Files and Binary Files
•Text files are normal .txt files that can be easily created using Notepad or any other text editors.
•All the contents are displayed as plain text.
•Edit or delete contents.
•Easily readable and takes bigger storage space.

•Binary files are .bin files.


•Stores in binary form.
•Not readable easily.
File Operations
Different operations that can be carried out are:
•Creating a new file
•Opening an existing file
•Reading from a file
•Writing to a file
•Moving to a specific location in a file(seeking).
•Closing a file
•When working with files, a pointer of type file has to be declared.
•Necessary for the communication between file and program
type pointer_name
FILE *ptr;
Opening a file
•Operation perform using fopen() library function.

•Syntax:
ptr = fopen(“filepath”, “mode”);

Example:
fp = fopen ( "PR1.C", "r" ) ;
fp = fopen(“E:\\cprogram\\newprogram.txt”, “w”);
Opening modes for Text Files
Mode Operation

r Operations possible – reading from the file. Returns NULL if not present.
w Operations possible – writing to the file. Creates new file if it doesn’t exist.
a Operations possible - adding new contents at the end of file.
r+ Operations possible - reading existing contents, writing newcontents, modifying
existing contents of the file.
w+ Operations possible - writing new contents, reading them back and modifying existing
contents of the file.
a+ Operations possible - reading existing contents, appending new contents to end of file.
Cannot modify existing contents.
Opening modes in Binary Files
Mode Operation

rb Open for reading in binary mode. fopen() returns NULL if file doesn’t exist.
wb Open for writing in binary mode. New file is created if it doesn’t exist.
ab Open to append in binary mode. Data is added to the end of file.
rb+ Open for both reading and writing in binary mode. fopen() returns NULL.
wb+ Open for both reading and writing in binary mode
ab+ Open for both reading and appending in binary mode.
Closing a file

•After every read/write, file must be closed.


•Performed using library function fclose().
Syntax:
fclose(fptr);
Reading and Writing to a Text file
•fprintf() and fscanf() – reading and writing to a file
•File version of printf() and scanf()
•Difference is that both expect a pointer to the FILE structure.
Example:
fprintf ( fptr, ”%d”, num);
fscanf(fptr, “%d”, &num);
Writing to a text file
Reading from a text file
Reading and Writing to a Binary file
• fread() and fwrite() – reading from and writing to a binary file.
•fwrite() function takes 4 arguments-
i. Address of data to be written in disk
ii. Size of data to be written
iii. Number of such type of data
iv. Pointer to the file where you want to write
Syntax:
fwrite(&num, sizeof(num), 1, fptr);
•fread() also takes four arguments
Syntax –
fread(&num, sizeof(struct student), 1, fptr);
Writing to a binary file
Reading from a binary file
Moving to a specific location in a file(seeking)
•To access a specific record in a file.
•fseek() seeks the cursor to the given record in the file.
Syntax:
fseek(FILE * stream, long int offset, int whence);
1. First parameter is pointer to the file
2. Second parameter is the position of the record to be found
3. Third parameter specifies the location where the offset starts.
Different whence in fseek :-

SEEK_SET : Starts the offset from the beginning of the file


SEEK_END : Starts the offset from the end of the file
SEEK_CUR : Starts the offset from the current location of the cursor
THANK YOU.

S-ar putea să vă placă și