Skip to content

Instantly share code, notes, and snippets.

@Nyarll
Created July 26, 2018 06:15
Show Gist options
  • Save Nyarll/7e381a367c5b254ae83f6a7ffe8ea280 to your computer and use it in GitHub Desktop.
Save Nyarll/7e381a367c5b254ae83f6a7ffe8ea280 to your computer and use it in GitHub Desktop.
csvファイルのファイルストリーミング(読み込み)
// バッファと, 本来のデータの型は同じにする。
//EnemyData data[256]; // バッファ
//EnemyData enemy[256]; // 例
void FileStreamEnemy(const TCHAR *file_name)
{
char buf[100];
int c;
int col = 1;
int row = 0;
memset(buf, 0, sizeof(buf));
if (-1 == fopen_s(&fp, file_name, "r"))
{
MSG("エラー発生");
EXIT
}
// 最初を読み飛ばす
while (fgetc(fp) != '\n');
while (1)
{
while (1)
{
c = fgetc(fp);
// 末尾ならループから抜ける
if (c == EOF)
{
return;
}
// カンマか改行でないなら文字として読む
if ((c != ',') && (c != '\n'))
{
strcat_s(buf, (const char*)&c);
}
// カンマか改行なら抜ける
else
{
break;
}
}
// 1セル分の文字列になった
switch (col)
{
//1列目は敵種類 atoi関数で数値として代入する
case 1: data[row].type = atoi(buf); break;
//2列目は弾種類
case 2: data[row].shot_type = atoi(buf); break;
//3列目は移動パターン
case 3: data[row].move_pattern = atoi(buf); break;
//4列目はショットパターン
case 4: data[row].shot_pattern = atoi(buf); break;
//5列目は出現時間
case 5: data[row].in_time = atoi(buf); break;
//6列目は停止時間
case 6: data[row].stop_time = atoi(buf); break;
//7列目は発射時間
case 7: data[row].shot_time = atoi(buf); break;
//8列目は帰還時間
case 8: data[row].out_time = atoi(buf); break;
//9列目はx座標
case 9: data[row].x = atoi(buf); break;
//10列目はy座標
case 10: data[row].y = atoi(buf); break;
//11列目は弾速度
case 11: data[row].speed = atoi(buf); break;
//12列目はHP
case 12: data[row].hp = atoi(buf); break;
//13列目はアイテム
case 13: data[row].item = atoi(buf); break;
}
// バッファの初期化
memset(buf, 0, sizeof(buf));
// 列
++col;
// 読んだ文字が改行なら
if (c == '\n')
{
col = 1;
++row;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment