CS1061_L18

download CS1061_L18

of 13

Transcript of CS1061_L18

  • 8/16/2019 CS1061_L18

    1/13

     

    CS1061 C ProgrammingLecture 18: Sequential File Processing

    A. O’Riordan, 2004, 2007 updated

  • 8/16/2019 CS1061_L18

    2/13

     

     The FILE structure C views each file as a sequence of btes

    !iles end with the end"of"file #ar$er %&O!'

    (trea# created when a file is opened

    Openin) a file returns a pointer to a !*+& structure

    he !*+& structure

    !ile descriptor  inde- into operatin) sste# arra called the open file table

    !ile Control loc$ %!C' found in ever arra ele#ent, sste# uses it to ad#inister the file

  • 8/16/2019 CS1061_L18

    3/13

     

    Creating a FileFILE *myPtr;

    creates a !*+& pointer called #/tr 

    myPtr = fopen("myFile.dat", openmode);

    !unction fopen%' returns a !*+& pointer and ta$es two ar)u#ents file to openand file open #ode.

    *f open fails, 1++ returned.

    fclose(FILE ointer!

    !unction fclose%' closes specified file.

    &ven thou)h it’s perfor#ed auto#aticall when pro)ra# ends it is )ood practiceto close files e-plicitl.

  • 8/16/2019 CS1061_L18

    4/13

     

    "ea#ing a sequential access $leA sequential access file is written and read fro# the start to the end of the file

    usin) a file pointer.

    Create a !*+& pointer, tie it to the file to read

    myPtr = fopen("myFile.dat", "r");

    se fscanf%' to read fro# the file.

    +i$e scanf%', e-cept first ar)u#ent is a !*+& pointer, e.).3

    fscanf(myPtr, "%d%s%f", &myInt, mytrin!, &myFloat);

  • 8/16/2019 CS1061_L18

    5/13

     

    foen(!&ver call to fopen%' will tpicall be followed with a test, li$e this3

    ifp = fopen("inpt.dat", "r");

    if(ifp == #$LL)printf("cant open file'n");

    eit or retrn

    or 

    if((ifp = fopen("inpt.dat", "r")) == #$LL)

    printf("cant open file'n");

    eit or retrn

  • 8/16/2019 CS1061_L18

    6/13

     

    File %o#es!iles are open in a certain #ode.

    %&'E SE' F&" FILE C"E)TE'* E+ISTI,- FILE*

    .a. )en#ing /es )en#e# to

    .a. "ea#ingaen#ing /es )en#e# to

    .r. "ea# onl2 ,o /es

    .r. "ea#ing3riting ,o /es

    .3. 4rite onl2 /es 'estro2e#

    .3. "ea#ing3riting /es 'estro2e#

    he nu#ber of strea#s that #a be open si#ultaneousl is specified b the #acro

    !O/&15A6 %in stdio.h'. !O/&15A6 #ust be at least ei)ht in *(O C.

  • 8/16/2019 CS1061_L18

    7/13 

    fscanf(! an# frintf(!fscanf%' and fprintf%' are file processin) equivalents of scanf%' and printf%'.

    /rototpes %as in stdio.h'3

    int fprintf(FILE *stream, const c+ar *format, ...);

    int fscanf(FILE *stream, const c+ar *format, ...);

    fprintf%stdout, 8ello' is eqivalent to printf%98ello'

    fscanf%stdin, 9:d;,

  • 8/16/2019 CS1061_L18

    8/13 

    frintf(! e5amleinclde -stdio.+

    inclde -stdli/.+

    int main()

    int i, score0123;

    FILE *4icores;

    for(i=2; i-12; i55) score0i3 = 122 6 i;

    if(7(4icores = fopen("scores.tt", "8"))== #$LL)

    for(i=2; i-12; i55)

    fprintf(4icores, "#m/er %d9't %d'n", i51, score0i3);

    retrn 2;

  • 8/16/2019 CS1061_L18

    9/13 

    fscanf(! e5amleinclde -stdio.+

    int main()

    int i;FILE *fp;

    fp = fopen("scores.tt", "r");

    8+ile(fscanf(fp, "%d", &i) 7= E:F)

    printf("%d'n", i);

    retrn 2;

  • 8/16/2019 CS1061_L18

    10/13 

    fgetc(! an# futc(!f)etc%' reads one character fro# a file.

    a$es a !*+& pointer as an ar)u#ent.

    f)etc%' and )etc%' are identical, e-cept that )etc%' is usuall i#ple#ented as a#acro for efficienc.

    fputc%' writes one character to a file

    a$es a !*+& pointer and a character to write as an ar)u#ent.

    +i$ewise fputc%' and putc%' are identical.

  • 8/16/2019 CS1061_L18

    11/13 

    fgetline(!Read one line fro# data.t-t, copin) it to line arra %not #ore than #a- chars'.

    =oes not place ter#inatin) >n in line arra. Returns line len)th, or 0 for e#ptline, or &O! for end"of"file.

     1ow we could read one line fro# ifp b callin)3

    define r?);

    ...

    f!etline(ifp, line,

  • 8/16/2019 CS1061_L18

    12/13 

    fgetc(! e5amleint f!etline(FILE *fp, c+ar line03, int ma)

    int nc+ = 2, c;

    ma = ma 6 1; * lea@e room for '2 *

    8+ile((c = f!etc(fp)) 7= E:F)

    if(c == 'n)

    /reaA;

    if(nc+ - ma)

    line0nc+3 = c;

    nc+ = nc+ 5 1;

    if((c == E:F) && (nc+ == 2))

    retrn E:F;

    line0nc+3 = '2;

    retrn nc+;

  • 8/16/2019 CS1061_L18

    13/13 

    Caution(equential access file cannot be #odified without the ris$ of destroin) other

    data.

    !ields can var in sie "different representation in files and screen than internalrepresentation.

    ?, B4, "D0 are all ints, but have different sies on dis$.

    ecause of these li#itations C also has rando# access file processin) features.

    (ee ne-t lecture.