{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

lecture_20 - ECE 190 Lecture 20 I 0 1n C Lecture Topics...

Info icon This preview shows pages 1–12. Sign up to view the full content.

View Full Document Right Arrow Icon
Image of page 1

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Image of page 2
Image of page 3

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Image of page 4
Image of page 5

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Image of page 6
Image of page 7

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Image of page 8
Image of page 9

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Image of page 10
Image of page 11

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Image of page 12
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: ECE 190 Lecture 20 March 31, 2011 I / 0 1n C Lecture Topics Stream and buffered I/O concepts Creating l/O streams: fopen, fclose l/O one char at a time: fgetc, fputc i/O one line at a time: fgets, fputs End of file: EOF & feof Formatted i/O: fprintf, fscanf Binary stream I/O: fread, fwrite Navigation inside a file: fseek, fteli Low-level file I/O: open, close, read, write Lecture materials Textbook Ch 18 Homework Machine problem MP4.1 due 4/6 MP4.2 due 4/14 Announcements 1 V. Kindratenko “>M' ., . RWJ @Imme LQQ-IID.I __-> I/O Irma CL? 1”? 3"? FumoIvM _W€ vae Legal .39 It” 46 5 “WM 3 ‘IP‘IWCI: ”4 TRHP x20 [6N6 PPMHI) “I‘M TMIP x2; {aw *> Sireew 5263 #520sz 01/2 ~> WQIIW; 43 aII aILM/fldvzer’ gageo/ l/Q ié‘ #PVIDIWMJ 0” SIM/dams >114 qu QIVW I CKQWIDZQ ' 'L PPPW IIIII P” ”I” @33ng 5" LIAIImI 01%le Iqu-B Values (2 3 ) (‘VIIMWIP/rfl) “4 Io I42 34mm > 0W Erogww Ia” Mme/S W: law; 700m #1 QImaazM/z 9 flu I _ 9 Ire/«WA exam/9 Ie I____ I J— PPDWW ) ..'5.__ Si 939 [123/ \> Our Frag/rm PMS Values (:10 mam/m I 9 Sale gained 001 IIM‘I cleV/CQ @uc/WIS Wm ”[0 I I122 aIpIoroprmIe Mes/[52 _ I 7 Ifif SIMW a/gsimaI/pm aIIW/S c$ VA? qtu W I dewonIQ I’M Iprodacar Irma . I’M mgwmw ’ I WI’W/{A Implwus 06%;er £1 £2 cliff—[MW PQL-Ie . “I gljefipbé) ‘5 CL? MEJ’LIIM +m W :2. 8% W E“, diva/3‘ - r _ PM (i _' ' ‘> '1?’ I" 5 _M&/ ‘J W, M01 , ,. 3168;; “(9); _) \> H¢+ [(90]! (+512 dI§F%, Lke/[f/DW“ é. 2% p3 [ Wald- W 13> F? yebéowd/ guy/1% age/g Ii (wieww lg) ~> «fiz'zJolzegL _ ¥ r2223 aw ,1 (Wk/19-01403) ~> _ cli‘spllag > _ __ _ .7 wilflu._ W \ 3%me [#[4 @ @904”? 62 74144 69014144“; or Sivm Je>amfi£§lar 7 FILE fiW§~lVer _ (a Marlo cur (ff/[g @914sz I’m/o a 75% [W 0%; WM‘ _ MWS4VW : ‘QFeH "fl, 9495;“ urn), 91Con 0 9 ”a 0”” gym (ll/(gig (at; mtg/{fig ~> 941%? WWW i”; 9M(f;f7z‘€~cl_ (11$ MM ‘IUWZVZ WW4 vl—a W #Mfil/am 1f 0%. . “9“WcLe“ argon/Wu! [SW4 ”El/WV!) dIBOL/Wé, WI/uaxL vaflW’s we WW4 >10 [99"me (9% m firearm/4:. _ _ , 9 ”f” ~ ”Miomlg ._> ”WM’WWLig—wh-Zflm W2 aid/é .7 ”a” —- a2 ‘10 em": r’ "z ~> “H“ v. :WWWW¢I 94,1:ng 0W7" W _ g5$flfmfi1fig 004 flu? 74/”44 ., ' _ "> “W_+“«ow_ 0h?” Vw&%. J wrflvé’ag. .‘> 40W ”Wt/ts. 12 WNW. +0 fl. W SJWW/ W? A/Wb 1% 2‘1 umnoi mwmsérm. Q (1&6 4% firm 4 (9 MN m aimlm whey: olme {lo/Loy [me/WSwHw/m}; Wale/r ca Val/We,” 4 7353a P9 “ W0; §._ (_ , a :ey4wwr()i vmmk_aneefim»$mn7 9¢dfla [MM%® a 14%Q‘lchwjvlm‘3b”) _} waLg we Wat¥w_ 0’1sz _ 44:5 9(ka dzse/rfpky $'Wm4e a thQ (MWWN&*§ _‘ W 1>uéobwwfl> “/ ww‘lag MHZ WW OV/ (4)10 flzi‘li’g% {mflpxyy afV/hlc‘(c, Mad/54mm) 3 wm‘ifi: #gl‘fl Web/(1 0/6: -b M&M§#vwm _‘_‘> etrmkkz MAJ CUM Mgmawcl (we! Swim/e )4 (2 74% it [Walla #QHJCQ.Z1_‘> _ _ [Ia—(— WMM[) \ i _ tun-L 0,) l #494 _ FILE #001 3m rem'n ‘ _ __ walaW-Lg—Lramvz VEWEW/ht9uio_dfilhl (AM/u}; 1‘} {WMUMW ==_§/l/Z{LZ,) ”9*“r” ‘1; , Em 6: WV()/ W («1* 1: 'w‘) 4 ~ _ _ § $601416 (0/ (911—1de Slmgm); H +dfl%€{oalpuiS~/mm)) H VVe/vflmm U) _ SH ECE 190 Lecture 20 March 31, 2011 I / 0 one char at a time: fgetc, fputc #include <stdio.h> int main() { int 0; FILE *f; /* write to file */ f = fopen("out.txt", "w"); if (f == NULL) printf("Unable to open file out.txt for writing\n"); return —1; c = getchar(); while (0 != '\n') { fputc(c, f); c = getchar(); } fclose(f); /* read from file */ if ((f = fopen("out.txt", "r")) == NULL) {” printf("Unable to open file out.txt for reading\n"); return —1; c fgetc(f); while (c != EOF) /* 30F is a macro defined in stdio.h */ { putchar(c); c = fgetc(f); } putchar('\n'); fclose(f); return 0; 2 V. Kindratenko ECE 190 Lecture 20 March 31, 2011 I / 0 one line at a time: fgets, fputs int main() { FILE *inf; FILE *outf; char buf[32]; if ((inf = fopen("inputfile.txt", "r")) == NULL) { printf("Unable to open file inputfile.txt for reading\n"); return -1; ((outf = fopen("outputfile.txt", "w")) == NULL) printf("Unable to open file outoutfile.txt for writing\n"); return —l; } fgets(buf, sizeof(buf), inf); while (!feof(inf)) /* feof tests the end—of—file indicator */ { fputs(buf, outf); fgets(buf, sizeof(buf), inf); } fclose(inf); fclose(outf); return 0; 3 . V. Kindratenko \ ~> W‘M’ wm‘fL‘fg VEWV/Wflei 7W4 7210 W M -~> SW Wei; vaWHed fl-WT (id/1% 7%” 9741/07. .-—7 $19le Lg . ‘ ‘ ¥ _. ”7 {LPN/(M L} {WS‘sz} K > W.N“r/~€S UTW7L¢€L _. _ \> £9W7£ [ngmm/ . / wwig (farw,H&é/{ flgcj] (WM 7Lf‘0744 fiwgrwm. a PhiMl'F QXWFfiéj _ , PPI'M1L7L["W o ,_ f , %B \V' F/”¢1%J\“ 933’”. _ . (WW/WWW ska/55M WW? _> [Writ/ILL Wmieé [Sig 41%4’ PWWL?!» [7LflV‘I’W/lflyz gim’ud +0 W (9&1in 979W, _ .___> [M M lfi’wwgfi OWL defibgwi Maversw‘ph gdgeu‘la~ _ 4100MS(7.,>5W€ eMJ/w‘wd (Stud W 0&er Mel/M _- _ éumlflm WWQWW 1‘9 Fm’wlédl (eéwrd/hgan _ > 9(9ng (91/15 3W“ mum; (Maw W4 @«g/w _ Mow/v \> are i'hwlerfarewled wd fm’mleo/ Mmcmfl, ~»> MMVfi/trgl‘gm g' “fifwf/ ' Za‘Wfiff/IW ”‘5 1%} +3404 keel ~ CLCC’JW MW _. 00F} ~ [lfidfll/‘Vl& 3901/14" him/043W %X H? LLKQEHM'M/ mum/é??? H , W, amazM/e WWW fat Wilswcéquw; V . W. ,V MM _.. W..4.“kuW,M._...._. ___.ma...__.__.__.___. 5 '? Saw/g; frat/"Jazz: ' W WWIWJ) ¥. , (‘er WOhCIl/LI/ dag/V W) Awe W; . V _ PMM { "[1449?! MW :gz‘rWM._«g/rw-de~pax’h¥—cormwgfl\h”/; e M (:g 45/44/45 43 w w, w); wmvergz‘m 9/;m3flwwux LN—j ”VYW‘L 94M”? _7'-J:0VWVL §vzkflmg rglgoflgmwlg m7lvrm¢ avl Ma 1946me sir-wow. _ _ H __ _ ‘7 Sheufiel‘wlz’m (m; flu CW6 (29 9&7?” {yr/mag ~> tormF/Q; I’hfmalgf _ Ehlw: lame 6Mch W Mould V‘Z/[é/ég 3,02 _ “-> zS./.AWW/r94(fi74 SPEC/“fiafibh I‘MSJVuflh SW! )4: , 9W (1 927me ”vi anJcmg /s¥r/24} w: 90% . Weak/L141? Slaw/e 1'3 wquRI/LKC/VWJ L0 Geo/93 W I'mlo W [WV , _ _ __ ‘> %J SageuFflk/d‘é/flm flMSJVMfl/slg 9L; sum (Q QQWAQ 0:} d’R/‘Lg .625 W Lari Sal 0% WWW/Mile karg and waved Maw #4440 cc dea’mé WW. ,_ . "> M224 ,Wmlek I594 J’Ae Vicki/nfl/JIQ’LXs4P/Q4fi i"; ’/’37Z;§ SWWL ficmigb $wa 411/; Wwvlek ,6», (Me MW; j Snfidawnw We Woéfge: 9W$ éeqiare fév _ ‘>°SW7[ Yflwfurng m humgék OJ Sugwssgtuaig valteci . WA «2934‘ng 1‘me 1W5. . ¥. , 1 5 a; ’Li g: g ! E ECE190 Lecture 20 March 31, 2011 Formatted I / O: fscanf, fprintf #include <stdio.h> #define N 100 #define M 100 int main() { FILE *inf; FILE *outf; int n, m; int i, j; int array[N][M]; /* write to file */ if ((inf = fopen("array.txt", "r")) == NULL) { printf("Unable to open file array.txt for reading\n"); return —1; } fscanf(inf, "%i %i", &n, &m); for (i = O; i < n; i++) for (j = 0; j < m; j++) fscanf(inf, "%i", &array[i][j]); fclose(inf); if ((outf = fopen("outputfile.txt", "w")) == NULL) { printf("Unable to open file outputfile.txt for writing\n"); return ~1; } fprintf(outf, "%i %i\n", m, n); for (i = 0; i < m; i++) { for (j = 0; j < n; j++) fprintf(outf, "%i ", array[j][i]); fprintf(outf, "\n"); } fclose(outf); return 0; 4 V. Kind ratenko we 49401,».z,> e W § § 4.292 p.97, :11anwa Mfr/L MLVMfl/$__/_t9 § § § _ 5M4 «ma/u; Z) < [Wm avemw MM; WWI/$3;< as) .z‘mL V‘oW9/ Mfumlaé/ r, c fiéfi¢%_}_\/&Z_ FILE M; 576 ( 3“?” (”437(me "92+“ ~izfl/MM {: _ ¥P:I(M<ll7f[§1ldi—i:i: ”IL/Ml? W117 Dig—W 7Q/\Z)mb4lfl,d¢wl \jML‘) 1‘18 um ”13 , ) /.. “% Z4531» ( C; 0 o ég @émflhqj eff) . ‘3’ $112th : sum / ” ”/24 1,4 Z4 Wow/9, Vwklw’wé ’ H qu (314240;: 1:37?) 4‘ _ I _ J) _ UQFNLMH [giderr/ '4 Error maul/mg V/a \MH); _ 7W”? (9) _ Ve—Lwrh -1“ g / 40% {”30} V 4 WW9} PW) 5 *0)» ((3:6)) CdaagaMw/g;g+rjo< __‘ 317(2st saijjfl) “%J”_) QWZ); _ c'JKQéaxiwg [:04 ., , . , ; . 4})Y‘1'14qu [glclerr/ 9§wror War/$15 [0/9 MMZJJ \‘4‘4, KC» #6er H); Y/€1[wrm +1; . 3 E 1} WWW .503. ”w“; * U ’ nfpr’IMlnE-IH) M 22 new”; ext/mg? [ej/wlumwg); ’ : dew (39 ‘ ELM 0); ECE190 Lecture 20 March 31, 2011 Binary stream I / O: fread, fwrite #include <stdio.h> #define N 100 #define M 100 int main() { FILE *inf; FILE *outf; int n, m; int i, j; int array[N][M]; /* write to file */ if ((inf { fopen("array.txt", "r")) == NULL) printf("Unable to open file array.txt for reading\n"); return -1; } fscanf(inf, "%i %i", &n, &m); for (i = O; i < n; i++) for (j = 0; j < m; j++) fscanf(inf, "%i", &array[i][j]); fclose(inf); if ((outf = fopen("outputfile.txt", "w")) == NULL) { printf("Unable to open file outputfile.txt for writing\n"); return -1; } /* mix ascii and binary mode */ fprintf(outf, "%i %i\n", n, m); for (i = O; i < n; i++) fwrite(array[i], sizeof(int), n, outf); fclose(outf); return 0; 5 , V. Kindratenko ...
View Full Document

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern