????????? (????????) http://programming.xjtu.edu.cn - PowerPoint PPT Presentation

1 / 63
About This Presentation
Title:

????????? (????????) http://programming.xjtu.edu.cn

Description:

http://programming.xjtu.edu.cn 14 14.1 14.2 14.3 ... – PowerPoint PPT presentation

Number of Views:51
Avg rating:3.0/5.0
Slides: 64
Provided by: 6649303
Category:
Tags: edu | http | programming | xjtu

less

Transcript and Presenter's Notes

Title: ????????? (????????) http://programming.xjtu.edu.cn


1
?????????(????????)http//programming.xjtu.edu.c
n
2
?14? ?????????
  • 14.1 ?????
  • 14.2 ?
  • 14.3 ?????
  • 14.4 ???I/O
  • 14.5 ????
  • 14.6 ?????/??

3
14.1 ?????
  • ???????
  • ?C??????????
  • ????????,??????????????

4
???????
  • 1??????
  • ?C???????
  • C???????????????????????????????????????????????
  • 2?????
  • ??C?I/O??????????????????????STL???

5
????????
  • 1?????????
  • 2?C??????
  • ??C?????????,??.h??
  • ??C????????,??.h??
  • ???C?????????????C????,?????C??????????.h?????c?
    ??

6
14.2 ?
  • 1??C?,??????????
  • ???????????????????,????????
  • ??????????????????,?????????????????????
  • ?????????????
  • ????????????
  • ????????????????????????????
  • 2???
  • C????????????????????,????????,?????????????????
    ??????????,??????????????????

7
??/???????????
iostream????????
8
14.3 ?????
  • ???iostream???4???
  • cin?????
  • cout ?????
  • cerr ????????
  • clog ????????
  • ???I/O ?????iomanip
  • ?????? ?????fstream

9
??????????
  • ????(???)
  • 1. ?????????(ltlt)
  • 2. ??????put( )??????
  • 3. ??????write( )???????

10
  • ????(???)
  • 1. ?????????(gtgt)?
  • 2. ??????get( )??????
  • 3. ??????getline( )??????
  • 4. ??????read( )???????

11
??????????
  • get()????????
  • getline()????????????
  • put()??????
  • ignore()?????????
  • putback()
  • peek() ????????????,???????????
  • read()/write()????????
  • gcount()??????

12
?14-1 ??cin?cin.get???
  • include ltiostreamgt
  • using namespace std
  • int main()
  • const int SIZE 80
  • char ch1,ch2,ch3,buffer1SIZE,buffer2SIZE
  • cout ltlt "Enter a sentence\n"
  • cin gtgt buffer1
  • cout ltlt "The string read with cin is\n" ltlt
    buffer1
  • ch1 cin.get()
  • ch2 cin.get()

13
  • cout ltlt "\nThe result of cin.get() is\n" ltlt ch1
    ltlt ch2
  • cin.get(ch3)
  • cout ltlt "\nThe result of cin.get(ch3) is\n" ltlt
    ch3
  • cin.get(buffer2,SIZE)
  • cout ltlt "\nThe string read with
    cin.get(buffer2,SIZE) is\n" ltlt buffer2 ltlt endl
  • return 0

14
14.4 ???I/O
  • 1. ??????????????????
  • (1) ?????????
  • ????????(????)
  • basefield? delocthex
  • adjustfield? leftrightinternal
  • floatfield? seientificfixed

15
  • (2) ??????????
  • long flags( )????????????
  • ?cout.flags(iosscientificiosshowpos)
  • long setf()???????
  • ?cout.setf(iosuppercaseiosscientific)
  • long unsetf(long)???????

16
  • (3) ???????????
  • ????int width( )
  • ????int fill( )
  • ????int precision( )

17
??
  • include ltiostreamgt
  • using namespace std
  • int main()
  • cout.precision (4)
  • cout.width(10)
  • coutltlt10.12345ltltendl
  • cout.fill ('')
  • cout.width (10)
  • coutltlt10.12345ltltendl
  • coutltlt"Hi"ltltendl
  • cout.width (10)
  • cout.setf(iosleft)
  • coutltlt10.12345ltltendl
  • return 0

