アクセスカウンター(画像)
#include <stdio.h> #include <stdlib.h> #include <string.h> int get_count(void) { FILE *fp; int count; char buf[16]; fp = fopen("./dat/count.dat", "r+"); /* ファイルを開く */ fgets(buf, 16, fp); /* カウント読み込み */ count = atoi(buf) + 1; /* カウント(文字列を数字に変換、1加える) */ fseek(fp, 0, SEEK_SET); /* ファイルポインタ移動 */ fprintf(fp, "%d", count); /* ファイル書き込み */ fclose(fp); /* ファイルを閉じる */ return count; } void print_count(int count) { int i; char c[16]; char dir[] = "../../sample/img/no/"; /* 数字画像保存先 */ sprintf(c, "%d", count); /* カウントを文字列に変換 */ for(i = 0 ; i < strlen(c) ; i++) { /* 一文字づつ画像に */ printf("<img src=\"%s%c.gif\" width=\"20\" alt=\"%c\">", dir, c[i], c[i]); } } int main(void) { int count; printf("Content-type: text/html\n\n"); printf("<html>\n"); printf("<head>\n"); printf("<title>アクセスカウンター</title>\n"); printf("</head>\n"); printf("<body>\n"); printf("あなたは "); count = get_count(); print_count(count); printf(" 人目の訪問者です。<br>\n"); printf("</body>\n"); printf("</html>\n"); return 0; }
〔 実行する 〕