Text and binary file processing
This presentation is the property of its rightful owner.
Sponsored Links
1 / 24

Text and Binary File Processing PowerPoint PPT Presentation


  • 140 Views
  • Uploaded on
  • Presentation posted in: General

Text and Binary File Processing. 程式設計 潘仁義 CCU COMM. User Program. C Library. OS File System. File. C File I/O Overview(1/3). C File I/O Overview(3/3). - stdio.h 與檔案相關之函式 -. 12.1 Input and Output Files, Review. text file a named collection of characters saved in secondary storage

Download Presentation

Text and Binary File Processing

An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -

Presentation Transcript


Text and binary file processing

Text and Binary File Processing

程式設計

潘仁義

CCU COMM


C file i o overview 1 3

User Program

C Library

OS File System

File

C File I/O Overview(1/3)


C file i o overview 3 3

C File I/O Overview(3/3)

- stdio.h 與檔案相關之函式-


12 1 input and output files review

12.1 Input and Output Files, Review

  • text file

    • a named collection of characters saved in secondary storage

  • input (output) stream

    • continuous stream of character codes representing textual input (or output) data

  • (FILE *) stdin

    • system file pointer for keyboard’s input stream

  • (FILE *) stdout, stderr

    • system file pointers for screen’s output stream


Table 12 2 placeholders for printf format strings

TABLE 12.2Placeholders for printf Format Strings


Table 12 1 meanings of common escape sequences

TABLE 12.1 Meanings of Common Escape Sequences


Table 12 4 comparison of i o with standard files and i o with user defined file pointers

TABLE 12.4 Comparison of I/O with Standard Files and I/O with User-Defined File Pointers


Figure 12 1 program to make a backup copy of a text file

Figure 12.1 Program to Make a Backup Copy of a Text File


Figure 12 1 program to make a backup copy of a text file cont d

Figure 12.1 Program to Make a Backup Copy of a Text File (cont’d)

feof( inp)


Figure 12 2 input and output streams for file backup program

Figure 12.2 Input and Output Streams for File Backup Program


File open mode

File Open Mode


12 2 binary files

12.2 Binary Files

  • Formatted Text files

    • contain variable length records

    • must be accessed sequentially, processing all records from the start of file to access a particular record

  • Binary Files (random access file)

    • a file containing binary numbers that are the computer’s internal representation of each file component

    • contain fixed length records

    • can be accessed directly, directly accessing the record that is required

  • Binary files are appropriate for online transaction processing systems,

    • e.g. airline reservation, order processing, banking systems,

  • sizeof

    • operator that finds the number of bytes used for storage of a data type


The data hierarchy

The Data Hierarchy

  • Bit - smallest data item

    • Value of 0 or 1

  • Byte – 8 bits

    • Used to store a character

      • Decimal digits, letters, and special symbols

  • Field - group of characters conveying meaning

    • Example: your name

  • Record– group of related fields

    • Represented astruct or a class

    • Example: In a payroll system, a record for a particular employee that contained his/her identification number, name, address, etc.

  • File– group of related records

    • Example: payroll file

  • Database – group of related files


In a random access file

In a Random Access File …

  • Data

    • Data unformatted (stored as "raw bytes") in random access files

      • All data of the same type (ints, for example) use the same memory

      • All records of the same type have a fixed length

      • Data not human readable


Random access

Random Access

  • Access individual records without searching through other records

  • Instant access to records in a file

  • Data can be inserted without destroying other data

  • Data previously stored can be updated or deleted without overwriting.

  • Implemented using fixed length records

    • Sequential files do not have fixed length records


Random access a file fread

Random Access a File -- fread ()

  • fread --Transfer bytes from a file to a location in memory

  • Function fread requires four arguments

    • ret = fread(buffer, size, num, myptr);

      • the number of objects read

    • buffer: Address of first memory cell to fill

    • size: Size of one value

    • num: Maximum number of elements to copy from the file into memory

    • myptr: File pointer to a binary file opened in mode “rb” using function fopen

How to distinguish error and EOF?


Random access a file fwrite

Random Access a File – fwrite()

  • fwrite - Transfer bytes from a location in memory to a file

  • fwrite( &number, sizeof( int ), 1, myPtr );

    • &number - Location to transfer bytes from

    • sizeof( int ) - Number of bytes to transfer

    • 1 - For arrays, number of elements to transfer

      • In this case, "one element" of an array is being transferred

    • myPtr - File to transfer to or from


Random access a file fwrite ii

Random Access a File – fwrite() (II)

  • Writing structs

    fwrite( &myObject, sizeof (struct myStruct), 1, myPtr );

    • sizeof - Returns size in bytes of object in parentheses

  • To write several array elements

    • Pointer to array as first argument

    • Number of elements to write as third argument


Figure 12 3 creating a binary file of integers

Figure 12.3 Creating a Binary File of Integers


Access data randomly in a random access file

目前存取位置

位移後

file

offset

origin:

SEEK_SET

SEEK_CUR

SEEK_END

Access Data Randomly in a Random Access File

  • fseek

    • Sets file position pointer to a specific position

    • fseek( myPtr, offset, symbolic_constant);

      • myPtr- pointer to file

      • offset - file position pointer (0 is first location)

      • symbolic_constant - specifies where in file we are reading from

      • SEEK_SET - seek starts at beginning of file

      • SEEK_CUR - seek starts at current location in file

      • SEEK_END - seek starts at end of file

  • ftell

    • Return the current position in a stream

    • ftell( myptr)

      • myPtr- pointer to file


12 3 searching a database

12.3 SEARCHING A DATABASE

  • database

    • a vast electronic file of information that can be quickly searched using subject headings or keywords

  • 本節請自修, 寫作業時可參考


12 4 common programming errors 1 2

12.4 COMMON PROGRAMMING ERRORS(1/2)

  • Remember to declare a file pointer variable (type FILE *) for each file you want to process

  • fscanf, fprintf, getc and putc must be used for text I/O only

  • fread andfwrite are applied exclusively to binary files

  • fscanf, fprintfand getc take the file pointer as their first argument

  • putc, fread and fwrite take the file pointer as their last argument


12 4 common programming errors 2 2

12.4 COMMON PROGRAMMING ERRORS (2/2)

  • Opening a file for output by calling fopen with a second argument of “w” or “wb” typically results in a loss of any existing file whose name matches the first argument

  • Binary files cannot be created, viewed, or modified using an editor or word processor program

  • Depending on OS, a read cannot follow a write, a write cannot follow a read, except using a feek(SEEK_CUR, 0) between them


  • Login