• Tidak ada hasil yang ditemukan

Fundamentals of Programming

N/A
N/A
Protected

Academic year: 2024

Membagikan "Fundamentals of Programming"

Copied!
76
0
0

Teks penuh

(1)

Fundamentals of Programming

session 31

File Processing

(2)

Why files?

Program data (variables, arrays, structures, etc.) is stored in ram

removed once the program finishes

Secondary storage

Hard Disk

SSD

Flash disk

CD/DVD

:

(3)

Remember: Streams

A sequence of data (bytes)

processed sequentially

potentially unlimited

Stream vs. Batch data

user input, user output, files, I/O devices, etc.

(4)

Remember: Stream Processing

while (1) {

char c = getchar();

putchar(toupper(c));

}

423423234532asdfsadfasdfzxc...

423423234532ASDFSADFASDFZXC...

(5)

Remember: Standard streams

Standard Input

Standard Output

Standard Error

(6)

Remember: Standard streams

Standard Input

Standard Output

Standard Error

stdio.h

(7)

Files as streams

Standard Input

Standard Output

Standard Error

Open a file -> stream

Read from stream

Write to stream

stdio.h

(8)

Writing to streams

Open a file -> stream

Read from stream

Write to stream

(9)

Writing to streams

Open a file -> stream

Read from stream

Write to stream

Never use _IO_FILE!

(10)

Writing to streams

Open a file -> stream

Read from stream

Write to stream

(11)

Writing to streams

Open a file -> stream

Read from stream

Write to stream

(12)

Writing to files (File -> stream)

Open a file -> stream

Read from stream

Write to stream

(13)

Writing to files

(14)

Writing to files

(15)

Writing to files

(16)

Writing to files

(17)

Writing to files

(18)

Writing to files

(19)

Writing to files

(20)

Write to stream functions

standard output arbitrary stream

printf("format", …) fprintf(stream, "format", …)

putchar(c) fputc(c, stream)

putc(c, stream) puts("string")

(prints trailing '\n', but not '\0')

fputs("string", stream) (without trailing '\n' or '\0')

(21)

Appending to files

(22)

Appending to files

(23)

Appending to files

(24)

Appending to files

(25)

Reading a file

Salam

Salaaaaaaaaam!

Chetori?

Kako?

input1.txt

(26)

Reading a file

Salam

Salaaaaaaaaam!

Chetori?

Kako?

input1.txt

(27)

Reading a file

Salam

Salaaaaaaaaam!

Chetori?

Kako?

input1.txt

(28)

Reading a file

Salam

Salaaaaaaaaam!

Chetori?

Kako?

input1.txt

(29)

Reading a file

Salam

Salaaaaaaaaam!

Chetori?

Kako?

input1.txt

what's going on?

(30)

Reading a file

Salam

Salaaaaaaaaam!

Chetori?

Kako?

input1.txt

(31)

Reading a file

Salam

Salaaaaaaaaam!

Chetori?

Kako?

input1.txt

(32)

Reading a file

Salam

Salaaaaaaaaam!

Chetori?

Kako?

input1.txt

stdio.h

(33)

Reading a file

Salam

Salaaaaaaaaam!

Chetori?

Kako?

input1.txt

(34)

Reading a file

78.4781 53.8523 84.8712 83.4061 39.1451 53.3214 76.6731 46.5567 98.4300 70.6770 23.6938 88.3916 63.9619 69.9066 43.9198 83.0578 34.5258 82.6090 84.4959 34.9013 63.6365 41.0624 9.8199 30.3438 55.9582 19.7075 51.8794 28.2899

input2.txt

(35)

Reading a file

78.4781 53.8523 84.8712 83.4061 39.1451 53.3214 76.6731 46.5567 98.4300 70.6770 23.6938 88.3916 63.9619 69.9066 43.9198 83.0578 34.5258 82.6090 84.4959 34.9013 63.6365 41.0624 9.8199 30.3438 55.9582 19.7075 51.8794 28.2899

input2.txt

(36)

Reading a file

96324532 Amin Parchami 18.5 96234534 Behnam Beigi 17.9 96838459 Parham Parviz 18.9 96838222 Mahdi Forozan 19.99

input3.txt

(37)

Reading a file

96324532 Amin Parchami 18.5 96234534 Behnam Beigi 17.9 96838459 Parham Parviz 18.9 96838222 Mahdi Forozan 19.99

input3.txt

(38)

Reading a file

96324532 Amin Parchami 18.5 96234534 Behnam Beigi 17.9 96838459 Parham Parviz 18.9 96838222 Mahdi Forozan 19.99

input3.txt

(39)

Reading streams functions

standard input arbitrary stream

scanf("format", …) fscanf(stream, "format", …)

c = getchar() c = fgetc(stream)

c = getc(stream)

gets(s) fgets(s, size, stream)

(40)

Write you own cat

(41)

Write you own cat

(42)

Write you own cat

(43)

Write you own cat

(44)

Write you own cat

why int instead of char?

(45)

