[ create a new paste ] login | about

Link: http://codepad.org/YWsKOWtO    [ raw code | output | fork ]

C, pasted on Feb 1:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(int argc, char *argv[])
{
  FILE *fp_out = NULL;
  char name[256] = { '\0' };
  char age[128] = { '\0' };

  if (argc != 2) {
    fprintf(stderr, "Kadai1 <Filename>\n");
    exit(1);
  }

  if ((fp_out = fopen(argv[1], "w")) == NULL) {
    fprintf(stderr, "Cannot open file: %s\n", argv[1]);
    exit(2);
  }

  fprintf(fp_out, "名前\t年齢\n");
  for (;;) {
    memset(name, 0, sizeof(name));
    memset(age, 0, sizeof(age));
    printf("名前: ");
    if (scanf("%s", name) == EOF) {
      break;
    }
    printf("年齢: ");
    if (scanf("%s", age) == EOF) {
      break;
    }
    if (fprintf(fp_out, "%s\t%s\n", name, age) < 0) {
      fprintf(stderr, "Failed to write\n");
      fclose(fp_out);
      exit(3);
    }
  }
  fclose(fp_out);
  printf("\n");

  return 0;
}


Output:
1
Kadai1 <Filename>


Create a new paste based on this one


Comments: