Text and Binary File Processing - PowerPoint PPT Presentation

1 / 16
About This Presentation
Title:

Text and Binary File Processing

Description:

We will explore the use of standard input, standard output, and program-controlled text files. ... Precision. 20061213 cha12. 16. A Negative Side of Binary files ... – PowerPoint PPT presentation

Number of Views:363
Avg rating:3.0/5.0
Slides: 17
Provided by: hhl5
Category:

less

Transcript and Presenter's Notes

Title: Text and Binary File Processing


1
Chapter 12
  • Text and Binary File Processing
  • (Hanly)

2
Objectives
  • We will explore the use of standard input,
    standard output, and program-controlled text
    files.
  • We will introduce binary files.
  • We will compare the advantages and disadvantages
    of text and binary files.

3
Input/Output Files Review and Further Study
  • Text file a named collection of characters saved
    in secondary storage (e.g. on a disk).
  • To mark the end of a text file a special
    end-of-file character. lteofgt
  • Pressing the ltreturngt or ltentergt key causes the
    newline character to be placed in the file.
    (\n).
  • This is a text file!ltnewlinegt
  • It has two lines.ltnewlinegtlteofgt
  • This is a text file!ltnewlinegtIt has two
    lines.ltnewlinegtlteofgt

4
The Keyboard Screen as Text Streams
  • Input (output) steam continuous stream of
    character codes representing textual input (or
    output) data.
  • stdin system file pointer for keyboards input
    stream.
  • stdout, stderr system file pointers for
    screens output system.
  • All these streams can be treated like text files
    because their individual components are
    characters.

5
Escape Sequences
  • \n new line
  • \t tab
  • \f form feed (new page) ??
  • \r return (go back to column 1 of current
    output line)
  • \b backspace
  • Demo

6
Formatting Output with printf
7
(No Transcript)
8
File Pointer Variables
  • Chap 2
  • FILE infilep
  • FILE outfilep
  • infile fopen(data.txt,r)
  • outfile fopen(results.txt,w)
  • Unsuccessful due to the nonexistence of a file.
  • if (infilep NULL)
  • printf(Cannot open data.txt for input)
  • Closing a File fclose(infilep)

9
I/O with Standard Files and with User-Defined
File Pointer
10
Demo Program to Make a Backup Copy of a Text
File(demo)
11
(No Transcript)
12
Input and Output Streams for File Backup Program
13
Binary Files
  • Files containing binary numbers that are the
    computers internal representation of each file
    component.
  • Creating a Binary File of Integers

14
  • binaryp fopen(nums.bin,wb)
  • wb write binary
  • rb read binary
  • fwrite(i, sizeof(int), 1, binaryp)
  • i the address of the first memory cell whose
    contents are copied to the file.
  • sizeof(int) the number of bytes to copy to the
    file for one component. sizeof can be applied to
    both bulit-in and user-defined types.
  • 1 the number of values to write to the binary
    file.

15
Advantages of Binary files
  • Assume that two bytes are used store an int
    value.
  • 244 (1286432164)
  • 11110100
  • In text files,
  • Write 11110100 ? 2 4 4 and blank
  • Read 244 ? 11110100
  • It takes more time.
  • It takes more space. (Four bytes versus two)
  • Precision

16
A Negative Side of Binary files
  • A binary file created on one computer is rarely
    readable on another type of computers.
  • A binary file can not be created or modified in a
    word processor.
  • fread ?? fwrite
  • fscanf ?? fprintf
Write a Comment
User Comments (0)
About PowerShow.com