ログイン
kid's world

ファイルを扱う具体例

while(c!='>' && c!=EOF)
  c=fgetc(fp);
#include <stdio.h>
#include <stdlib.h>

main()
{
  FILE *fp;
  int c;
  char filename[100];
  
  scanf("%s",filename);
  
  if ((fp=fopen(filename, "r")) == NULL) {
    exit(1);
  }
  
  while((c=fgetc(fp)) != EOF) {
    if (c=='<') {
      while (c!='>' && c!=EOF)
        c=fgetc(fp);
    }
    else
      putchar(c);
  }

  fclose(fp);
}
#include <stdio.h>

main()
{
  int op,i,j;
  int a[100];
  FILE *fp;

  do {
    scanf("%d",&op);
    
    switch (op) {
    case 1:
      fp=fopen("data.txt","r");
      for(i=0; i<100; i++)
        fscanf(fp, "%d", &a[i]);
      fclose(fp);
      break;
    case 2:
      fp=fopen("data.txt","w");
      for(i=0; i<100; i++)
        fprintf(fp, "%d ",a[i]);
      fclose(fp);
      break;
    case 3:
      scanf("%d",&i);
      printf("a[%d] = %d\n", i, a[i]);
      scanf("%d",&j);
      if(j==1){
        printf("a[%d] = ", i);
        scanf("%d",&j);
        a[i]=j;
      }
    }
  } while (1<=op && op<=3);
}
    case 1:
      fp=fopen("data.bin","rb");
      fread(a, sizeof(int)*100, 1, fp);
      fclose(fp);
      break;
    case 2:
      fp=fopen("data.bin","wb");
      fwrite(a, sizeof(int)*100, 1, fp);
      fclose(fp);
      break;

広告


©Toshio Koide 1996-2007.

mail.gif

広告