c Ó é ¬ å Û ï ¬ Ö ó - waseda university...: 7 p î æ b \ q s x | Ó é ¬ å Ü m 4 ^ d \...

14
C ϓϩάϥϛϯά ೖ ϑΝΠϧೖग़ Ҵେ

Upload: others

Post on 12-Dec-2020

0 views

Category:

Documents


0 download

TRANSCRIPT

  • C

    — —

  • fopenfclosefprintf

    printffscanf

    exit

  • (fopen )

    fprintf fscanf

    (fcolse )

    FILE *

    fopen(“ ”,” ”)

    fprintf( , , )

    fscanf( , , )

    fclose( )

  • FILE * ;

    FILE * ’ ’

    FILE stdio.h

  • fopen fopen(" "," ")

    FILE *file; /* file */

    file = fopen(" "," ");

    w

    r

    r NULL

    fclose

    fclose ;

  • fprintf

    fprintf( , );

    ”w”

    int a=10;FILE *file=fopen("test","w"); /*test */fprintf(file, "%dYt",a); /* 10 */fprintf(file, "%d",a+1); /* 11 */fprintf(file, "%dYn",a*2); /* 20 */fclose(file);

  • printf

    C Y

    Yn Yt Y" YY

    " Y

    printf

    printf %

    %d 10 %x 16

    %5d5 %05d 5 55

    %f 10 %.2f

    %5.2f5

    %+5.2f+

    5%p %e 1.23e-4%c %s%% %

  • 11

    kukuout.c

    kuku

    fprintf ”%2dY t” ”Y n”

    1 2 3 4 5 6 7 8 92 4 6 8 10 12 14 16 183 6 9 12 15 18 21 24 274 8 12 16 20 24 28 32 365 10 15 20 25 30 35 40 456 12 18 24 30 36 42 48 547 14 21 28 35 42 49 56 638 16 24 32 40 48 56 64 729 18 27 36 45 54 63 72 81

  • fscanf

    scanf

    fopen

    fscanf( , );

    ”r”

    int a; double b;FILE *file=fopen("test","r"); /*test */fscanf(file, "%d",&a); /* a */fscanf(file, "%lf",&b); /* b */fclose(file);

  • C 3

    stdin →fprintf(stdin,”· · · ”,· · · ) ⇔ scanf(”· · · ”,· · · )

    stdout → (./a.out )fprintf(stdout,”· · · ”,· · · ) ⇔ printf(”· · · ”,· · · )

    stderr → (./a.out )stdout stderr UNIX OS

  • exit

    main

    #include

    0main return 0; 0

    exit(1);

    exit

    fprintf(stderr," " ;

  • ✓ ✏FILE *file = fopen("test","r");if(file==NULL){ /* */

    fprintf(stderr, "cannot open file ’test’"); /* */exit(1); /* */

    }✒ ✑✓ ✏double a; int scannum;FILE *file ;...scannum = fscanf(file,"%lf",&a); /* */if(scannum!=1){ /* */

    fprintf(stderr, "cannot read file"); /* */exit(1); /* */

    }✒ ✑fclose exit

  • 22

    kukuin.c

    kuku

    - X

    1- 2- 3- 4- 5- 6- 7- 8- 9-2- 4- 6- 8- 10- 12- 14- 16- 18-3- 6- 9- 12- 15- 18- 21- 24- 27-4- 8- 12- 16- 20- 24- 28- 32- 36-5- 10- 15- 20- 25- 30- 36X 40- 45-6- 12- 18- 24- 30- 36- 42- 48- 54-7- 14- 11X 28- 35- 42- 49- 56- 63-8- 16- 24- 32- 40- 48- 56- 64- 72-9- 18- 27- 36- 45- 54- 63- 72- 81-

    kuku

    exit

  • fopenfclosefprintf

    printffscanf

    exit