Unit V
Unit V
23CSE1002 - C Programming
PLD
Class - F.Y. (SEM-II)
Unit - V
Pointers and File handling
1
MIT School of Computing
Department of Computer Science & Engineering
C Programming
SEMESTER – I
Course Description:
In this course, students will learn the basics of programming using the C language. They will use their problem-solving skills by
designing and implementing solutions to various programming challenges. This course provides an overview of the principles
and practices of programming using the C language. Students will learn about the syntax and semantics of the language.
2
MIT School of Computing
Department of Computer Science & Engineering
3
MIT School of Computing
Department of Computer Science & Engineering
4
MIT School of Computing
Department of Computer Science & Engineering
5
MIT School of Computing
Department of Computer Science & Engineering
6
MIT School of Computing
Department of Computer Science & Engineering
7
MIT School of Computing
Department of Computer Science & Engineering
8
MIT School of Computing
Department of Computer Science & Engineering
9
MIT School of Computing
Department of Computer Science & Engineering
10
MIT School of Computing
Department of Computer Science & Engineering
Pointer to pointer
1. Pointer which store address of another pointer is
called as pointer of pointer.(Double Pointer)
2. General syntax is:
data type **name of pointer of pointer;
Example: int **p;
11
MIT School of Computing
Department of Computer Science & Engineering
12
MIT School of Computing
Department of Computer Science & Engineering
Advantages of Pointers in C ?
• Pointers are useful for accessing memory locations.
structure.
• Pointers are used to form complex data structures such as linked list,
13
MIT School of Computing
Department of Computer Science & Engineering
Disadvantages of Pointers in C ?
• Pointers are a little complex to understand.
• Pointers can lead to various errors such as segmentation faults or can access a
memory location which is not required at all.
• If an incorrect value is provided to a pointer, it may cause memory corruption.
• Pointers are also responsible for memory leakage.
• Pointers are comparatively slower than that of the variables.
• Programmers find it very difficult to work with the pointers; therefore it is
programmer's responsibility to manipulate a pointer carefully.
14
MIT School of Computing
Department of Computer Science & Engineering
Summary
• A pointer is nothing but a memory location where data is stored.
• A pointer is used to access the memory location.
• There are various types of pointers such as a null pointer, wild pointer,
void pointer and other types of pointers.
• Pointers can be used with array and string to access elements more
efficiently.
• We can create function pointers to invoke a function dynamically.
• Arithmetic operations can be done on a pointer which is known as
pointer arithmetic.
• Pointers can also point to function which make it easy to call different
functions in the case of defining an array of pointers.
• When you want to deal different variable data type, you can use a
typecast void pointer.
15
MIT School of Computing
Department of Computer Science & Engineering
File Handling
• A file is a container in computer storage devices used for storing data.
• I/O functions handle data on a secondary storage device
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.
16
MIT School of Computing
Department of Computer Science & Engineering
C file operations
18
MIT School of Computing
Department of Computer Science & Engineering
19
MIT School of Computing
Department of Computer Science & Engineering
20
MIT School of Computing
Department of Computer Science & Engineering
21
MIT School of Computing
Department of Computer Science & Engineering
22
MIT School of Computing
Department of Computer Science & Engineering
Writing File :
1) fprintf() function
The fprintf() function is used to write set of characters into file. It sends formatted
output to a stream.
Syntax
int fprintf(FILE *stream, const char *format [, argument, ...])
2) fputs() function
The fputs() function writes a line of characters into file. It outputs string to a
stream.
Syntax
int fputs(const char *s, FILE *stream)
23
MIT School of Computing
Department of Computer Science & Engineering
Reading File :
1) fscanf() function
The fscanf() function is used to read set of characters from file. It reads a word
from the file and returns EOF at the end of file.
Syntax
2) fgets() function
The fgets() function reads a line of characters from file. It gets string from a
stream.
Syntax
char* fgets(char *s, int n, FILE *stream)
24
MIT School of Computing
Department of Computer Science & Engineering
● 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);
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.
26
MIT School of Computing
Department of Computer Science & Engineering
27
MIT School of Computing
Department of Computer Science & Engineering
28
MIT School of Computing
Department of Computer Science & Engineering
29
MIT School of Computing
Department of Computer Science & Engineering
31
MIT School of Computing
Department of Computer Science & Engineering
Summary
● File handling refers to the method of storing data in the C program in the form of an
output
or input that might have been generated while running a C program in a data file, i.e.,
a
binary file or a text file for future analysis and reference in that very program.
● There are times when the output generated out of a program after its compilation
and running do not serve our intended purpose. In such cases, we might want to check
the program’s output various times. Now, compiling and running the very same
program multiple times becomes a tedious task for any programmer. It is exactly
where file handling becomes useful.
● We create the text files using an extension .txt with the help of a simple text editor.
The binary files store info and data in the binary format of 0’s and 1’s (the binary
number system). The binary files are created with the extension .bin in a program.
● We can use a variety of functions in order to open a file, read it, write more data,
create a new file, close or delete a file, search for a file, etc. These are known as file
handling operations in C.
33
MIT School of Computing
Department of Computer Science & Engineering
Thank You..!!!
34