18
?. ????
  • ??I/O??????????????iomanip.h????
  • ??? ? ?
  • skipws ????????
  • left ??????????????
  • right ??????????????
  • internal ?????????????????
  • dec ??????????
  • oct ??????????
  • hex ???????????
  • showbase ?????????????
  • showpoint ??????????????????0

19
  • ??? ? ?
  • uppercase ????????????AF,????
  • ????????E
  • showpos ?????????
  • fixed ???????????
  • scientific ????????????
  • untibuf ??????, ostream?osfx???
  • ?????????????cerr
  • setbase(base) ??????base

20
?14-2 ????????
  • include ltiostreamgt
  • include ltiomanipgt
  • using namespace std
  • int main()
  • int n
  • cout ltlt "Enter a decimal number "
  • cin gtgt n
  • cout ltlt n ltlt " in hexadecimal is " ltlt hex ltlt n
    ltlt '\n'
  • ltlt dec ltlt n ltlt " in octal is " ltlt oct ltlt n ltlt
    '\n'
  • ltlt setbase( 10 ) ltlt n ltlt " in decimal is " ltlt
    n ltlt endl
  • return 0

21
?14-3 ???????
  • include ltiostreamgt
  • include ltiomanipgt
  • include ltcmathgt
  • using namespace std
  • int main()
  • double root2 sqrt( 2.0 )
  • int places
  • cout ltlt setiosflags( iosfixed)
  • ltlt "Precision set by the precision member
    function" ltlt endl

22
  • for ( places 0 places lt 4 places )
  • cout.precision( places )
  • cout ltlt root2 ltlt '\n'
  • cout ltlt "Precision set by the " ltlt "setprecision
    manipulator\n"
  • for ( places 0 places lt 4 places )
  • cout ltlt setprecision( places ) ltlt root2 ltlt
    '\n'
  • return 0

23
14.5 ????
  • ????
  • ???????
  • ?
  • ?
  • ????
  • ???

