0% found this document useful (0 votes)
2 views34 pages

Unit V

Uploaded by

singhvivkumar.11
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)
2 views34 pages

Unit V

Uploaded by

singhvivkumar.11
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/ 34

MIT School of Computing

Department of Computer Science & Engineering

First Year Engineering

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 Code: 23CSE1002 Course Credits: 01

Teaching Hours / Week (L:T:P): 0:0:2 CA Marks: 25

Total Number of Teaching Hours: 30 END-SEM Marks: 25

Course Prerequisites: None

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

Pointers and Arrays


When an array is declared,
–The compiler allocates a base address and sufficient amount of storage to contain
all the elements of the array in contiguous
memory locations.
–The base address is the location of the first element (index 0) of the array.
The compiler also defines the array name as a constant pointer to the first
element

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.

• Pointers provide an efficient way for accessing the elements of an array

structure.

• Pointers are used for dynamic memory allocation as well as deallocation.

• Pointers are used to form complex data structures such as linked list,

graph, tree, etc.

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

Five significant operations can be performed on files:


● Creation of a new file (fopen with attributes as “a” or “a+” or “w” or “w+”)
● Opening an existing file (fopen)
● Reading from file (fscanf or fgets)
● Writing to a file (fprintf or fputs)
● Moving to a specific location in a file (fseek, rewind)
● Closing a file (fclose)

Steps for Processing a File

● Declare a file pointer variable.


● Open a file using fopen() function.
● Process the file using the suitable function.
● Close the file using fclose() function.
17
MIT School of Computing
Department of Computer Science & Engineering

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

File Writing Function

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

File Reading Function

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

int fscanf(FILE *stream, const char *format [, argument, ...])

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

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;
● 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("fileopen","mode");
For example,
fopen("E:\\cprogram\\newprogram.txt","w");
fopen("E:\\cprogram\\oldprogram.bin","rb"); 25
MIT School of Computing
Department of Computer Science & Engineering

Working with files

● 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);

Here, fptr 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.

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

Writing File : fputc() function


The fputc() function is used to write a single character into file. It outputs a
character to a stream.
Syntax:
int fputc(int c, FILE *stream)
Example:
#include <stdio.h>
main()
{
FILE *fp;
fp = fopen("file1.txt", "w");//opening file
fputc('a',fp);//writing single character into file
fclose(fp);//closing file
}

29
MIT School of Computing
Department of Computer Science & Engineering

Reading File : fgetc() function


The fgetc() function returns a single character from the file. It gets a
character from the
stream. It returns EOF at the end of file.
Syntax:
int fgetc(FILE *stream)
Example:
#include<stdio.h>
#include<conio.h>
void main(){
FILE *fp;
char c;
clrscr();
fp=fopen("myfile.txt","r");
while((c=fgetc(fp))!=EOF){
printf("%c",c);
}
fclose(fp);
getch();
}
30
MIT School of Computing
Department of Computer Science & Engineering

Writing File : 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)
Example
#include<stdio.h>
#include<conio.h>
void main(){
FILE *fp;
clrscr();
fp=fopen("myfile2.txt","w");
fputs("hello c programming",fp);
fclose(fp);
getch();
}

31
MIT School of Computing
Department of Computer Science & Engineering

Reading File : 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)
Example:
#include<stdio.h>
#include<conio.h>
void main(){
FILE *fp;
char text[300];
clrscr();
fp=fopen("myfile2.txt","r");
printf("%s",fgets(text,200,fp));
fclose(fp);
getch();
}
32
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

You might also like