Sunteți pe pagina 1din 5

Files

Definition:
A file represents a sequence of bytes on the disk where a group of related data is stored. File is
created for permanent storage of data.

Why files are needed?

 When a program is terminated, the entire data is lost. Storing in a file will preserve your data
even if the program terminates.
 If you have to enter a large number of data, it will take a lot of time to enter them all.
However, if you have a file containing all the data, you can easily access the contents of the file
using a few commands in C.
 You can easily move your data from one computer to another without any changes.

Types of Files

When dealing with files, there are two types of files you should know about:

1. Text files
2. Binary files
1. Text files
 Text files are the normal .txt files. We can easily create text files using any simple text
editors such as Notepad.
 When you open those files, you'll see all the contents within the file as plain text. You
can easily edit or delete the contents.
 They take minimum effort to maintain, are easily readable, and provide the least security
and takes bigger storage space.
2. Binary files
 Binary files are mostly the .bin files in computer.
 Instead of storing data in plain text, they store it in the binary form (0's and 1's).
 They can hold a higher amount of data, are not readable easily, and provides better
security than text files.

Creating text file:


 To create a text file we use a built-in FILE structure.
 We need to create pointer to FILE type.
 The pointer to FILE type will hold a logical reference to file on disk (hard disk).
 To create a file in a 'C' program following syntax is used,
FILE *fp;
fp = fopen ("file_name", "mode");

 In the above syntax, the file is a data structure which is defined in the standard library.
 fopen is a standard function which is used to open a file.
 If the file is not present on the system, then it is created and then opened.
 If a file is already present on the system, then it is directly opened using this function.

Following are the different types of modes in 'C' programming:


mode description

r opens a text file in reading mode

w Opens or create a text file in writing mode.

a opens a text file in append mode

r+ opens a text file in both reading and writing mode

w+ opens a text file in both reading and writing mode

a+ opens a text file in both reading and writing mode

rb opens a binary file in reading mode

wb opens or create a binary file in writing mode

ab opens a binary file in append mode

rb+ opens a binary file in both reading and writing mode

wb+ opens a binary file in both reading and writing mode

ab+ opens a binary file in both reading and writing mode

How to Close a file:


 We should always close a file whenever the operations on file are over.
 This prevents accidental damage to the file.
 'C' provides the fclose function to perform file closing operation.
 The syntax of fclose is as follows,

fclose (file_pointer);

Example:
FILE *fp;
fp = fopen ("data.txt", "r");
fclose (fp);

 The fclose function takes a file pointer as an argument.


 It returns 0 if close was successful and returns EOF (end of file) if there is an error has
occurred while file closing.

Formatted and unformatted i/o operations:


Writing to a File:
The stdio library offers the necessary functions to write to a file:
Unformatted output:
 fputc(char, file_pointer): It writes a character to the file pointed to by file_pointer.

 fputs(str, file_pointer): It writes a string to the file pointed to by file_pointer.

Formatted output:
 fprintf(file_pointer, str, variable_lists): It prints a string to the file pointed to by
file_pointer. The string can optionally include format specifiers and a list of variables
variable_lists.

Reading data from a File:


There are three different functions dedicated to reading data from a file:

Unformatted input:
 fgetc(file_pointer): It returns the next character from the file pointed to by the file
pointer. When the end of the file has been reached, the EOF is sent back.

 fgets(buffer, n, file_pointer): It reads n-1 characters from the file and stores the string in
a buffer in which the NULL character '\0' is appended as the last character.

Formatted input:
 fscanf(file_pointer, conversion_specifiers, variable_adresses): It reads characters from
the file and assigns the input to a list of variable pointers variable_adresses using
conversion specifiers. Keep in mind that as with scanf, fscanf stops reading a string when
space or newline is encountered.

C provides a number of functions that helps to perform basic file operations. Following are the
functions,
Functionx description
fopen() create a new file or open a existing file
fclose() closes 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

Random files:
There is no need to read each record sequentially, if we want to access a particular record.
C supports following functions for random access file processing.
 fseek()
 ftell()
 rewind()

fseek():

This function is used for seeking the pointer position in the file at the specified byte.
Syntax:
fseek( file pointer, displacement, pointer position);
Where
 file pointer ---- It is the pointer which points to the file.
 displacement ---- It is positive or negative. This is the number of bytes which are
skipped backward (if negative) or forward (if positive) from the current position. This
is used with L because it is long int.
 Pointer position----This sets the pointer position in the file.

Value pointer position


0 Beginning of file.
1 Current position
2 End of file

Ex:
1) fseek( p,10L,0)

0 means pointer position is on beginning of the file; the 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. the 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():
This function returns the value of the current pointer position in the file. The value is count
from the beginning of the file.
Syntax:
ftell(fptr);

Where fptr is a file pointer.

rewind():
This function is used to move the file pointer to the beginning of the given file.
Syntax:
rewind( fptr);

Where fptr is a file pointer.

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