24
??????
25
?. ???????
  • (1) ?????????
  • ???
  • fstream lt????gt
  • ????.open("????,??)
  • ??
  • fstream ????.open("???",??)

26
  • ???
  • ? ?????
  • ofstream lt????gt
  • lt????gt.open(?lt???gt?)
  • ??
  • ofstreamlt????gt.open(?lt???gt?)

27
  • ? ?????
  • ifstreamlt????gt
  • lt????gt.open(?lt???gt?)
  • ??
  • ifstream lt????gt.open(?lt???gt?)

28
  • (2) ??????
  • ????.close( )

29
???? ??
ios?in ?????????,????????????????
ios?out ?????????,??????
ios?ate ????????????????????
ios?app ????????????????
ios?nocreate ???????,???????
ios?noreplace ?????ios ? ate?ios?app,????????,??????
ios?binary ????????????,???????
ios?trunc ?????,???????????????
30
?. ?????
  • ??????grade?????,???3??????????
  • include ltiostreamgt
  • include ltfstreamgt
  • using namespace std

31
  • int main()
  • ofstream out("grade")
  • if(!out)
  • cout ltlt "Cannot open the grade file." ltlt endl
  • return 1
  • out ltlt "C" ltlt " " ltlt89.5 ltlt endl
  • out ltlt "English" ltlt " " ltlt 93.5 ltlt endl
  • out ltlt "Maths" ltlt " " ltlt 87 ltlt endl
  • out.close()
  • return 0

32
?14-5 ???
  • ???14-4???grade??,?????????????
  • int main()
  • ifstream in("grade")
  • if(!in)
  • cout ltlt "Cannot open grade file.\n"
  • return 1

33
  • char course20 float grade
  • in gtgt course gtgt grade
  • cout ltlt course ltlt " " ltlt grade ltlt "\n"
  • in gtgt course gtgt grade
  • cout ltlt course ltlt " " ltlt grade ltlt "\n"
  • in gtgt course gtgt grade
  • cout ltlt course ltlt " " ltlt grade ltlt "\n"
  • in.close()
  • return 0

34
?. ?????
  • ???????????a?????data.dat?????a?20???,?len???
  • ? ??ofstream???????????????????
  • ofstream wf(?data.dat?,ios?binary)
  • wf.write((char )a, len)

35
  • ? ??ofstream???????,????open????????
  • ofstream wf
  • wf.open(?data.dat?,ios?binary)
  • wf.write((char )a, sizeof a)

36
  • ? ???????,???setmode????????
  • ofstream wf(?data.dat?)
  • wf.setmode(filebuf?binary)
  • wf.write((char )a, len)

37
  • ? ??????????setmode????
  • ofstream wf(?data.dat?)
  • wfltltbinary
  • wf.write((char )a, len)

38
?14-6 ????
  • include ltiostreamgt
  • include ltfstreamgt
  • using namespace std
  • int main(int argc, char argv)
  • char ch
  • if(argc!2)
  • cout ltlt "Usage ProgramName ltfilenamegt\n"
  • return 1
  • ifstream in(argv1, iosin iosbinary)

39
  • if(!in)
  • cout ltlt "Cannot open the file."
  • return 1
  • while(in) // in will be false when eof is
    reached
  • in.get(ch)
  • if(in) cout ltlt ch
  • return 0

40
? 14-7 ??????????
  • ?3??????????????????????,???????,???????????
  • include ltiostreamgt
  • include ltfstreamgt
  • using namespace std
  • struct list
  • char course10
  • int score

41
  • int main()
  • list st13, st23
  • int i
  • ofstream out("grade", iosbinary)
  • if(!out)
  • cout ltlt "Cannot open the grade file.\n"
  • return 1

42
  • for (i0 ilt3 i)
  • cin gtgt st1i.course gtgt st1i.score
  • out.write((char )st1i, sizeof(st1i))
  • out.close()
  • ifstream in("grade", iosbinary)
  • if(!in)
  • cout ltlt "Cannot open the grade file.\n"
  • return 1

43
  • cout ltlt "File grade" ltlt endl
  • for (i0 ilt3 i)
  • in.read((char )st2i, sizeof(st2i))
  • cout ltlt st2i.course ltlt " " ltlt st2i.score
    ltlt endl
  • in.close()
  • return 0

44
????????
  • ?????????I/O????????????????????????

45
(1) ??????????
  • istream istream ?seekg(????)
  • istream istream ?seekg(???,????)
  • long istream ?tellg( )
  • ??,lt????gt?lt???gt??long??,???????lt????gt?????
  • cur1?????????
  • beg0?????????
  • end2?????????

46
(2) ??????????
  • ostream ostream ?seekp(????)
  • ostream ostream ?seekp(???,????)
  • long ostream ?tellp( )

47
? 14-8 ?????????
  • ???????????????????,???????????????,?????????????
    ???????
  • include ltiostreamgt
  • include ltfstreamgt
  • include ltiomanipgt
  • include ltcstdlibgt
  • using namespace std
  • struct TVChannel
  • int channelNum
  • char channelName20
  • void outputLine( ostream output, const TVChannel
    c )

48
  • int main()
  • ofstream outTV( "tv.dat", iosate
    iosbinary)
  • if ( !outTV )
  • cerr ltlt "File could not be opened." ltlt endl
  • return 1
  • cout ltlt "Enter channel number " ltlt "(1 to 100, 0
    to end input)\n? "
  • TVChannel tv
  • cin gtgt tv.channelNum

49
  • while ( tv.channelNum gt 0 tv.channelNum lt
    100 )
  • cout ltlt "Enter Channel Name\n? "
  • cin gtgt tv.channelName
  • outTV.seekp( ( tv.channelNum - 1 ) sizeof(
    TVChannel ) )
  • outTV.write( (char )( tv ), sizeof( TVChannel
    ) )
  • cout ltlt "Enter channel number\n? "
  • cin gtgt tv.channelNum

