File Input/Output, Character Encodings, Binary Input/Output

(ファイル入出力、文字コード、バイナリ入出力)

Computing Practice I

12th lecture, June 28, 2018

http://www.sw.it.aoyama.ac.jp/2018/CP1/lecture12.html

Martin J. Dürst

AGU

© 2005-18 Martin J. Dürst 青山学院大学

Today's Schedule

 

ミニテスト

 

Summary of Last Lecture

 

Results of Previous Exercises

11A1 11C1 11C2 11C3
100 points 99 50 0 2
60 points 1 50 44 46
partial - - - 30
errors - - 30 4
not submitted - - 26 18

 

About 11C2

Recursive creation of permutations

 

About 11C3

 

Importance of Q&A Forum

 

About the Standard Library

 

File Input/Output

 

Standard Input and File Input

source standard input (stdin) arbitrary file FILE * f
preparation (not needed) f = fopen(name,"r")
one-character input getchar() getc(f) or fgetc(f)
one-line input gets(s) fgets(s,l,stdin) fgets(s,l,f)
formatted input scanf(format,...) %s fscanf(f,format,...) %s
cleanup (not needed) fclose(f)

 

Standard Output and File Output

destination standard output (stdout) arbirtary file FILE * f
preparation (not needed) f = fopen(name,"w")
one-character output putchar(c) putc(c,f) or fputc(c,f)
one-line output puts(s) fputs(s,f)
formatted output printf(format,...) fprintf(f,format,...)
cleanup (not needed) fclose(f)

 

Additional Convenient Functions

 

Standard Error Output (stderr)

 

Overview of Character Encoding

Examples of Character Encoding
Character A o y
Shift_JIS 8E 52   90 C2   41   6F   79  
UTF-8 E9 9D 92 E5 B1 B1 41   6F   79  
UTF-16 (BE) 97 52   5C 71   00 41 00 6F 00 79

 

Japanese Legacy Character Encodings

 

World-Wide Character Encodings

Various names: Unicode/ISO 10646/JIS X 0221/...

Encodings for Unicode:

 

Character Encodings during Compilation and Execution

How to indicate character encodings for gcc:

Encoding used for display:

 

Binary Input/Output

 

Text Output and Binary Output


text binary
integers conversion to decimal representation
(number of digits depends on the size of the number)

directly
(if internally, int uses 4 bytes, then 4 bytes)

strings up to (but not including) the first '\0' fixed length
(may include final '\0' and garbage)

Example: fwrite.c

 

How to Check Files

Text files:

Binary files:

 

The fread and fwrite Functions

 

The fseek Function

 

Q&A フォーラムの題名

 

今日の演習

 

次回の準備

 

Glossary

standard library
標準ライブラリ
standard error output
標準エラー出力
character encoding
文字コード
legacy
従来からの、古い
compatible
互換である
compatibility
互換性
default
既定値
rewind
巻き戻し
offset
ゲタ