A Digression - PowerPoint PPT Presentation

1 / 35
About This Presentation
Title:

A Digression

Description:

ASCII is what we use. American Standard Code for Information Interchange. 1 byte per character. ... numbers but we can use this fact in c' to do interesting ... – PowerPoint PPT presentation

Number of Views:24
Avg rating:3.0/5.0
Slides: 36
Provided by: morr3
Category:

less

Transcript and Presenter's Notes

Title: A Digression


1
A Digression
  • More about characters
  • File I/O (Input/Output)

2
How are characters stored in memory?
  • Each character is stored in one or two bytes.
  • Everyone agrees on a standard for how these
    characters are stored in memory.
  • Two most important standards are
  • 1) ASCII 1 byte
  • 2) UNICODE 2 bytes
  • There are others as well.

3
  • ASCII is what we use.
  • American Standard Code for Information
    Interchange.
  • 1 byte per character.
  • A is 01000001 a is 01100001
  • B is 01000010 b is 01100010
  • C is 01000011 c is 01100011
  • With 1 byte 8 bits you have 256 patterns so
    256 possible letters. In fact there are only 128.

4
  • 127 characters is plenty for the European
    languages and the characters include
  • Upper case
  • Lower case
  • Digits
  • Punctuation
  • Non- Printing Characters

5
  • Non-printing characters are ASCII characters
    that do things rather the print something.
    (These days they print as smiley faces or do
    something)
  • e.g.
  • ltcrgt causes carriage to go to start of line.
  • ltlfgt causes a move to next line
  • ltbellgt causes a bell to ring on the terminal
  • lteotgt end of transmission
  • We dont typically use most of them

6
  • But if you want other alphabets e.g Chinese,
    Japanese, Greek... etc. 127 characters is not
    enough.
  • Go to Unicode 2 bytes
  • Used in the Java programming language
  • There are 65536 patterns of 16 bits so plenty
    for all character sets.
  • We stick to ASCII

7
  • Because a byte is also a number each character
    has its associated integer.
  • A 01000001 65 blank 32
  • B 01000010 66 0 48
  • C 01000011 67 1 49
  • 2 50
  • a 01100001 97 . 46
  • b 01100010 98 / 47
  • c 01100011 99

8
  • We definitely do not need to remember these
    numbers but we can use this fact in c to do
    interesting things with characters.
  • consider the following logical expression
  • c gt a This means nothing
    really
  • BUT
  • Because each byte also has a number value
  • c gt a is true.

9
  • So
  • z gt q true
  • b gt d false
  • So this can be used to put things in
    alphabetical order and to do other things.

10
  • Suppose we read a character and we want to know
    if its a digit.
  • char character
  • if ( (character gt 0 ) (character lt 9 )
    )
  • printf ( character is a digit)

11
  • 1)Suppose we read a character and we want to know
    if its an upper case letter.
  • char character
  • if ( (character gt A ) (character lt Z )
    )
  • printf ( character is an upper case
    letter)
  • 2) Suppose we read a character and we want to
    know if its an lower case letter.
  • char character
  • if ( (character gt a ) (character lt z )
    )
  • printf ( character is a lower case
    letter)

12
  • 1)Suppose we read a character and we want to know
    if its a letter.
  • char character
  • if ( ((character gt A ) (character lt Z
    ))
  • ((character gt a ) (character lt
    z )) )
  • printf ( character is a letter)

13
  • We can now write two functions which convert
    all letters to upper case or all letters to lower
    case. (Actually it is already in a library).
  • char toupper(char letter)
  • if ( (letter gt 'A') (letter lt'Z' ) )
  • return(letter)
  • else
  • return( letter - ('a'-'A'))

14
  • A Function to convert to lower case
  • char tolower(char letter)
  • if ( (letter gt 'a') (letter lt'z' ) )
  • return(letter)
  • else
  • return( letter ('a'-'A'))

15
  • Actually these functions are already there for
    use.
  • Need to include ltctype.hgt
  • toupper( char c)
  • tolower(char c)
  • isdigit( char c)
  • And a whole bunch more.

16
  • Exercise Write a function to convert a
    character digit to its equivalent number
  • ASCII 0 is 48 we want a value of 0
  • ASCII 1 is 49 we want a value of 1
  • So subtract 48 or better since you dont have to
    remember subtract a

