File-based Input and Output - PowerPoint PPT Presentation

About This Presentation
Title:

File-based Input and Output

Description:

- File-based Input and Output ... – PowerPoint PPT presentation

Number of Views:81
Avg rating:3.0/5.0
Slides: 32
Provided by: abas151
Category:
Tags: based | file | input | output

less

Transcript and Presenter's Notes

Title: File-based Input and Output


1
????? ??? ?????-????? ?? ???? ??????? ?? ????
  • File-based Input and Output

2
?????
  • ?????? ?? ???? ???????? ????? ? ????? ??
    ??????? ?? ??
  • ??????? ?? ?? ???? ????? ? ????? ??????? ?? ?
    ?? ?? ??????? ??? ??? ???
  • ?? ?????? ?? ????? ?????? ?????? ??????? ? ??
    ????? ???? ????
  • ????? ????? ??????? (intermediate results)
  • ????? ????? ??????? ?? ???????
  • ????? ?? ????? ?? ?????? ???????? ???? ??????
    ???? ????
  • ????? ?? ??????? ???? ?? ???? ?????? ???? ????
    ????

3
  • ??? ????? ???? ??????? ?? ???? ???? ???? source
    file ?? ???? ??????? ???
  • ?? ??????? ?? ????? FORMAT ?? ???? ?? ???
    ??????? ??????? ?? ???? ????
  • ??? ???? ?? ?? ???? ?? ?? extension ?????? ??
    ???? ??? ????? ????? ???? ??? ????? ????.
  • ?????? ?? ???? Text ????? ??? ?? ?? Editor ???
    ???????? ?????? MS Word ? ???? ???? edit ???
    ?????
  • ?? ?????? ???????? ?? ??? ?????? ?? ???? ???? ???
    ????? ? ????? Unit ????? ???? ?? ???
  • ?? ?? ???? ?? ????? Unit Number ?? ???? ??????
    ???
  • ??? ?? ???? ?? ?????? ?? ?? ????? ?????? ?? ???
  • ?? ????? ??? ????? ?? ????? ?? 1 ?? 100 ????.

4
????? ???? Unit Number
  • ???? ?????? ?? ?? ???? ?? ???? ??????
  • ?????? ?? ?????? ?????? ??? ?? ?????? ?? ?? ??
    ???? ?? ??? ?? ??????? ????? ???? ?? ???
  • UN ?? ????? ?? INTEGER ? ?? ?? ????? ???????? ??
    ????? INTEGER ????
  • ????? ?? ????? ?? ???? ??? ??? ??? ( ????
    ???? ? ???????) ???
  • ???? ?? ????? ?? ?? ???? ??? ??? ?? ??????? ???
    ?????? ?? ???? ????? 5 ? 6 ???? ???? ???? ?
    ??????? ??? ????? 10 ?? ????? ??? UNIX ????
    ????? ?? ??? fort10 ?????? ?? ????

5
  • ???? ????? ?? ?? ???? ? ?????? ?? ?? ???? ???? ??
    ????? WRITE ? READ ??????? ????
  • ?? ????? ?? ?? ?????? ???? ??????? ???? ?? ???
    ???? ???? ????? OPEN ??? ? ?? ?????? ?????? ????
    ????
  • ??? ??? READ ???? ????
  • READ (clist) I/O list
  • Clist ????? ??
  • UNIT unit-number,
  • FMT format-spec
  • ,REC record-number
  • ,IOSTATios
  • ,ADVANCEadv
  • ,SIZEinteger-variable
  • ,EORlabel
  • ,ENDlabel
  • ,ERRlabel

6
????
  • READ ,I,J ?? ???? ????
  • READ ,LINE ?? ???? ????
  • READ (,) A,B,C ?? ???? ????
  • READ (5,) LINE ?? ???? ????
  • READ (5,100) X, Y, Z ?? ???? ???? ?? ????? 100
  • READ (UNIT10,FMT100,ERR10,IOSTATios)
  • ?? ???? 10 ?? ????? 100 ?? ??? ??? ????? ??
    ?????? ???? ????? ?? ????? ????? 10 ?? ??? ?
    ????? ios ?? ????? ???? (???? ?? ?????) ??
    ????? ??? ?????? ?? ?????? ????? ??? ios ?????
    ??? ?? ????
  • ?? ??? ????? ???? ??? UNIT ? ??? FMT ????? ??
    ???. ???? ????? ??????? ?????

