Introduction to Programming - PowerPoint PPT Presentation

About This Presentation
Title:

Introduction to Programming

Description:

Introduction to Programming Lecture 17 String Handling String Manipulation Functions Character ASCII 1 byte = 8 bits Example 1 #include main ( ) { int i ... – PowerPoint PPT presentation

Number of Views:24
Avg rating:3.0/5.0
Slides: 34
Provided by: Umer4
Category:

less

Transcript and Presenter's Notes

Title: Introduction to Programming


1
Introduction to Programming
  • Lecture 17

2
  • String Handling

3
  • String Manipulation Functions

4
  • Character

5
  • ASCII

6
  • 1 byte 8 bits

7
Example 1
  • includeltiostream.hgt
  • main ( )
  • int i
  • char c
  • for( i 0 i lt 256 i )
  • c i
  • cout ltlt i ltlt \t ltlt c ltltendl

8
Header File
  • ctype.h
  • includeltctype.hgt

Storage Requirements for Text and Binary Modes
Storage Requirements for Text and Binary Modes
for an Integer
for an Integer
00110101
00110101
00111001
00111001
Page 32
Page 32
The fwrite() Function
The fwrite() Function
size_t fwrite (void ptr, size_t size, size_t
nmemb,
size_t fwrite (void ptr, size_t size, size_t
nmemb,
FILE fp)
FILE fp)
fwrite() writes, from the address pointed to by
ptr,
fwrite() writes, from the address pointed to by
ptr,
up to nmemb elements whose size is specified by
up to nmemb elements whose size is specified by
size, to the file pointed by fp
size, to the file pointed by fp
fwrite() returns the number of elements
fwrite() returns the number of elements
successfully written, which will be less than
nmemb
successfully written, which will be less than
nmemb
only if a write error is encountered.
only if a write error is encountered.
double earnings10
double earnings10
fwrite(earnings,sizeof(double),10,fp)
fwrite(earnings,sizeof(double),10,fp)
32
32
Page 33
Page 33
The fread() Function
The fread() Function
size_t fread(void ptr, size_t size, size_t
nmemb,
size_t fread(void ptr, size_t size, size_t
nmemb,
FILE fp)
FILE fp)
fread() reads, into the array pointed to by ptr,
up to
fread() reads, into the array pointed to by ptr,
up to
nmemb elements whose size is specified by size,
nmemb elements whose size is specified by size,
form the file pointed to by fp.
form the file pointed to by fp.
fread() returns the number of elements
fread() returns the number of elements
successfully read, which may be less than nmemb
successfully read, which may be less than nmemb
is a read error or end-of-file is encountered.
is a read error or end-of-file is encountered.
33
33
Page 34
Page 34
Writing file data using block I/O functions
Writing file data using block I/O functions
include ltstdio.hgt
include ltstdio.hgt
typedef struct
typedef struct
char name20
char name20
int serial_code
int serial_code
int amount
int amount
float
cost
float
cost
component
component
main(void)
main(void)
component comp_data
component comp_data
FILE
fp
FILE
fp
char
filename80
char
filename80
int numofcomp
int numofcomp
int i
int i
printf("Enter the file name ")
printf("Enter the file name ")
gets(filename)
gets(filename)
34
34
Page 35
Page 35
if ((fp fopen(filename, "wb")) NULL)
if ((fp fopen(filename, "wb")) NULL)
printf("can't open file \n")
printf("can't open file \n")
exit(1)
exit(1)
printf("Enter the number of components ")
printf("Enter the number of components ")
scanf("d", numofcomp)
scanf("d", numofcomp)
for (i0 iltnumofcomp i)
for (i0 iltnumofcomp i)
printf("Name of the component ")
printf("Name of the component ")
gets(comp_data.name)
gets(comp_data.name)
printf("Serial code of the component ")
printf("Serial code of the component ")
scanf("d", comp_data.serial_code)
scanf("d", comp_data.serial_code)
printf("Amount of the component ")
printf("Amount of the component ")
scanf("d", comp_data.amount)
scanf("d", comp_data.amount)
printf("Cost of each component ")
printf("Cost of each component ")
scanf("f", comp_data.cost)
scanf("f", comp_data.cost)
fwrite(comp_data, sizeof(comp_data), 1, fp)
fwrite(comp_data, sizeof(comp_data), 1, fp)
fclose(fp)
fclose(fp)
return 0
return 0


35
35
Page 36
Page 36
12.5 Random Access
12.5 Random Access
Random Access fseek() and ftell()
Random Access fseek() and ftell()
fseek, ftell All the previous I/O functions do
reading
fseek, ftell All the previous I/O functions do
reading
and writing sequentially, i.e. read the 1st
datum, 2rd
and writing sequentially, i.e. read the 1st
datum, 2rd
datum, 3rd datum ..., etc.
fseek
and
ftell
help doing
datum, 3rd datum ..., etc.
fseek
and
ftell
help doing
I/O in non-sequential manner, i.e. read the 10th
I/O in non-sequential manner, i.e. read the 10th
datum, then go back and read the 2nd datum.
datum, then go back and read the 2nd datum.
36
36
Page 37
Page 37
File position pointer
File position pointer
The system keeps a file position pointer for
The system keeps a file position pointer for
each open file.
each open file.
It indicates the location in the file at
It indicates the location in the file at
which data will be read or written.
which data will be read or written.
file position marker
file position marker
0 10 20 30 40 50 60 70 80 90
0 10 20 30 40 50 60 70 80 90
......
......
Disk
Disk
37
37
Page 38
Page 38
The fseek() Function
The fseek() Function
The function prototype of
fseek()
is
The function prototype of
fseek()
is
int fseek(FILE fp, long int offset, int mode)
int fseek(FILE fp, long int offset, int mode)

fseek()
returns
0
if OK, and
-1
if there is an error.

fseek()
returns
0
if OK, and
-1
if there is an error.

offset
tells how far to move from the starting point

offset
tells how far to move from the starting point
(depending on the mode). It can be ve (move
(depending on the mode). It can be ve (move
forward) or -ve (move backward) or 0.
forward) or -ve (move backward) or 0.

mode
identifies the starting point. In stdio.h, the

mode
identifies the starting point. In stdio.h, the
following constants can be assigned to mode
following constants can be assigned to mode
Measure offset from
Mode
Measure offset from
Mode
Beginning of file
SEEK_SET
Beginning of file
SEEK_SET
Current position
SEEK_CUR
Current position
SEEK_CUR
End of file
SEEK_END
End of file
SEEK_END
38
38
Page 39
Page 39
The ftell() Function
The ftell() Function
The function prototype of
ftell()
is
The function prototype of
ftell()
is
long int ftell(FILE fp)
long int ftell(FILE fp)
returns the current file location.
returns the current file location.
The rewind() Function
The rewind() Function
The function prototype of
rewind()
is
The function prototype of
rewind()
is
void rewind(FILE fp)
void rewind(FILE fp)
Resets the file position marker to the beginning
of
Resets the file position marker to the beginning
of
the file. This is equivalent to
the file. This is equivalent to
39
39
fseek(fp, 0L, SEEK_SET)
fseek(fp, 0L, SEEK_SET)
Page 40
Page 40
/ reverse.c - displays a file in reverse order
/
/ reverse.c - displays a file in reverse order
/
include ltstdio.hgt
include ltstdio.hgt
include ltstdlib.hgt
include ltstdlib.hgt
define CNTL_Z '\032'
/ eof marker in DOS textfiles /
define CNTL_Z '\032'
/ eof marker in DOS textfiles /
int main(int argc, char argv)
int main(int argc, char argv)
char ch
char ch
FILE fp
FILE fp
long count, last
long count, last
if ((fpfopen(argv1,"rb")) NULL)
if ((fpfopen(argv1,"rb")) NULL)
printf("reverse can't open s\n", argv1)
printf("reverse can't open s\n", argv1)
exit(1)

