0% found this document useful (0 votes)
324 views16 pages

C Pointers & File Handling Guide

PPS

Uploaded by

Satwik Baraskar
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)
324 views16 pages

C Pointers & File Handling Guide

PPS

Uploaded by

Satwik Baraskar
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/ 16

Unit- 5 Pointers and File Handling

Introduction to pointers, Pointers with arrays, Pointers to structures, Pointers to pointers. Input
and Output: Introduction to Files, Modes of Files, Streams, Standard Library Input/Output
Functions, Input/Output Functions, Input?Output operations on files.

1
What is a Pointer?

• The Pointer in C, is a variable that stores address of another variable.

• A pointer can also be used to refer to another pointer function.

• A pointer can be incremented/decremented, i.e., to point to the


next/ previous memory location.

• The purpose of pointer is to save memory space and achieve faster


execution time.
How to Use Pointers in C ?

• If we declare a variable v of type int, v will actually store a value.


int v=0;
• v is equal to zero now.
• However, each variable, apart from value, also has its address (or,
simply put, where it is located in the memory).
• The address can be retrieved by putting an ampersand (&) before the
variable name.
&v
• If you print the address of a variable on the screen, it will look like a
totally random number (moreover, it can be different from run to run).
What is a Pointer?
• Instead of storing a value, a pointer will y store the address of a
variable.

• Pointer Variable
• Int *y = &v;

VARIABLE POINTER

A value stored in a named storage/memory A variable that points to the storage/memory


address address of another variable
Declaring a Pointers in C

• Like variables, pointers in C programming have to be declared before


they can be used in your program.
• Pointers can be named anything you want as long as they obey C's
naming rules.
• A pointer declaration has the following form.
• data_type * pointer_variable_name;
• Here,
• data_type is the pointer's base type of C's variable types and indicates
the type of the variable that the pointer points to.
• The asterisk (*: the same asterisk used for multiplication) which is
indirection operator, declares a pointer.
Initializing a Pointers in C
#include <stdio.h>
• After declaring a pointer, we initialize it int main()
like standard variables with a variable {
address. int a=10; //variable declaration
• If pointers in C programming are not int *p; //pointer variable
uninitialized and used in the program, declaration p=&a; //store
the results are unpredictable and address of variable a in pointer
potentially disastrous. p
• To get the address of a variable, we use printf("Address stored in a
the ampersand (&)operator, placed variable p is:%x\n",p);
before the name of a variable whose //accessing the address
address we need. printf("Value stored in a
• Pointer initialization is done with the variable p is:%d\n",*p);
following syntax. pointer = &variable; //accessing the value return 0;
}
Operators of Pointers in C

Operator Meaning

* Serves 2 purposes
1.Declaration of a pointer
2.Returns the value of the referenced variable

& Serves only 1 purposeReturns the address of a


variable
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.
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.
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.
File Handling

? Basics of File Handling


Basics of File Handling
? File handling in C enables us to create, update, read, and delete the files
stored on the local file system through our C program. The following
operations can be performed on a file.
? Different operations that can be performed on a file are:
1. Creation of a new file (fopen with attributes as “a” or “a+” or “w” or “w++”)
2. Opening an existing file (fopen)
3. Reading from file (fscanf or fgets)
4. Writing to a file (fprintf or fputs)
5. Moving to a specific location in a file (fseek, rewind)
6. Closing a file (fclose)
File Handling Functions
Functions for file handling
There are many functions in the C library to open, read, write, search and close the file. A list of file functions
are given below:

No. Function Description No. Function Description

1 fopen() opens new or 7 fseek() sets the file pointer


existing file to given position

2 fprintf() write data into the 8 fputw() writes an integer to


file file

3 fscanf() reads data from the 9 fgetw() reads an integer


file from file

4 fputc() writes a character 10 ftell() returns current


into the file position

5 fgetc() reads a character


11 rewind() sets the file pointer
from file
to the beginning of
the file
6 fclose() closes the file
File writing Function
15
Writing File :

1) fprintf() function

The
1. 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)
File Reading Function
16

Reading File :

1) fscanf() function
The fscanf() function is used to read set of characters from file. It reads
1.
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)

You might also like