50
  • outTV.close()
  • ifstream inTV( "tv.dat", iosin )
  • if ( !inTV )
  • cerr ltlt "File could not be opened." ltlt endl
  • return 1

51
  • cout ltlt setiosflags( iosleft ) ltlt setw( 10 )
    ltlt "ChannelNum"
  • ltlt setw( 20 ) ltlt "Channel Name" ltlt endl
  • inTV.read( (char )( tv ), sizeof( TVChannel )
    )
  • while ( inTV !inTV.eof() )
  • if ( tv.channelNum ! 0 )
  • outputLine( cout, tv )
  • inTV.read( (char )( tv ), sizeof( TVChannel )
    )
  • return 0

52
  • void outputLine( ostream output, const TVChannel
    tv )
  • output ltlt setiosflags( iosleft ) ltlt setw( 10
    )
  • ltlt tv.channelNum ltlt setw( 20 ) ltlt
    tv.channelName ltlt '\n'

53
??????
  • ???12??????Date?,??ltlt???,??Date???
  • class Date
  • friend ostream operatorltlt( ostream , const
    Date )
  • int day, month, year
  • ?Date.cpp?????????
  • ostream operatorltlt( ostream output, const Date
    d )
  • static char monthName 12 "January",
    "February", "March", "April", "May",
  • "June", "July", "August", "September",
    "October", "November", "December"
  • output ltlt monthName d.month-1 ltlt ' ' ltlt d.day
    ltlt ", " ltlt d.year
  • return output

54
// Example14-9.cpp????????
  • include ltiostream gt
  • include "date.h"
  • using namespace std
  • int main()
  • Date today, Olympicday(2004,8,13)
  • cout ltlt "Today (the computer's day) is " ltlt
    todayltlt endl
  • today 365
  • cout ltlt "After 365 days, the date is " ltlt today
    ltlt endl
  • Date testday(2,28)

55
  • cout ltlt "the test date is " ltlt testday ltlt endl
  • Date nextday testday 1
  • cout ltlt "the next date is " ltlt testday ltlt endl
  • today.SystemDate()
  • cout ltlt "the Athens Olympic Games openday is "
    ltlt Olympicday ltlt endl
  • cout ltlt "And after " ltlt Olympicday-today
  • ltlt " days, the Athens Olympic Games will open."
    ltltendl
  • return 0

56
?14-10 ????????
  • // Example 14-10???????
  • include ltiostreamgt
  • namespace university
  • int grade4
  • namespace highschool
  • int grade3

57
  • int main()
  • stdcoutltlt"The university's grade is
    "ltltuniversitygrade ltltstdendl
  • stdcoutltlt"The highschool's grade is
    "ltlthighschoolgrade ltltstdendl
  • return 0

58
?14-11 ???14-6???
  • ???????????ASCII????????????
  • include ltiostreamgt
  • include ltfstreamgt
  • include ltcctypegt
  • include ltiomanipgt
  • using namespace std
  • int main(int argc, char argv)
  • if(argc!2)
  • cout ltlt "Usage ProgramName ltfilenamegt\n"
  • return 1

59
  • ifstream in(argv1, iosin iosbinary)
  • if(!in)
  • cout ltlt "Cannot open input file.\n"
  • return 1

60
  • int i, j
  • int count 0
  • char c16
  • cout.setf(iosuppercase)
  • while(!in.eof())
  • for(i0 ilt16 !in.eof() i) in.get(ci)
  • if(ilt16) i-- // get rid of eof
  • for(j0 jlti j) cout ltlt ' ' ltlt setw(2) ltlt
    setfill('0') ltlt hex ltlt (int) cj
  • for( jlt16 j) cout ltlt " "
  • cout ltlt "\t"

61
  • for(j0 jlti j)
  • if(isprint(cj)) cout ltlt cj
  • else cout ltlt "."
  • cout ltlt endl
  • count

62
  • if(count16)
  • count 0
  • cout ltlt "Press ENTER to continue "
  • cin.get()
  • cout ltlt endl
  • in.close()
  • return 0

63
? ? ?
  • ???????????
  • ???????????????????

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