working with sequential files ? ? ? ?. data representation ▪ text ▪ binary (data files) ...

16
Algorithms and programming techniques Working with sequential files ? ? ? ?

Upload: ellen-blair

Post on 18-Jan-2016

218 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Working with sequential files ? ? ? ?.  Data representation ▪ text ▪ binary (data files)  Records with the same/different structure (size) ▪ Same structure

Algorithms and programming techniques

Working with sequential files

? ??

?

Page 2: Working with sequential files ? ? ? ?.  Data representation ▪ text ▪ binary (data files)  Records with the same/different structure (size) ▪ Same structure

Data representation▪ text

▪ binary (data files)

Records with the same/different structure

(size) ▪ Same structure (same description) – parts with

variable structure

Record order▪ usually chronological order

▪ no retrieving information

Access▪ sequential

▪ direct (position, nr. of bytes)

Sequential files

Page 3: Working with sequential files ? ? ? ?.  Data representation ▪ text ▪ binary (data files)  Records with the same/different structure (size) ▪ Same structure

Create (usually with populate) Consult: where are the results (screen / text

file)?▪ 1 record (search)

▪ several records (selection)

▪ all records (integral)

Update▪ add (1 or more records)

▪ change (single record / selection / all

records)

▪ delete not defined (can be simulated)

Working with sequential files. General operations

Singurele operații pentru fișiere text

Only operations for text files

Page 4: Working with sequential files ? ? ? ?.  Data representation ▪ text ▪ binary (data files)  Records with the same/different structure (size) ▪ Same structure

CNP Full name

Year

Group

No. of subjec

ts

Grades

0 1 … 19

1..3 1..201..1

01..1

0 …1..1

0

char [14] char [30] int int unsigned char

unsigned char

Working with sequential files. (create) populate a file

Create and populate a binary data file with records or the following structure. The end of processing is marked in standard way.

Block diagram

Create Stud.datData.txt

1234567891234Ionescu Ion2 10351510 10 10 10 10 10 10 10 10 10 10 10 10 10 101234567891235Popescu Pop2 1035159 9 9 9 9 9 9 9 9 9 9 9 9 9 91234567891236Georgescu George1 1015100 0 0 0 0 0 0 0 0 0…

typedef struct { char CNP[14]; char nume[30]; int an; inf grupa; unsigned char nrd; unsigned char note[20]; } STUDENT;…STUDENT x;

Page 5: Working with sequential files ? ? ? ?.  Data representation ▪ text ▪ binary (data files)  Records with the same/different structure (size) ▪ Same structure

Working with sequential files. (create) populate a file

Structured diagram

Start

Open new file

!feof(stdin)

Close file

Stop

Read fields

CNP

Record

CNP

Da

Nu

First field of the record to be read (does not have

to be the first described)

Al the other fields of the record

Page 6: Working with sequential files ? ? ? ?.  Data representation ▪ text ▪ binary (data files)  Records with the same/different structure (size) ▪ Same structure

Working with sequential files.Search for 1 record

Compute the average grade for the students whose CNP are entered from the keyboard. The end of processing is marked in the standard way. (Sequential consulting, 1 record, may be repeated.)

Block diagramLIST Stud.dat

Page 7: Working with sequential files ? ? ? ?.  Data representation ▪ text ▪ binary (data files)  Records with the same/different structure (size) ▪ Same structure

Working with sequential files.Search for 1 record

Structured diagram

Start

Open file

!feof(stdin)

Close file

Stop

Process search CNP

search CNP

Da

Nu

search CNP

Page 8: Working with sequential files ? ? ? ?.  Data representation ▪ text ▪ binary (data files)  Records with the same/different structure (size) ▪ Same structure

Working with sequential files.Search for 1 record

Structured diagram

Process searched CNP

Go to start of file

!feof(f) && vb==0

Exit

Process record

Read rec.

Yes

No

Yes

No results

vb = 0

vb==0No Read rec.

Page 9: Working with sequential files ? ? ? ?.  Data representation ▪ text ▪ binary (data files)  Records with the same/different structure (size) ▪ Same structure

Working with sequential files.Search for 1 record

Structured diagram

Prelucrare articol

Ieșire

m = m + nota[i] i = 0,n-1

Da

nume, m

CNP == CNP căutat

Nu

m = 0

m = m/n

vb = 1

Page 10: Working with sequential files ? ? ? ?.  Data representation ▪ text ▪ binary (data files)  Records with the same/different structure (size) ▪ Same structure

Working with sequential files.Consulting, select records

For each group number entered from keyboard, list in a separate text file all the students belonging to that group. The end of processing is marked standard. (Sequential consulting, several records, selection on common value field – group number, may be repeated.)

Block diagramList Stud.dat

Page 11: Working with sequential files ? ? ? ?.  Data representation ▪ text ▪ binary (data files)  Records with the same/different structure (size) ▪ Same structure

Working with sequential files.Consulting, select records

Structured diagram

Start

Open file

!feof(stdin)

Close file

Stop

Process search gr.

search gr.

Yes

Nu

Search gr.

Page 12: Working with sequential files ? ? ? ?.  Data representation ▪ text ▪ binary (data files)  Records with the same/different structure (size) ▪ Same structure

Working with sequential files.Consulting, select records

Schema logică structurată

Process searched gr.

Go to start of file

!feof(f)

Exit

Process record

Read rec.

Yes

No

Yes

Error msg.

vb = 0

vb==0No

Open text file

Success msg.

Close text file

Delete text file

Read rec.

Page 13: Working with sequential files ? ? ? ?.  Data representation ▪ text ▪ binary (data files)  Records with the same/different structure (size) ▪ Same structure

Working with sequential files.Consulting, select records

Structured diagram Process record

Exit

Yes

Name, avg.Text file

x.gr == search gr.No

vb = 1Selecti

on conditi

on

Page 14: Working with sequential files ? ? ? ?.  Data representation ▪ text ▪ binary (data files)  Records with the same/different structure (size) ▪ Same structure

Working with sequential files

Integral consulting▪ Remove selection condition

Update▪ add record see populate, change position to end

of file

▪ change (single record / selection / all

records)▪ requires finding the record to be modified consulting

▪ when found: confirm (if necessary), change record in

memory, rewrite to file

▪ delete not defined (may be simulated)▪ copy all records (except the one(s) to be deleted) in a new

file

▪ delete original file and give its name to the new file

Page 15: Working with sequential files ? ? ? ?.  Data representation ▪ text ▪ binary (data files)  Records with the same/different structure (size) ▪ Same structure

Working with sequential files

Homework Subchapters 1, 2, 3 from files chapter in the problem

book ▪ Chapter 4 in 2012 edition

▪ Chapter 2 in 2015 edition

Page 16: Working with sequential files ? ? ? ?.  Data representation ▪ text ▪ binary (data files)  Records with the same/different structure (size) ▪ Same structure

Spor la învăţat!