0% found this document useful (0 votes)
35 views4 pages

Files

Files allow storing information permanently on a disk. There are two types: sequential and random access. Sequential files require reading all records sequentially to access a specific record, while random access files allow directly accessing any record. File operations in C involve opening, writing, reading, and closing a file. Functions like fopen(), fscanf(), fprintf(), and fclose() are used to perform these operations. Files are opened using modes like read, write, and append and the file pointer returned by fopen() is used for input/output operations on the file.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
35 views4 pages

Files

Files allow storing information permanently on a disk. There are two types: sequential and random access. Sequential files require reading all records sequentially to access a specific record, while random access files allow directly accessing any record. File operations in C involve opening, writing, reading, and closing a file. Functions like fopen(), fscanf(), fprintf(), and fclose() are used to perform these operations. Files are opened using modes like read, write, and append and the file pointer returned by fopen() is used for input/output operations on the file.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Files

In console operation the output of the program will store on temporary memory location.
So , To save such information on the disk we use data files. File is a collection of numbers,
symbols & text placed on the disk. Thus, files allows us to store information permanently in the
disk.

Definition of File:

File is a set of records that can accessed through the set of library functions. File is a
group of records which is stored on the disk. Doing operations in files, we use “FILE” key word.

Streams And File Types:

Stream means reading and writing of data. The streams are designed to allow the user to
access the files efficiently. A stream is a file or physical device like keyboard, printer and
monitor. The FILE object uses these devices.

The FILE object contains all the information about stream like current position, pointer
to any buffer, error and EOF(End Of File).

File Types:

There are two types of files.

1. Sequential file
2. Random accesses file.
Sequential file:
In this type data is stored in sequentially. If we want to read the last record of the file we
need to read all the records before that record. It takes more time. (Or) if we want to access the
10th record then the first 9 records should be read sequentially for reaching to the 10th record.
Random accesses 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.

Steps for File Operations:

There are four steps for file operations in C.

a) Opening of File.
b) Writing the data into File.
M.L.Rajeswari, Assist, Prof..,BVRICE Page 1
c) Reading the data from File.
d) Closing File.

C language supports the following file handling functions.

1. fopen(): This function creates a new file read/write operations. Or opens a file.
2. fscanf(): Reads data from the existing file.
3. fprintf(): writes data to the file.
4. fclose(): This function is used to closes the already existing file.
5. getc(): Reads single character from the file.
6. putc(): Writes single character to the file.
7. gets(): Reads string from the file.
8. puts(): Writes string to the file.
9. closeall(): Closes all opened files with fopen().
10. fwrite(): writes block of structured data to the file.
11. fread(): Reads structured data written by fwrite() function.

Opening a file:

Opening a file creates a link between the operating system and the file functions. We
have to specify the name of file and it’s mode to the operating system. We have three types of
modes. They are

a) “w”: Write mode which tells, a file is open for writing.


b) “r”: Read mode which tells, a file is open for reading.
c) “a”: Append mode which tells, a file is open for appending.

Declaration of file:

Syntax for creating a file:

FILE *filepointer;

Now we can open a file with the help the help of file ponter.

Ex: FILE *fp;

M.L.Rajeswari, Assist, Prof..,BVRICE Page 2


Syntax for opening the file:

Filepointer name=fopen(“filename”,”mode”);

Ex: fp=fopen(“data.txt”,”r”);

Here ‘fopen()’ performs three important tasks.

1. It searches the disk for opening a file.


2. In the existing file, it loads the file from the disk into memory. If the file found with huge
contents then it loads the file part by part.
3. If the file is not existing in this function, it returns a NULL. NULL is a macro defined
character in the header file “stdio.h”. This indicates that is unable to open file. Because when
the file is in protected or hidden mode another one is the file may be used for another
program.
4. It locates a character pointer, which points the first character of the file. Whenever a file is
opened the character pointer points to the first character of the file.

Closing a File:

The file that is opened from the open() should be closed after the work is over i.e., we
need to close the file after reading and writing operations are over.

Syntax for Closing a file:

fclose(file pointer name);

Ex: fclose(fp);

fscanf():

this function reads character, strings, integers, floats etc. from the file opened by file pointer.

Sysntax for fscanf():

fscanf( file pointer name, “format specifier”, address of the variable name);

Ex: fscanf(fp,”%d”,&a);

M.L.Rajeswari, Assist, Prof..,BVRICE Page 3


fprintf():

this function is used for writing characters, strings, integers, float etc. to the file. It contains
one more parameter that is file pointer, which points the opened file.

Syntax for fprintf():

fprintf( file pointer name, “format specifier”, variable name);

Ex: fprintf(fp,”%d”,a);

M.L.Rajeswari, Assist, Prof..,BVRICE Page 4

You might also like