Skip to content

Instantly share code, notes, and snippets.

@froop
Created May 9, 2011 15:09
Show Gist options
  • Save froop/962702 to your computer and use it in GitHub Desktop.
Save froop/962702 to your computer and use it in GitHub Desktop.
[C言語] CSVファイル出力
#include <stdio.h>
#include <errno.h>
#define LINE_SEP "\r\n" /* 改行コード */
static int G_iWriteErrFlg = FALSE;
static int G_iWriteErrNo = 0;
void proc_write_err(void)
{
G_iWriteErrFlg = TRUE;
G_iWriteErrNo = errno;
}
int write_char(FILE *fp, char ch)
{
int iRc = 0;
errno = 0;
iRc = fputc(ch, fp);
if (EOF == iRc) {
proc_write_err();
return -1;
}
return 0;
}
int write_str(FILE *fp, char *cpStr)
{
int iRc = 0;
errno = 0;
iRc = fputs(cpStr, fp);
if (EOF == iRc) {
proc_write_err();
return -1;
}
return 0;
}
/* 区切り文字出力 */
int write_sep(FILE *fp, int hasNext)
{
int iRc = 0;
char *cpSep = NULL; /* 区切り文字 */
/* 行の途中ならカンマ区切り */
if (hasNext) {
cpSep = ",";
}
/* 行の最後なら改行 */
else {
cpSep = LINE_SEP;
}
/* ファイルへ出力 */
iRc = write_str(fp, cpSep);
if (0 != iRc) {
return -1;
}
return 0;
}
/*
* CSV形式で値を出力
*「"」で囲う場合
* 引数:
* *fp 出力先ファイル
* hasNext 同一行に後続の値があればTRUE ->「,」を出力
* なければFALSE -> 改行を出力
* *cpStr 出力する値
*/
void write_val(FILE *fp, int hasNext, char *cpStr)
{
int iRc = 0;
if (G_iWriteErrFlg) {
return;
}
/* ダブルクォーテーションで囲う(開き) */
iRc = write_str(fp, "\"");
if (0 != iRc) {
return;
}
/* 1byte単位 */
while ('\0' != *cpStr) {
iRc = write_char(fp, *cpStr);
if (0 != iRc) {
return;
}
/* 「"」なら「""」に変換 */
/* 「"」(0x22)はシフトJISの範囲外のため2byte文字の考慮は不要 */
if ('"' == *cpStr) {
iRc = write_char(fp, '"');
if (0 != iRc) {
return;
}
}
cpStr++;
}
/* ダブルクォーテーションで囲う(閉じ) */
iRc = write_str(fp, "\"");
if (0 != iRc) {
return;
}
/* 区切り文字を付加 */
iRc = write_sep(fp, hasNext);
if (0 != iRc) {
return;
}
}
int main(int argc, char *argv[])
{
int rc;
FILE *fp;
if ((fp = fopen("test.dat", "w")) == NULL) {
fputs("write open error !!", stderr);
return -1;
}
write_val(fp, TRUE, "abc");
write_val(fp, TRUE, "あいうえお");
write_val(fp, FALSE, "\"あい\"\"うえお\"");
write_val(fp, FALSE, "abc");
write_val(fp, TRUE, "\"");
write_val(fp, FALSE, "");
if (G_iWriteErrFlg) {
fprintf(stderr, "file write error !! errno=%d strerror=%s\n",
G_iWriteErrNo, strerror(G_iWriteErrNo));
return -1;
}
fclose(fp);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment