(ファイル入出力、文字コード、バイナリ入出力)
http://www.sw.it.aoyama.ac.jp/2017/CP1/lecture12.html
© 2005-17 Martin J. Dürst 青山学院大学
11A1 | 11C1 | 11C2 | 11C3 | |
100 points | 95 | 63 | 5 | 6 |
60 points | 2 | 34 | 66 | 74 |
partial | - | - | - | 11 |
errors | - | - | 20 | 2 |
not submitted | 2 | 2 | 8 | 6 |
http://
to
https://
http://
will automatically be redirected to
https://
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) |
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) |
fflush(f)
: Flushing of output bufferprintf
for debugging)sprintf(s,format,...)
: Formatted "output" to a string
(buffer) in memory, may be dangeroussscanf(s,format,...)
: Formatted "input" from a string
(buffer) in memorystderr
)stdin
and stdout
,
stderr
is always availablestderr
, the file output functions have to be
usedstderr
is not buffered (so fflush
is not
necessary)malloc
pattern, stderr
should be
usedexit
, include stdlib.h
if (!(file=fopen("myfile.txt", "w"))) {
fprintf (stderr, "Cannot open file myfile.txt for writing.\n");
exit(1);
}
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 |
iso-2022-jp
, used for E-mail)Shift_JIS
、Windows PC, MacIntosh)CP932
is a variant of SJIS)EUC-JP
、Unix/Linux)Various names: Unicode/ISO 10646/JIS X 0221/...
Encodings for Unicode:
UTF-8
: Compatible with US-ASCII, widely used on Internet/Web
and internally in Ruby, Perl, Go,...UTF-16
: Almost all characters use 16 bits, used internally
in Windows, Java, JavaScript,...UTF-32
: All characters use 32 bits, rarely usedHow to indicate character encodings for gcc
:
-finput-charset=encoding
: Source encoding-fexec-charset=encoding
: Execution encodingEncoding used for display:
CP932
on Japanese MS Windows systems, may be changed with
chcp
command)fopen
: "rb"
, "wb"
,
"r+b"
,... (see p. 318)fseek
functionfwrite
for output, fread
for inputtext | binary | |
---|---|---|
integers | conversion to decimal representation (number of digits depends on the size of the number) |
directly |
strings | up to (but not including) the first '\0' |
fixed length (may include final '\0' and garbage) |
Example: fwrite.c
Text files:
cat file.txt
Binary files:
od -hc raw.bin
or hexdump
file.bin
fread
and fwrite
Functionsvoid *
: pointer to data/spacesize_t
(close to int
): Size of one record
(element, structure) in bytessize_t
: Number of recordsFILE*
: File pointerfseek
or
fflush
fwrite (students, sizeof(Student), studentCount,
rawFile);
fseek
FunctionFILE*
: File pointerlong
: Offset (in bytes)int
: starting point for offset:
SEEK_SET
: from start of fileSEEK_CUR
: from current position (negative means
backwards)SEEK_END
: from end of file (0 or negative)0
in case of success, otherwise
EOF