0% found this document useful (0 votes)
11 views19 pages

UNIT5 PS Part2

The document provides an overview of file handling in C programming, detailing the types of files (text and binary) and their characteristics. It explains essential file operations such as creating, opening, reading, writing, and closing files, along with the necessary functions like fopen(), fclose(), fprintf(), and fread(). Additionally, it covers file pointer declaration and functions like fseek() and ftell() for managing file positions.

Uploaded by

2401020626
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)
11 views19 pages

UNIT5 PS Part2

The document provides an overview of file handling in C programming, detailing the types of files (text and binary) and their characteristics. It explains essential file operations such as creating, opening, reading, writing, and closing files, along with the necessary functions like fopen(), fclose(), fprintf(), and fread(). Additionally, it covers file pointer declaration and functions like fseek() and ftell() for managing file positions.

Uploaded by

2401020626
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/ 19

Programming for Problem Solving

COURSE CODE:CS101

1st Semester

File in C
UNIT 5
Dr. Ram Chandra Barik
Dr. Ram Chandra Barik
FILE
A file is a container in computer storage devices used for storing 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 FILE
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 we 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 your 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.
FILE

File Operations
In C, you can perform four major operations on files, either text or binary:
❑Creating a new file
❑Opening an existing file
❑Reading from and writing information to a file
❑Closing a file
FILE POINTER DECLARATION

Working with files


When working with files, you need to declare a pointer of type file. This declaration is needed for
communication between the file and the program.

FILE *fptr;
FILE *x;
Opening a file - for creation and edit
Opening a file is performed using the fopen() function defined in the stdio.h header file.

The syntax for opening a file in standard I/O is:

ptr = fopen("filepath/location","mode");

For example,
fopen(“D:\\cprogram\\newprogram.txt","w");
fopen(“D:\\cprogram\\oldprogram.bin","rb");

❑ Let's suppose the file newprogram.txt doesn't exist in the location E:\cprogram. The first function creates
a new file named newprogram.txt and opens it for writing as per the mode 'w'.
❑ The writing mode allows you to create and edit (overwrite) the contents of the file.

❑ Now let's suppose the second binary file oldprogram.bin exists in the location E:\cprogram. The second
function opens the existing file for reading in binary mode 'rb'.
❑ The reading mode only allows you to read the file, you cannot write into the file.
FILE OPERATION MODES
Closing a FILE

Closing a File
The file (both text and binary) should be closed after reading/writing.

Closing a file is performed using the fclose() function.

fclose(fptr);
fclose(x);
Here, fptr or x is a file pointer associated with the file to be closed.
Reading and writing to a text file

❑ For reading and writing to a text file, we use the functions


fprintf() and fscanf().

❑ They are just the file versions of printf() and scanf(). The
only difference is that fprintf() and fscanf() expects a
pointer to the structure FILE.
Write to a text file

This program takes a number from the user and

stores in the file program.txt.


After we compile and run this program, we can see a
text file program.txt created in C drive in computer.
When we open the file, we can see the integer we
entered.
Read from a text file

This program reads the integer present in the

program.txt file and prints it onto the screen.


If we successfully created the file from Example 1, running
this program will get the integer what was entered.

Other functions like fgetchar(), fputc() etc. can be used in


a similar way.
Read and write from/to a Binary file

Functions fread() and fwrite() are used for reading from and writing to a file on the disk respectively in case of binary files.

Writing to a binary file


To write into a binary file, you need to use the fwrite() function. The functions take four arguments:

❑ address of data to be written in the disk


❑ size of data to be written in the disk
❑ number of such type of data
❑ pointer to the file where you want to write.

fwrite(addressData, sizeData, numbersData, pointerToFile);


Write to a binary file using fwrite()
Reading and writing to a binary file

❑ In this program, we create a new file program.bin in the C drive.

❑ We declare a structure threeNum with three numbers - n1, n2 and n3, and
define it in the main function as num.

❑ Now, inside the for loop, we store the value into the file using fwrite().

❑ The first parameter takes the address of num and the second parameter takes
the size of the structure threeNum.

❑ Since we're only inserting one instance of num, the third parameter is 1. And,
the last parameter *fptr points to the file we're storing the data.

❑ Finally, we close the file.


Read from a binary file using fread()

In this program, you read the same file


program.bin and loop through the records one by
one.

In simple terms, you read one threeNum record


of threeNum size from the file pointed by *fptr
into the structure num.
fseek()

fseek() is used to move the file pointer associated with a given file to a specific position.

The fseek() syntax is:


int fseek(FILE *pointer, long int offset, int position);

Parameters
•pointer: It is the pointer to a FILE object that identifies the stream.
•offset: It is the number of bytes to offset from the position
•position: It is the position from where the offset is added. Position defines the point with respect to which the
file pointer needs to be moved. It has three values:
• SEEK_END: It denotes the end of the file.
• SEEK_SET: It denotes starting of the file.
• SEEK_CUR: It denotes the file pointer’s current position.
Return Value
•It returns zero if successful, or else it returns a non-zero value.
fseek()

fopen(“c:\test.txt”, ”r”)
ftell()

ftell() in C is used to find out the position of the file pointer in the file with respect to starting of the file.

The syntax of ftell() is:

long ftell(FILE *stream);

Parameters
•stream: It is the pointer to the file stream.

Return Value
•It returns a long integer value as the current position in the file.
•It returns -1 if an error occurs.
ftell()

g4g.txt
Someone over there is
calling you. We are
going for work. Take
care of yourself.

Output
7

You might also like