7
????? ?????
  • WRITE (clist) I/O list
  •  where clist is defined as    
  • UNIT unit-number,
  • FMT format-spec
  • ,REC record-number
  • ,IOSTATios
  • ADVANCEadv
  • ,SIZEinteger-variable
  • ,EORlabel
  • ,ERRlabel

8
????
  • PRINT, I,J ?? ??? ???????
  • WRITE (,) I,J ?? ??? ???????
  • WRITE (6,) I,J ?? ??? ???????
  • WRITE (6,100) I ?? ??? ????????? ????? 100
  • WRITE (6,,ERR10) LINE
  • WRITE (UNITfile1,FMT100,RECrecordnumber,
    ERR10) newline

9
????? OPEN
  • ???? ??? ???? ? ?? ?????? ?????? ????? ???? ??
  • ??? ???
  • OPEN (u, olist )
  • u ????? ?????? ???? ??? ?? ???? ?? ????? ???
    ????? ? ????? ??????? ?? ???
  • WRITE (u,) variables list
  • Olist ?????? ???? ????? ?? ????? ???? ?? ???? ??
    ????
  • OPEN(10)
  • OPEN (UNIT10)
  • OPEN (UNITIFILE)
  • OPEN (15, FILEoutput.test)

10
???? ????? ?? Olist
  • 1- STATUS st
  • St ?? ????? ??? ?? ???? ??? ??? ?? ?? ???
    ?????
  • Old
  • ???? ???? ???? ?? ???? ????? ??? ???
  • ???? ???? ??? ????? Input file
  • ??? ?? ???? ????? ????? open ????? ?????? ????
    ??? ???? ?? ???
  • NEW
  • ???? ???? ???? ?? ????? ???? ??????
  • ?? ????? ??? ????? ?? ???? ???? ????? ?? ????
  • ??? ?? ???? ???? ????? ?? ??? ??? ???? ???? ???
    ???? ?? ???

11
  • REPLACE
  • ??? ????? ?? ??? ?? ??? ??? ????? ???? ?? ??
    Delete ? ???? ????? ?? ???? ??? ????? ?? ???
  • ??????? ???? ?? ??? ??? ???? ?? ??? ?? ???
  • ?SCRATCH
  • ?? ??? ???? ???? ????? Close ???? ??? ???? ???
    ?? ???
  • ???? ??????? ????? ?? ??????? ?? ???? ??????
  • UNKNOWN
  • ???? ???? ??? ????? ???? ?? ???
  • ??? ?? ??? ????? ???? ???? ??? ???? ? ??? ?????
    ????? ???? ????? ? ??? ????? ???

12
  • 2- ERR Label
  • ??????? ?? ?????? ????? ?? ??? ?? ????? ??? ????
    ???? ????? ???? ??? ????? ?? ????? ?? ????? Label
    ?? ???
  • 3- IOSTATivar
  • ?? ?????? ?? ????? ERRLabel ?? ????. ??? ??
    ???? ??? ???? ???? ????? ???? ??? ivar ?? ????
    ??? ????? ?? ????
  • ??? ??? ???? ??? ????? ???? ?? ????
  • ???? ?? ?????? ???? ?? ????? ????? ???? ??????
    ???? ?? ????
  • ??? ????? ?????? ???? ???? ??? ????? ???