Write you own cat

(46)

Write you own cat

(47)

Using feof

(48)

EOF in stdin

linux: Ctrl+d windows: Ctrl+z

(49)

rewinding a file stream

(50)

rewinding a file stream

(51)

using fseek

(52)

using fseek

(53)

using fseek

(54)

using fseek

fseek(stream,10, SEEK_SET); sets position indicator to 10 (from the beginning of the file)

fseek(stream,-20, SEEK_END); sets position indicator to 20 bytes to the end of the file (end of file - 20)

fseek(stream,30, SEEK_CUR); increase position indicator by 30 (current position + 30)

fseek(stream,-5, SEEK_CUR); decrease position indicator by 5 (current position - 5)

stdio.h

(55)

using ftell

(56)

using ftell

(57)

fopen modes

r Opens file for reading. File must exist.

w Creates an empty file for writing.

a Opens file for appending. File created if not exist.

r+ Opens files for reading and writing. File must exist.

w+ Creates an empty file for reading and writing.

a+ Opens file for reading and appending.

https://www.tutorialspoint.com/c_standard_library/c_function_fopen.htm

(58)

Updating records

96324532 Amin Parchami 18.5 96234534 Behnam Beigi 17.9 96838459 Parham Parviz 18.9 96838222 Mahdi Forozan 19.99

input.txt

How to

go to n-th record?

change a record?

(59)

Fixed-sized records

https://ascii.cl/

12

{'1', '2'} = {49, 50} (2 bytes)

00001100 (1 byte) 228

{'2', '2', '8'} = {50,50,56} (3 bytes)

11100100 (1 byte)

(60)

Fixed-sized records

https://ascii.cl/

12

{'1', '2'} = {49, 50} (2 bytes)

00001100 (1 byte) 228

{'2', '2', '8'} = {50,50,56} (3 bytes)

11100100 (1 byte) fprintf(stream, "%d", 12);

fprintf(stream, "%d", 228);

49 50

49 50 56

(61)

Fixed-sized records

https://ascii.cl/

12

{'1', '2'} = {49, 50} (2 bytes)

00001100 (1 byte) 228

{'2', '2', '8'} = {50,50,56} (3 bytes)

11100100 (1 byte) fprintf(stream, "%d", 12);

fprintf(stream, "%d", 228);

49 50

49 50 56

12

228

(62)

Using fwrite: directly writing bytes to file

12

{'1', '2'} = {49, 50} (2 bytes)

00001100 (1 byte) 228

{'2', '2', '8'} = {50,50,56} (3 bytes)

11100100 (1 byte)

fprintf(stream, "%d", 12); char c = 12; fwrite(&c, 1, 1, stream);

fprintf(stream, "%d", 228); char c = 228; fwrite(&c, 1, 1, stream);

49 50

49 50 56

12

228

(63)

Using fwrite: directly writing bytes to file

12

{'1', '2'} = {49, 50} (2 bytes)

00001100 (1 byte) 228

{'2', '2', '8'} = {50,50,56} (3 bytes)

11100100 (1 byte)

fprintf(stream, "%d", 12); char c = 12; fwrite(&c, 1, 1, stream);

fprintf(stream, "%d", 228); char c = 228; fwrite(&c, 1, 1, stream);

49 50

49 50 56

12

228

size_t fwrite(const void *ptr, size_t size, size_t nmemb, FILE *stream);

starting address

element size number of

elements

(64)

Writing an array to file

(65)

Writing an array to file

(66)

Writing an array to file

(67)

Writing an array to file

(68)

Remember: struct Student

(69)

Writing records to a file

(70)

Read/Write n-th record in a file

(71)

Read/Write n-th record in a file

(72)

Read/Write n-th record in a file

(73)

Changing student's name of n-th record

(74)

Changing student's name of n-th record

(75)

Record Key

(76)

Look at example from the book!

Referensi

Dokumen terkait

Harmer (2003) expresses that the teachers have a responsibility to manage the students from the beginning of the activity till the end through three stages: before

Television sets continue to increase in size, evolving into home theater equipment on one end of the spectrum and at the other end you can find television sets that are small enough

The approach is based on the conviction that when organized, communities have sufficient control over forested land and are equipped with the right skill sets to decide and

12de, Hosea sets the parable aside to expound in plain language the significance of the moral injunction: “It is time to seek Yhwh until he comes and showers ṣeḏeq on you” ~kl qdc hryw

Via the literature and an industry based survey we seek to identify skill sets and the alignment between the perceived importance of these skills and their knowledge level..

These communication mediums include the following technologies:  Wi-Fi  ZigBee  Bluetooth  Radio Frequency  Satellites  Ethernet LANs  CAN Bus  2G/3G/4G/5G Internet 

Beginning and end of downstream less than the allowable limit 0.5 mg/L, and the highest value at the site of beginning of downstream 0.34 mg/L in November while at the site of end of

– Inversion : • The solution is established by merely adding The solution is established by merely adding a δ - function of surface charge to the solution existing at the end of