exit(1)

fseek(fp,0L, SEEK_END)
/ goto eof /
fseek(fp,0L, SEEK_END)
/ goto eof /
lastftell(fp)
lastftell(fp)
for (count1L countltlast count)
for (count1L countltlast count)
fseek(fp,-count,SEEK_END)
fseek(fp,-count,SEEK_END)
chgetc(fp)
chgetc(fp)
if (ch!CNTL_Z ch! '\r')
if (ch!CNTL_Z ch! '\r')
putchar(ch)
putchar(ch)
fclose(fp)
fclose(fp)
return(0)
return(0)
40
40


9
ctype Functions
int isspace ( int c ) int iscntrl ( int c ) int
ispunct ( int c ) int isprint ( int c ) int
isgraph ( int c )
  • int isdigit ( int c )
  • int isalpha ( int c )
  • int isalnum ( int c )
  • int isxdigit ( int c )
  • int islower ( int c )
  • int isupper ( int c )
  • int tolower ( int c )
  • int toupper ( int c )

10
isdigit ( ) Function
  • int isdigit ( int c )

11
isalpha ( ) Function
  • int isalpha ( int c )

12
isalnum ( ) Function
  • int isalnum ( int c )

13
islower ( ) Function
  • int islower ( int c )

14
isupper ( ) Function
  • int isupper ( int c )

15
tolower ( ) Function
  • int tolower ( int c )

16
toupper ( ) Function
  • int toupper ( int c )

17
  • getchar ( )

18
  • cout ltlt Please enter a character string then
    press enter
  • while ( ( c getchar ( ) ) ! \n )
  • if ( islower ( c ) )
  • lc
  • else if ( isupper ( c ) )
  • uc
  • else if (isdigit ( c ) )
  • dig
  • else if ( isspace ( c ) )
  • ws
  • else if ( ispunct ( c ) )
  • pun
  • else
  • oth

19
String Conversion Functions
  • double atof ( char str )
  • int atoi (char str )
  • long atol (char str )

20
atoi ( ) Function
  • char str
  • int age
  • age atoi ( str )

21
atof ( ) Function
  • 12.89

22
atof ( ) Function
  • char str
  • double dVar
  • dVar atof ( str )

23
  • int main (int agrc, char agrv )
  • argc stands for a count of the number of
    arguments
  • argv stands for a vector of arguments

24
  • String Functions

25

String Manipulation Functions

26
  • int sum
  • int sum_even
  • int sum_odd
  • myStrcpy ( )

27
String Manipulation Functions
  • char strcpy (char s1 , const char s2 )
  • char strncpy ( char s1 , char s2 , int n )
  • char strcat (char s1 , char s2 )
  • char strncat ( char s1 , char s2 , int n )

28
strcmp ( ) Function
  • int strcmp (const char s1 , const char s2 )

29
strncmp ( ) Function
  • int strncmp ( const char s1 , const char s2 ,
    int n )

30
strlen ( ) Function
  • int strlen ( const char s )

31
  • Search Functions

32
Search Functions
char strstr( co
nst char s1,
Locates the first occurrence in string
of string
. If the string is found, a pointer
s1
s2
const char s2 )

to the string in
is re
turned. Otherwise, a
pointer is returned.

s1
NULL
char strtok( char s1, const
A sequence of call
s to
breaks string
into tokens

logical pieces such
strtok
s1
char s2 )

as words in a line of text

separated by characters contained in string
. The first
s2
call contains
as the first argument, and subsequent calls to
continue tokenizing
s1
the same string contain
as the first argument. A pointer to the current
token is
NULL
returned by each call. If there are no more
tokens when the function is called,

NULL
is returned.


33
  • This is a test
  • wrong
  • right
  • NULL
Write a Comment
User Comments (0)
About PowerShow.com