17
  • So a function to convert digits to numbers is
  • int ascii_to_int(char digit)
  • return( (int)(digit - '0') )

18
FILE I/O
  • When we have large amounts of numeric data or
    text its too difficult to type things in at the
    terminal. So put data into a file and have the
    program read from the file.
  • Similarly if you have a lot of output have the
    program write to a file.

19
  • C allows us to mix and match. We can input
    from different files as well as from the keyboard
    in the same program. The setup is all important.
  • Also when reading from files we dont prompt the
    user since there is no-one there to read the
    prompt

20
File Types
  • C has two main types
  • Binary Files
  • - Everything stored as 0s and 1s
  • Text Files
  • - Usually human readable characters
  • but any ASCII characters O.K.
  • - Each data line ends with newline char

21
File Storage Access
  • Files are stored in auxiliary storage devices,
  • Such as the hard drive, CDs, etc.
  • Read and Write access is buffered
  • A buffer is a storage area
  • It is temporary

22
Streams
  • In C each file is seen as a sequential stream
    of bytes. Depending on the nature of the file
    these bytes can be seen as characters or binary
    bytes. We will use character streams.For a
    stream of characters the file ends with an EOF
    marker.
  • bc fghj ltentgt qr.,stjltentgt tg. /?hyg
    ltEOFgt

23
How its done
  • Somehow the file is tied to or associated with
    your program in the same way the keyboard is tied
    to your program but we dont worry about how.
  • We do need to know the precise syntax which are
    the main elements for what has to be done.

24
To Access files there are a series of actions we
need to do.
  • We need to include ltstdio.hgt to get all the
    functionality we need.
  • We declare the file name as follows
  • FILE myfilepointer
  • 3) We open the file and check that the file is
    there.
  • 4) We can now input from or output to the file.
  • 5) We close the file.

25
Opening a File fopen
  • Usage
  • fopen("filename","mode")
  • Returns a pointer to FILE, a Physical file
  • Automatically creates buffer

26
(No Transcript)
27
Examples Opening files
  • FILE myfileptr
  • myfileptr fopen("TEMPS.DAT","w")
  • FILE myfileptr
  • myfileptr fopen("A\\TEMPS.DAT","w)
  • Returns NULL if there is a problem
  • if((myfileptr fopen("T.DAT","w")) NULL)

28
Closing a file fclose
  • Usage
  • fclose(myfileptr)

29
  • There are special functions for reading from
    and writing to files that are similar to ones we
    know.
  • Reading characters from files.
  • fgetc(fptr) gets a character from file
  • fputc(fptr) writes a character to a
    file
  • Other read functions
  • fscanf( ) we will look at this later
  • Fprintf( ) we will look at this later

30
  • We now put it all together to read from a file
    and output it to screen.
  • First we make a textfile using notepad and
    call it myfile.txt and then we first write a
    program to input the file and output it to the
    screen.

31
  • include ltstdio.hgt
  • int main(void)
  • FILE fptr
  • char character
  • if( (fptrfopen("d\\junk\\myfile.txt","r"))
    NULL) printf("\n file did not open")
  • while ((character fgetc(fptr)) !EOF)
  • printf("c",character)
  • fclose(fptr)
  • return 0

32
  • The output
  • The red fox jumped over the green dog
  • What did the green dog do?
  • Nothing at all.
  • Note that it reproduced the file with the
    correct spacing because it also inputs and
    outputs the \r

33
  • Note that the statement
  • while ((character fgetc(fptr)) !EOF)
  • printf("c",character)
  • Does the following
  • 1.It assigns the input to character.
  • 2.It returns the value that is input.
  • 3.It evaluates the expression is it equal to EOF

34
  • We now modify the code so that it counts the
    lines as well as outputs the file

35
  • FILE fptr
  • int count 1
  • char character
  • if( (fptr fopen("d\\junk\\myfile.txt","r"))
    NULL)
  • printf("\n file did not open")
  • else
  • while ((character fgetc(fptr)) !EOF)
  • printf("c",character)
  • if (character '\n')
    count
  • printf("\nnumber of lines is d ",
    count)
  • fclose(fptr)
Write a Comment
User Comments (0)
About PowerShow.com