13
???? ???? ?? ????? OPEN
  • OPEN (UNIT10, FILEdata.out')
  • OPEN (UNIT11, FILEdata1.xls', STATUS'NEW',
    ERR10)
  • .......
  • 10 CONTINUE
  • WRITE(6,) 'Error opening file data1.xls'
  • OPEN (UNIT12, FILE'student.records',
    STATUS'OLD',
  • ERR20)
  • ........
  • 20 CONTINUE
  • IF (ERR gt 0) THEN
  • WRITE (6,) 'Error opening file
    student.records.'
  • ENDIF
  • STOP

14
????? ???? ???? ?? Close
  • ?? ??? ????? ????? ?????? ?? ???? ???? ??? ?? ???
    ????? ?????? ? ?? ?? ?????? ?????? ??? ?? ????.
  • ?? ???? ?? ????? Open ?????? ?????? ?? ?? ????
    ???? ? ?? ?? ???? ???? ??? ?? Option ??? ???? ??
    ???? ???
  • ??? ????? ?? ????? Close ???? ??? ? ?????? ?? ??
    ????? ??? ???? ??????? ????? ?? ???? ??? ?? ???
  • ??? ???
  • Close (UnitUnit_number, ERRlabel,
  • STATUSst)
  • St ?? ????? Keep ? ?? Delete ????. ????? ?? ??
    ???? SCRATCH ??? ??? ??? ??? ????? ????? keep
    ?????

15
???? ????? Close
  • CLOSE (10)
  • CLOSE (UNIT10, ERR10)
  • CLOSE (UNITNUNIT, STATUS'DELETE', ERR10)

16
  • ????? ???
  • REWIND
  • ????? ?? ??? ????? ?? ?? ????
  • BACKSPACE
  • ????? ?? ?? ????? ???
  • INCLUDE filename
  • ???? ???? ???? ?? ???? ????? ? ????? ??????
  • ???? ??????? ??????? ????? ? ???? ??????? ???
    ?????? ?????

17
????
  • OPEN (UNIT7, FILEtest, STATUSnew, IOSTAT
  • m1, ERR88)
  • a12.0 b13.5 c14.7
  • WRITE (7,1) a, b, c
  • 1 FORMAT (1x, 3f5.1)
  • CLOSE (UNIT7, STATUSkeep, IOSTAT m2,
  • ERR99)
  • WRITE (,2)
  • 2 FORMAT (//, file written successfully)
  • STOP
  • 88 WRITE (6,3) m1
  • 3 FORMAT (Had trouble opening file, error
    code,I12)
  • STOP
  • 99 WRITE (6,4) m2
  • 4 FORMAT (Had trouble closing file, error
    code,I12)
  • END

18
  • ?? ????? ?????? ???? test ????? ?? ???
  • ?? ?? ????? ?????? ?? ????????? ??? ???? ??????
    ????? ??
  • ??? ?????? ?????? ?? ???? ???? ????? ?? ????? 88
    ?? ??? ??? ???? test ???? ????
  • ??? ?? ????? CLOSE ?? ??? keep ?? delete ???????
    ??? ?????? ?? ???? ????? ??? ????
  • ??? ?? ????? OPEN ?? ??? NEW ?? Unknown ???????
    ??? ?? ?????? ???? ?? ???????????

19
???? ?????? ?? C
  • File based Programming in C

20
????? ????
  • ?????? (??????)
  • ???? ???? (?? ?????? ?? ?????)
  • ??? ????
  • ????? ?????? ??????
  • ??? ????? ???? ??
  • ????
  • ????? ??? ???? ?? ?????? ??? ?????? ???? ?????
  • ???? ?? ?? ?????? ?????? ???? ???? ?? ????
  • ???? ??? ???????

21
???? ??????? ?? ???? ??
  • ??????? ?? ?? ???? ?? ???? ????? ???
  • ????? ?? ????? ???? ????
  • ??? ???? ????
  • ?????? ? ?? ????? ?? ????
  • ???? ????

22
????? ????
  • ??????? ?? ??? ???? ?? ?? ??? FILE
  • ???? ?????? (??? ??????) ???
  • ?? ???? ????? stdio.h
  • ??????? ????? ?? ?????? FILE
  • ??? ????? ???? (??? ???? ?? ????? ????)
  • ??? ???? (?????? ?????? ??????? ????)
  • ???? ??? ???? ??? ?????? ? ????
  • ????? ?? ???? ???? ????
  • ????? ??? ?? ?? ?????? ? ?????

23
????? ??????? ?? ???? ?? ??????
  • ????? ?? ????? ?? ??? ????? ?? ?? ?????? ????
  • ????
  • FILE inputFile
  • ?? ??? ????? ????? inputFile ?? ?? ??? ????? ??
    ??? ?? ?????? FILE ????? ?? ???
  • ???? ??????? ???? ????? ????? ?? ??????? ?? ???

24
  • 2- ??? ???? ????
  • ??????? ?? ???? fopen
  • FILE fopen (char fileName , char )
  • ??????? ??? ???? ???? ?? ???? ????? ?? ??????
    ??????? ??? ????? ??? ? ?? ????? ?? ?? ???? ??
    ?????? ????? ??? ?? ??????
  • fileName ???? ????? ?? ??? ?? ??? ????? ???? ??
    ???? ?? ???. ??? ???? ?? ????? ????? ?? ??? ????
    ???? ???? ?? ??? ???? ??? ????
  • C\\myletters\\letter.txt
  • ?? ?????\\ ???? ???!!!

25
??????? mode ?? ???? fopen
  • ???? ????? ?? ?? ???? ??? ???? ???? ?? ???? ??
    ???
  • ????? ?? ?? ???
  • ??? ??? ????? ????? ?????? ???? ???? ?? ??? ????
    (?????? ? ?? ?????)
  • ??? ??? ????? ????? ??? ???? ??? (?????? b ? ????
    t ). ??? ??? t ??? (????)

???? ??? ???? ???? ???????
r ?????? ???? ???? ?? ??? ????? ????
w ????? ???? ???? ????? ?? ???. ??? ???? ??????? ?? ?? ??? ?? ???
a ?????? ?? ????? ??? ???? ????? ????? ? ??? ???? ?? ????? ????? ?? ???
r ?????? ? ????? ???? ???? ????? ????
w ????? ? ?????? ???? ???? ????? ?? ???. ??? ???? ??????? ?? ?? ??? ?? ???
a ?????? ? ?????? ??? ???? ????? ????? ? ??? ???? ?? ????? ????? ?? ???
26
???? ??? ???? ????
  • FILE inputFile
  • inputFilefopen (letter.txt, rt)
  • ?? ??? ???? ???? letter.txt ????? ???? ???? ?????
    ??? ?? ???. ??? ?? ???? ?? ??????? ?? inputfile
    ???? ???? ?? ?? ??? ???? ?????
  • ????
  • FILE itemFile
  • itemFilefopen (C\\data\\items, wt)

27
3- ???? ????
  • ??????? ?? ???? fclose
  • ???? ?? ???? ????? ?? ????? ??? ???? ?? ???? ??
    ????
  • FILE inputFile
  • .
  • fclose (inputFile)
  • ??? ???? ???? ???? ?? ?? ????? ?????? ??? ????
    ???? ?? ???.
  • ??? ?????? ?? ?? ????? ??? ??? ??????? ?? ????
    ??? ???? ? ?? ???? ????? ???

28
4- ????? ? ????? ?? ???? (?????? ? ?????)
  • ????? ?? ??? ????
  • ??????? ?? ???? fprintf ?? ??? ??? ???
  • fprintf (ltfile-pointergt,ltcontrol
    stringgt,ltvariable-listgt)
  • ????
  • FILE outPut
  • outPutfopen (data.txt, wt)
  • fprintf(outPut, " d\n",grade)

29
???? ?????
  • include ltstdio.hgt
  • void main()
  • FILE outFile
  • int i, n,sum
  • outFile fopen ("sales.txt","wt")
  • if (!outFile)
  • printf("can't open file")
  • return
  • printf("enter the upper limit")
  • scanf("d", n)
  • sum0
  • for (i0iltn i)
  • sumsumi
  • fprintf(outFile, " d\n",sum)
  • fclose (outFile)

??? ?????? ????? ???? ????? ?? ?? ?? ??? ?? ????
???? ???? ?????? ? ?? ?? ???? ????? ?? ???
30
??????
  • ?????? ?? ??? ????
  • ??????? ?? ???? fscanf ?? ??? ??? ???
  • fscanf (ltfile-pointergt,ltcontrol
    stringgt,ltvariable-addres-listgt)

31
???? ??????
  • include ltstdio.hgt
  • void main()
  • FILE inFile
  • int sales, counter0
  • float average0.0
  • inFile fopen ("sales.txt",rt")
  • if (!inFile)
  • printf("can't open file")
  • return
  • printf(calculation of average sale")
  • while (!feof (inFile))
  • averagesales
  • counter
  • fscanf(inFile,"d", sales)
  • fclose (inFile)
  • average/counter

??? ???? ????? ????? ???? ?? ???
???? feof ????? ???????? ?? ??? ????? ?? ?? ????
??? ?? ????? ?? ????? ?? ?? ?????? ???? ????
????? ???? (??? ???) ? ?? ??? ??????? ?????
?????? (???) ?? ??? ?? ??????
Write a Comment
User Comments (0)
About PowerShow.com