fopen rt read text rb read binary r
fopenのモード 例: "rt": read text "rb": read binary "r": read text(省略時はtext) "wt": write text "ab": append binary "r+b": read+ binary D-5
文字を読み込み,表示する プログラム(バイナリモード) #include <stdio. h> "rb"の意味は後述 void main(){ FILE *fp; int ch; fp = fopen("S: \a. txt", "rb"); if( fp == NULL ){ (略) } while( (ch=fgetc(fp)) != -1 ){ printf("%dn", ch); } fclose(fp); } D-12
文字を読み込み,表示する プログラム(バイナリモード) #include <stdio. h> "rb"の意味は後述 void main(){ Visual Studio版 FILE *fp; int ch, ret; ret = fopen_s( &fp, "s: \a. txt", "rb"); if( ret != 0 ) { (略) } while( (ch=fgetc(fp)) != -1 ){ printf("%dn", ch); } fclose(fp); } D-13
文字を読み込み,表示する プログラム(テキストモード) #include <stdio. h> void main(){ FILE *fp; int ch, ret; ret = fopen_s( &fp, "s: \a. txt", "rt"); if( ret != 0 ) { (略) } while( (ch=fgetc(fp)) != -1 ){ printf("%dn", ch); } fclose(fp); } D-16
文字を書き込むプログラム (テキストモード, Windows) #include "stdafx. h" Windowsにおける #include <stdio. h> 実行結果 void main() { a. txt FILE *fp; int ch, ret; ret = fopen_s( &fp, "s: \a. txt", "wt"); if (ret != 0 ) { (略) } fprintf( fp, "ABCn. DEF"); textモードでは, fclose(fp); n → r n } 10 → 13 10 に変換されて 書き込まれる. 65 66 67 13 10 68 69 70 D-18
文字を書き込むプログラム (バイナリモード) #include "stdafx. h" #include <stdio. h> void main() { FILE *fp; int ch, ret; ret = fopen_s( &fp, "s: \a. txt", "wb"); if (ret != 0 ) { (略) } fprintf( fp, "ABCn. DEF"); binaryモードでは, fclose(fp); n → r n } 10 → 13 10 の変換をしない. a. txt 65 66 67 10 68 69 70 D-19
文字を書き込むプログラム (テキストモード, Linux) #include <stdio. h> void main() { FILE *fp; int ch, ret; fp = fopen( "a. txt", "wt"); if( fp == NULL ){ (略) } fprintf( fp, "ABCn. DEF"); fclose(fp); } Linuxにおける 実行結果 a. txt 65 Linuxでは, 改行コードは n(10)なので, 変換なし. 66 67 10 68 69 70 D-20
高水準関数 fputs(char *s, FILE *fp); fpのファイルに,文字列sを書き込む. void main() { FILE *fp; int ret; ret = fopen_s( &fp, "s: \a. txt", "wt"); if( ret != 0 ){ (略) } fputs( "ABCn. DEF", fp); fclose(fp); } D-22
高水準関数 fgets(char *buf, int n, FILE *fp); fpのファイルから,bufに対して, n文字読み込む(n-1個の文字と, 1個の終端記号) void main() { char buf[100]; FILE *fp; int ret; ret = fopen_s(&fp, "s: \a. txt", "rt"); if( ret != 0 ){ (略) } fgets(buf, 100, fp); printf("%sn", buf); fclose(fp); } D-23
高水準関数 fprintf(FILE *fp, char s, . . . ); fpのファイルに,文字列sを書き込む. void main() { FILE *fp; int ret; ret = fopen_s(&fp, "s: \a. txt", "wt"); if( ret != 0 ){ (略) } fprintf(fp, "ret=%dn", ret); fclose(fp); } D-24
高水準関数 fscanf void main() { FILE *fp; int ret, hp, mp, exp, gold; fp = fopen( "S: \a. txt", "rb"); if ( fp == NULL ){ (略) } fscanf(fp, "HP=%dn", &hp); fscanf(fp, "MP=%dn", &mp); fscanf(fp, "EXP=%dn", &exp); fscanf(fp, "GOLD=%dn", &gold); fclose(fp); printf("%d %dn", hp, mp, exp, gold); } S: a. txt HP=23 MP=18 EXP=100 GOLD=99 実行結果 23 18 100 99 D-27
高水準関数 fscanf_s void main() { FILE *fp; int ret, hp, mp, exp, gold; ret = fopen_s(&fp, "s: \a. txt", "rb"); if (ret != 0) { (略) } fscanf_s(fp, "HP=%dn", &hp); fscanf_s(fp, "MP=%dn", &mp); fscanf_s(fp, "EXP=%dn", &exp); fscanf_s(fp, "GOLD=%dn", &gold); fclose(fp); printf("%d %dn", hp, mp, exp, gold); } S: a. txt HP=23 MP=18 EXP=100 GOLD=99 実行結果 23 18 100 99 D-28
高水準関数 fscanf_s void main() { FILE *fp; int ret, hp=0, mp=0, exp=0, gold=0; ret = fopen_s(&fp, "s: \a. txt", "rb"); if (ret != 0) { (略) } ret = fscanf_s(fp, "HP=%dn", &hp); printf("ret=%dn", ret); ret = fscanf_s(fp, "MP=%dn", &mp); printf("ret=%dn", ret); ret = fscanf_s(fp, "EXP=%dn", &exp); printf("ret=%dn", ret); ret = fscanf_s(fp, "GOLD=%dn", &gold); printf("ret=%dn", ret); fclose(fp); printf("%d %dn", hp, mp, exp, gold); } S: a. txt HP=23 MP=18 EXP=100 GOLD=99 実行結果 ret=1 23 18 100 99 D-29
高水準関数 fscanf_s void main() { FILE *fp; int ret, hp=0, mp=0, exp=0, gold=0; ret = fopen_s(&fp, "s: \a. txt", "rb"); if (ret != 0) { (略) } ret = fscanf_s(fp, "HP=%dn", &hp); printf("ret=%dn", ret); ret = fscanf_s(fp, "MP=%dn", &mp); printf("ret=%dn", ret); ret = fscanf_s(fp, "EXP=%dn", &exp); printf("ret=%dn", ret); ret = fscanf_s(fp, "GOLD=%dn", &gold); printf("ret=%dn", ret); fclose(fp); printf("%d %dn", hp, mp, exp, gold); } S: a. txt HP=23 EXP=100 MP=18 GOLD=99 実行結果 ret=1 ret=0 23 0 100 0 D-30
#include "stdafx. h" PNGファイルは, 16バイト目からの 4バイトに #include <stdio. h> 画像の横幅が, void main() { 20バイト目からの 4バイトに FILE *fp; int ret; 縦の長さが記録されている. unsigned int width, height; <<はビットシフト unsigned char buf[8]; //8バイトの配列 ret = fopen_s(&fp, "s: \a. png", "rb"); if (ret != 0) { (略) } fseek(fp, 16, SEEK_SET); //ファイルの 16バイト目に移動 fread(buf, 4, 2, fp); //その場所から, 4*2=8バイト読み込み fclose(fp); printf("%02 x %0 x 2 %02 xn", buf[0], buf[1], …); printf("%02 x %0 x 2 %02 xn", buf[4], buf[5], …); width = (buf[0]<<24 U) + (buf[1] << 16 U) + (buf[2] << 8 U) + (buf[3]); height = (buf[4] << 24 U) + (buf[5] << 16 U) + (buf[6] << 8 U) + (buf[7]); printf("%u * %un", width, height); D-32 }
- Slides: 32