+ All Categories
Home > Documents > CProgramming_LA07_Day2

CProgramming_LA07_Day2

Date post: 14-Apr-2018
Category:
Upload: tanujkarnik49
View: 216 times
Download: 0 times
Share this document with a friend
36
7/30/2019 CProgramming_LA07_Day2 http://slidepdf.com/reader/full/cprogrammingla07day2 1/36 C Programming Day 2
Transcript
Page 1: CProgramming_LA07_Day2

7/30/2019 CProgramming_LA07_Day2

http://slidepdf.com/reader/full/cprogrammingla07day2 1/36

C Programming

Day 2

Page 2: CProgramming_LA07_Day2

7/30/2019 CProgramming_LA07_Day2

http://slidepdf.com/reader/full/cprogrammingla07day2 2/36

2Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/LA07/003

Version No. 1.0 

Union

• Union

– mechanism to create user defined data types– single piece of memory that is shared

• The variables that share the memory may be of the same type or

different types

• Only one variable in the union can be in use at any point of time

Page 3: CProgramming_LA07_Day2

7/30/2019 CProgramming_LA07_Day2

http://slidepdf.com/reader/full/cprogrammingla07day2 3/36

3Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/LA07/003

Version No. 1.0 

• Size of the union is fixed at compile time and it’s large enough to

accommodate the largest element in the union

• Accessing elements of a union is similar to accessing elements of a

structure

Union… 

union encrypt{int iIntVar;char acArrayVar[2];

}uEnc;

uEnc.iIntVar=10;uEnc.acArrayVar[0]=‘a’; 

uEnc.acArrayVar[1]=‘b’; 

Page 4: CProgramming_LA07_Day2

7/30/2019 CProgramming_LA07_Day2

http://slidepdf.com/reader/full/cprogrammingla07day2 4/36

4Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/LA07/003

Version No. 1.0 

Lists - Stacks

• Stack is a Linear data structure in which addition of new element or

deletion of existing element always takes place at the same end

• The end at which addition and deletion takes place is called as the

top of the stack

• The element which is entered most recently is the first to beremoved from the stack i.e. Last in First Out (LIFO)

Page 5: CProgramming_LA07_Day2

7/30/2019 CProgramming_LA07_Day2

http://slidepdf.com/reader/full/cprogrammingla07day2 5/36

5Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/LA07/003

Version No. 1.0 

Lists - Stacks

• Illustration of a Stack

10

20

10

30

20

10

20

10

Stack empty

10 pushed

on the stack

20 pushed

on the stack

30 pushed

on the stack

30 popped from

the stack

Top

Top

Top

Top

Top

Page 6: CProgramming_LA07_Day2

7/30/2019 CProgramming_LA07_Day2

http://slidepdf.com/reader/full/cprogrammingla07day2 6/36

6Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/LA07/003

Version No. 1.0 

Lists - Queues

• Queue is a data structure in which addition of new element is

done at the rear of the queue and deletion of an element is donefrom the front of the queue.

• The first element to enter the queue is the first one to go outi.e. First in First out (FIFO).

• Queue is a Linear Data structure

Page 7: CProgramming_LA07_Day2

7/30/2019 CProgramming_LA07_Day2

http://slidepdf.com/reader/full/cprogrammingla07day2 7/36

7Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/LA07/003

Version No. 1.0 

Lists - Queues

• Illustration of a Queue

10

30 20 10

20 10

30 20

queue with one element

20 added to the queue

30 added to the queue

10 removed from the queue

F r  on t  

R e ar 

F r  on t  

R e a

F r  on t  

F r  on t  

R e ar 

R e ar 

Page 8: CProgramming_LA07_Day2

7/30/2019 CProgramming_LA07_Day2

http://slidepdf.com/reader/full/cprogrammingla07day2 8/36

8Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/LA07/003

Version No. 1.0 

Command Line Arguments

• What are command line arguments?

