金子邦彦研究室データベースCSV 形式ランダムデータの合成(C を使用)(Ubuntu 上)

CSV 形式ランダムデータの合成(C を使用)(Ubuntu 上)

乱数などを用いて,整数,浮動小数,文字列,日時を含むデータを合成. C のプログラム. データベースシステムの機能や性能を試験するために作成した.

#include<stdio.h>
#include<time.h>
#include<sys/time.h>
#include<stdlib.h>

int main(int argc, char** argv)
{
  struct timeval tv;
  struct tm *pnow = NULL;
  int i;
  srand((unsigned)time(NULL));

  for( i = 1; i < atoi(argv[1]); i++ ) {

    gettimeofday(&tv, NULL);
    pnow = localtime(&tv.tv_sec);

    int yy = pnow->tm_year+1900; 
    int mm = pnow->tm_mon + 1; 
    int dd = pnow->tm_mday; 

    int h = pnow->tm_hour;
    int m = pnow->tm_min;
    int s = pnow->tm_sec;
    int msec = tv.tv_usec; 

    /* K, NAME */
    printf( "%12.12d, A001, ", i ); 
    /* B1, B2 */ 
    printf( "%f, %f, ",     100.0 * ((double)rand())/RAND_MAX,     100.0 * ((double)rand())/RAND_MAX ); 
    /* created_at */ 
    printf( "%4.4d-%2.2d-%2.2d %2.2d:%2.2d:%2.2d.%6.6d\n", yy, mm, dd, h, m, s, msec ); 
  }

  return 0;
}

使い方

  1. 上のプログラムを populate.c のようなファイル名で保存.
  2. コンパイル

    Linux では,次のような操作でコンパイル

    gcc -o populate.out populate.c
    

    [image]
  3. 次のようにして実行

    10」のところには,生成したいデータの行数を指定.

    ./populate.out 10
    

    [image]
  4. 次のようなファイルが得られる

    整数,浮動小数,文字列,日時を含むデータである.

    [image]