かんたん作成【HTML CSS JavaScript PHP CGI Perl Ruby Python .htaccess】

アクセスカウンター

#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;
}

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");
    count = get_count();
    printf("あなたは %d 人目の訪問者です。<br>\n", count);
    printf("</body>\n");
    printf("</html>\n");
    return 0;
}
かんたん作成【HTML CSS JavaScript PHP CGI Perl Ruby Python .htaccess】