• echo command to display “Hello World” in UNIX 

> echo Hello World

• The input to echo are command line arguments

– Hello World

Page 9: CProgramming_LA07_Day2

7/30/2019 CProgramming_LA07_Day2

http://slidepdf.com/reader/full/cprogrammingla07day2 9/36

9Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/LA07/003

Version No. 1.0 

Command Line Arguments… 

• In a C program the function that accepts command line arguments is

the main() function

• To write the main() function to accept command-line arguments the

syntax is:

main( int argc, char *argv[] )

{… 

Page 10: CProgramming_LA07_Day2

7/30/2019 CProgramming_LA07_Day2

http://slidepdf.com/reader/full/cprogrammingla07day2 10/36

10Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/LA07/003

Version No. 1.0 

Command Line Arguments… 

• argc is the number of elements on the command line i.e. the

command and the arguments

Example: > echo Hello World

– argc will have the value 3 for the command:

• if argc is 1, then there are no arguments only the command

• argv[] is an array of all the elements on the command lineExample: > echo Hello World

– argv[0] points to the string “echo” 

– argv[1] points to the string “Hello” 

– argv[2] points to the string “World” 

– argv[3] is NULL value

• Note: “echo” is the command and the rest are arguments 

Page 11: CProgramming_LA07_Day2

7/30/2019 CProgramming_LA07_Day2

http://slidepdf.com/reader/full/cprogrammingla07day2 11/36

11Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/LA07/003

Version No. 1.0 

Command Line Arguments… 

/* Program to echo arguments */

#include<stdio.h>

main(int argc, char *argv[])

{

/* Increment pointer to point to arguments*/

argv++;

/* Print the arguments on screen */

while (*argv != NULL)

{

printf("%s ", *argv);

argv++;}

printf("\n");

}

Page 12: CProgramming_LA07_Day2

7/30/2019 CProgramming_LA07_Day2

http://slidepdf.com/reader/full/cprogrammingla07day2 12/36

12Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/LA07/003

Version No. 1.0 

Need for File I/O

• Data that outlives the program

– needs to be stored– in a permanent storage

– so that it can be referred to later

• Example

– word processing application might save the text in a linked list or someother data structure when the application is running, but when the

application is closed the contents of the linked list need to stored in a

file

Page 13: CProgramming_LA07_Day2

7/30/2019 CProgramming_LA07_Day2

http://slidepdf.com/reader/full/cprogrammingla07day2 13/36

13Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/LA07/003

Version No. 1.0 

Need for File I/O

• C has several functions for I/O

– available as I/O library

• The functions in the I/O library

– Uses the corresponding I/O function’s of the underlying operating system 

Page 14: CProgramming_LA07_Day2

7/30/2019 CProgramming_LA07_Day2

http://slidepdf.com/reader/full/cprogrammingla07day2 14/36

14Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/LA07/003

Version No. 1.0 

Classification of File I/O functions

FILE I/O

Functions

High Level

I/O Functions

Low Level

I/O Functions

Text

I/O Functions

Binary

I/O Functions

Page 15: CProgramming_LA07_Day2

7/30/2019 CProgramming_LA07_Day2

http://slidepdf.com/reader/full/cprogrammingla07day2 15/36

15Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/LA07/003

Version No. 1.0 

Understanding Streams

• The C Language I/O system provides a consistent interface to theprogrammer independent of the actual I/O device used

• I/O streams provides a level of abstraction between the programmerand the I/O device

• Stream is a logical connection to a file, screen , keyboard and otherI/O devices

Page 16: CProgramming_LA07_Day2

7/30/2019 CProgramming_LA07_Day2

http://slidepdf.com/reader/full/cprogrammingla07day2 16/36

16Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/LA07/003

Version No. 1.0 

C Program

Input Stream

Disk File

Output Stream

Understanding Streams… 

Page 17: CProgramming_LA07_Day2

7/30/2019 CProgramming_LA07_Day2

http://slidepdf.com/reader/full/cprogrammingla07day2 17/36

17Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/LA07/003

Version No. 1.0 

• There are 2 types of streams

– Text stream (stream of ASCII characters)– Binary stream (stream of bytes)

• C has an inbuilt structure which represents a the logical stream

– FILE

– This structure has a buffer– a variable of the structure FILE has to be created for interacting with the

actual storage

• The FILE structure is nothing but a stream of characters/bytes which acts as a

logical representative of the actual file

Understanding Streams… 

Page 18: CProgramming_LA07_Day2

7/30/2019 CProgramming_LA07_Day2

http://slidepdf.com/reader/full/cprogrammingla07day2 18/36

18Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/LA07/003

Version No. 1.0 

File Opening Modes

Mode Description

“r” Searches file. If the file exists, loads it into memory and sets up a

pointer which points to the first character in it. If the file doesn’t 

exists it returns a null.

Operations possible - reading from the files

“w” Searches file. If the file exists, contents are overwritten. If the filedoesn’t exists a new file is created. Returns a null if unable to open

the file.

Operations possible - writing to the file.

“a” Searches file. If the file exists, loads it into memory and sets up a

pointer which points to the end of the file. If the file doesn’t exists anew file is created. Returns a null if unable to open the file.

Operations possible - appending new contents at the end of the file.

Page 19: CProgramming_LA07_Day2

7/30/2019 CProgramming_LA07_Day2

http://slidepdf.com/reader/full/cprogrammingla07day2 19/36

19Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/LA07/003

Version No. 1.0 

File Opening Modes… 

Mode Description

“r+” Same as “r” 

Operations possible – reading existing contents, writing new

contents, modifying existing contents of the file.

“w+” Same as “w” Operations possible – writing new contents, reading them back and

modifying existing contents of the file.

“a+” Same as “a” 

Operations possible – reading existing contents, appending newcontents to the end of file, cannot modify existing contents of the

file.

Page 20: CProgramming_LA07_Day2

7/30/2019 CProgramming_LA07_Day2

http://slidepdf.com/reader/full/cprogrammingla07day2 20/36

20Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/LA07/003

Version No. 1.0 

High Level I/O functions

• Functions to open a File.

– fopen() is the function to open a file

• Prototype: FILE *fopen (char *name, char *mode);

– first argument specifies the file name and

– second argument specifies a mode in which the file has to be opened

Page 21: CProgramming_LA07_Day2

7/30/2019 CProgramming_LA07_Day2

http://slidepdf.com/reader/full/cprogrammingla07day2 21/36

21Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/LA07/003

Version No. 1.0 

High Level I/O functions… 

• fopen()

– If the file is found at the specified location then

• fopen() creates a variable of type FILE structure and

• returns a pointer to it. This pointer is termed as a file pointer

– If the file could not be opened then

• fopen() returns a NULL

Page 22: CProgramming_LA07_Day2

7/30/2019 CProgramming_LA07_Day2

http://slidepdf.com/reader/full/cprogrammingla07day2 22/36

22Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/LA07/003

Version No. 1.0 

• Functions to close a File

– fclose() is the counterpart of fopen()

• Prototype: int fclose( FILE *stream ); – The argument to fclose() is the file pointer returned by fopen()

– fclose() returns

• 0 if the file was closed successfully

• otherwise it returns -1

High Level I/O functions… 

Page 23: CProgramming_LA07_Day2

7/30/2019 CProgramming_LA07_Day2

http://slidepdf.com/reader/full/cprogrammingla07day2 23/36

23Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/LA07/003

Version No. 1.0 

• Functions for reading characters.

– fgetc() is a function to read characters from a file

• Prototype: int fgetc( FILE *stream );

– The argument to fgetc() is the FILE pointer returned by fopen()

– fgetc() returns• the character read as an int or

• returns EOF (end of file) to indicate an error

• EOF is a macro which is #defined to -1

High Level I/O functions… 

Page 24: CProgramming_LA07_Day2

7/30/2019 CProgramming_LA07_Day2

http://slidepdf.com/reader/full/cprogrammingla07day2 24/36

24Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/LA07/003

Version No. 1.0 

• Functions for writing characters.

– fputc() is a function to write characters to a file

• Prototype: int fputc( int c , FILE *stream  );– The first argument to fputc() is the character to be written to the file

– The second argument to fputc() is the FILE pointer returned by fopen()

– fputc() returns

• the character written to the file

• It returns a EOF to indicate an error

High Level I/O functions… 

Page 25: CProgramming_LA07_Day2

7/30/2019 CProgramming_LA07_Day2

http://slidepdf.com/reader/full/cprogrammingla07day2 25/36

25Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/LA07/003

Version No. 1.0 

• Functions for reading lines

– fgets() is a function to read lines from a file

• Prototype: char *fgets( char *string, int n, FILE *stream );

– first argument is a character pointer which points to a buffer which will

receive the line read from the file

– second argument is the size of the buffer

– third argument to fgets() is the FILE pointer returned by fopen()

– fgets() returns

• a non negative number if successful

• NULL to indicate an error or end of file

High Level I/O functions… 

Page 26: CProgramming_LA07_Day2

7/30/2019 CProgramming_LA07_Day2

http://slidepdf.com/reader/full/cprogrammingla07day2 26/36

26Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/LA07/003

Version No. 1.0 

• Functions for writing lines

– fputs() is a function to write lines to a file

• Prototype: int fputs( const char *string, FILE *stream );

– first argument to fputc() is a character pointer which points to a buffer

which holds the string to be written to the file

– second argument to fputc() is the FILE pointer returned by fopen()

– fputs() returns

• a non negative number if successful

• It returns a EOF to indicate an error

High Level I/O functions… 

Page 27: CProgramming_LA07_Day2

7/30/2019 CProgramming_LA07_Day2

http://slidepdf.com/reader/full/cprogrammingla07day2 27/36

27Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/LA07/003

Version No. 1.0 

•Functions for reading blocks of data

– A block will have records of the same type.

• Example of record could be employee record which stores employee details likeemployee #, Name date of joining etc.

– fread() is a function to read blocks of data from a filePrototype: size_t fread( void *buffer, size_t size,

size_t count, FILE *stream );

• first argument is a void pointer, which points to a buffer that receives the blocks

read from the file• second argument is the size of each record

• third argument is the number of records to be read

• fourth argument is the FILE pointer returned by fopen()

– fread() returns

• the number of full items actually read

• maybe less than count if an error occurs or if the end of the file is encounteredbefore reaching count

High Level I/O functions… 

Page 28: CProgramming_LA07_Day2

7/30/2019 CProgramming_LA07_Day2

http://slidepdf.com/reader/full/cprogrammingla07day2 28/36

28Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/LA07/003

Version No. 1.0 

• Functions for writing blocks of data

– fwrite() is a function to write blocks of data to a file

– Prototype: size_t fwrite( const void *buffer, size_t size,

size_t count, FILE *stream );

• first argument is a void pointer to a constant, which points to a buffer that

has the blocks of data, to be written to the file

• second argument is the size of each record• third argument is the number of records to be written

• fourth argument is the FILE pointer returned by fopen()

– fwrite() returns

• the number of full items actually written• which may be less than count if an error occurs

High Level I/O functions… 

Page 29: CProgramming_LA07_Day2

7/30/2019 CProgramming_LA07_Day2

http://slidepdf.com/reader/full/cprogrammingla07day2 29/36

29Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/LA07/003

Version No. 1.0 

• High Level functions can be categorized as

– text mode

– binary mode

• so far we have seen the text mode functions.

• There are 3 main areas where text and binary mode files are differ

– Handling new lines– Representation of End of File

– Storage of numbers

High Level I/O functions… 

Page 30: CProgramming_LA07_Day2

7/30/2019 CProgramming_LA07_Day2

http://slidepdf.com/reader/full/cprogrammingla07day2 30/36

30Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/LA07/003

Version No. 1.0 

• Behavior of newlines if a file is opened

– In a text mode operation

• every newline character is converted

– to a carriage return – linefeed combination when it is written to the disk.

• Likewise the carriage return – linefeed combination

– is converted to a newline character when it is read from the disk

– In a binary mode operation the above mentioned conversions don’t takeplace

High Level I/O functions… 

Page 31: CProgramming_LA07_Day2

7/30/2019 CProgramming_LA07_Day2

http://slidepdf.com/reader/full/cprogrammingla07day2 31/36

31Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/LA07/003

Version No. 1.0 

• End of File detection for a file opened

– In a text mode operation

• a special character, whose ASCII value 26, is inserted after the last character

in the file to mark the end of file.

– In a binary mode operation there is no special character to indicate end

of file

– The binary mode files keep track of the end of file from the number of characters present in the directory entry of the file

– A file opened for writing in binary mode should be opened for reading in

binary mode only

High Level I/O functions… 

Page 32: CProgramming_LA07_Day2

7/30/2019 CProgramming_LA07_Day2

http://slidepdf.com/reader/full/cprogrammingla07day2 32/36

32Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/LA07/003Version No. 1.0 

• Storage of numbers for a file opened

– In text mode operation,

• integers & floats are stored as strings of characters

• Example:

– 12345 will occupy 5 bytes because individual digits are stored as characters

occupying one byte each

– In a binary mode operation• integers and floats are stored exactly in the same way as they are stored in

memory

• Example:

– integers will occupy 2 bytes and floats will occupy 4 bytes

High Level I/O functions… 

Page 33: CProgramming_LA07_Day2

7/30/2019 CProgramming_LA07_Day2

http://slidepdf.com/reader/full/cprogrammingla07day2 33/36

33Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/LA07/003Version No. 1.0 

• Functions for random access of file contents

– fseek() is a function to move the file pointer to a specified location

– Prototype: int fseek( FILE *stream , long offset , int origin );

• first argument is the FILE pointer returned by fopen()

• second argument specifies the number of offset bytes from the initial

position.

• third argument specifies the initial position. It can be one of the following:

– SEEK_CUR Current position of file pointer 

– SEEK_END End of file 

– SEEK_SET Beginning of file

– fseek() returns

• Zero if successful

• otherwise, it returns a nonzero value

– The value of offset can be a negative value also

High Level I/O functions… 

Page 34: CProgramming_LA07_Day2

7/30/2019 CProgramming_LA07_Day2

http://slidepdf.com/reader/full/cprogrammingla07day2 34/36

34Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/LA07/003Version No. 1.0 

• Function for returning the current position of the file pointer.

– ftell() is a function to get the current position of a file pointer

• Prototype: long ftell( FILE *stream );

– The argument to ftell() is the FILE pointer returned by fopen()

– On error, ftell() returns –1L

High Level I/O functions… 

Page 35: CProgramming_LA07_Day2

7/30/2019 CProgramming_LA07_Day2

http://slidepdf.com/reader/full/cprogrammingla07day2 35/36

35Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/LA07/003Version No. 1.0 

• Function for repositioning the file pointer to the beginning of a file.

– rewind() repositions the file pointer to the beginning of a file

• Prototype: void rewind( FILE *stream );

– The argument to rewind() is the FILE pointer returned by fopen()

High Level I/O functions… 

Page 36: CProgramming_LA07_Day2

7/30/2019 CProgramming_LA07_Day2

http://slidepdf.com/reader/full/cprogrammingla07day2 36/36

36Copyright © 2005 Infosys Technologies Ltd ER/CORP/CRS/LA07/003

Thank You!