shougi_input.c
an anonymous user
·
C
#include <stdio.h>
#include <string.h>
#include "shougi_data.h"
void csv_read();
person dataretu[81]; // 駒のデータの読み込み
int koma_cnt = 0;
void explode(
/*----------------------------------*/
/* CSVデータから項目を取り出す */
/*----------------------------------*/
const char* kugiri, // 区切り文字
char* buf, // CSVの1レコード
int tatehoukou)
{
char* cp0, * cp;
int yokohoukou = 0, len;
cp0 = buf; // CSVデータの先頭アドレス
for (yokohoukou = 0; yokohoukou < 9; yokohoukou++)
{
if (*cp0 == 0x22) cp0++; // 最初の"(0x22)を除く
cp = strstr(cp0, kugiri); // 区切り文字を検索
if (cp == NULL) break; // 区切り文字なし
len = cp - cp0; // 項目の文字数
if (*(cp - 1) == 0x22) len--; // 最後の"(0x22)を除く
if (len > 0) // 項目あり
{
memcpy(&dataretu[koma_cnt].koma[0], cp0, len); // 項目の文字列をコピー
dataretu[koma_cnt].tate = tatehoukou + 1;
dataretu[koma_cnt].yoko = 9 - yokohoukou;
koma_cnt = koma_cnt + 1;
}else{
dataretu[koma_cnt].tate = tatehoukou + 1;
dataretu[koma_cnt].yoko = 9 - yokohoukou;
koma_cnt = koma_cnt + 1;
}
cp0 = cp + 1; // 次の文字のアドレス
}
}
void csv_read(){
FILE* fp;
char buf[512], * cp;
int tatehoukou;
fp = fopen("shougi_banmen.csv", "r"); // 将棋のファイルを開く
if (fp == NULL) goto END; // ファイルを開けない
tatehoukou = 0;
memset(&dataretu[0], '\0', sizeof(dataretu)); // データの全文字をNULLに
while (1)
{
cp = fgets(buf, 256, fp); // 1レコードを読む
if (cp == NULL) break; // EOF
explode(",", buf, tatehoukou); // csvデータを1行ずつ、項目を取り出す
tatehoukou = tatehoukou + 1;
if (tatehoukou == 9) break;
}
fclose(fp); // 将棋のファイルを閉じる
END:;
}
Output
Embed on website
To embed this program on your website, copy the following code and paste it into your website's HTML:
Comments
This comment belongs to a banned user and is only visible to admins.
This comment belongs to a deleted user and is only visible to admins.