File Processing - Lecture Slides | CS 350, Study notes of Computer Science

Material Type: Notes; Professor: Adjeroh; Class: Computer System Concepts; Subject: Computer Science; University: West Virginia University; Term: Unknown 2000;

Typology: Study notes

Pre 2010

Uploaded on 07/30/2009

koofers-user-evc
koofers-user-evc 🇺🇸

10 documents

1 / 46

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
1
2000 Prentice Hall, Inc. All rights reserved.
Chapter 11 – File Processing
 
   
 ! "$#&%'%)(* " %+  ,
 - ./ 0 "12%+345 "%+671
 8 97 "% :%;3<">=+" %?0@?A">1B1C./ 0 "
 D EF"%G? :H#A%G'%;I 6J%3<"=" %?0K@+A"1K1C./ 0 "
 L EF%+G6J@>"1K1M./ 0 ">1
'N 97 "% :%OE/%?G6P@+>"11C./ 0 "
 Q RST :O#&%U%HEF%?!60 ,VUW%HEF%?!6P@+A"1K1C./ 0 "
 X EF"%G? :H#A%G'%;3<"=+"Y5 %?0 0 ,VI Y6J%HE!%+GY6P@?>">1B1M./ 0 "
Z [\]%?6^G0 "_`@O! %?F1B%Yba] >">1K1< :Ha :+ %6
8 8 9c6;6d%+HeK "W@f :+&6d"YU1
2000 Prentice Hall, Inc. All rights reserved.
gVgih<gkjKldmonpSqsrdtumv'pwl
xzy|{}~{kC{fV2C{}~CV|VdV{}&f7{V
VdYC2CiVdV{
U¢¡2£$¤¥/¡P¦+£§¡2¨ª©«Y¥¬¡¥/®P¤2¯+¡¯°£°o«Y¥/¤±¡s«>©U¤2¥/±¡
¤2®s«¦¯G°<£H«©W¨¤~°4¤
²°<«Y¥/¤2±¡P«A©¨¤A°<¤P¢¯w³?¤¥FU¤2´U¡2£$¤¯+¨µ¤2¥/¥/¤A¶?£$'£$«¯
°4¡2®s¬«Y¥/¤2¥5¶
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25
pf26
pf27
pf28
pf29
pf2a
pf2b
pf2c
pf2d
pf2e

Partial preview of the text

Download File Processing - Lecture Slides | CS 350 and more Study notes Computer Science in PDF only on Docsity!

 2000 Prentice Hall, Inc. All rights reserved.

Chapter 11 – File Processing



   

  ! "$#&% '%)(* " %+  ,

 - ./ 0 "12%+345 "%+  8 9 7 "%  :%;3<">=+" %?0@?A">1B1C./ 0 "

 D EF"%G? :H#A%G'%;I 6J%3<"=" %?0K@+A"1K1C./ 0 "

 L EF%+G6J@>"1K1M./ 0 ">

'N 9 7 "%  :%OE/%?G6P@+>"11C./ 0 "

 Q RST  :O#&% U%HEF%?! 60 ,VUW%HEF%?! 6P@+A"1K1C./ 0 "

 X EF"%G? :H#A%G'%;3<"=+"Y5 %?0 0 ,VI Y6J%HE!%+GY6P@?>">1B1M./ 0 "  Z []%?6^G0 "_`@O! %?F1B%  Yba] >">1K1< :Ha :+ %

8 8 9 c6;6d%+HeK "W@f :+&6d"YU

 2000 Prentice Hall, Inc. All rights reserved.

gVgih<gkjKldmonpSqsrdtum v'pwl

xzy|{}~{k€‚„ƒC ‡†ˆ{f‰‹ŠVƒŒ†2ƒC{}~ƒCŽV|V‘dŽV{}&ƒfŽ7’{“‰VŽ

‘V ”d†YƒC • 2ƒCŽ‹Ši–‹—˜‘V ”d™V {“š›

œžˆŸU ¢¡2£$¤•¥/¡P¦+£§¡2¨ª©«Y¥¬¡•¥/®P¤2¯+¡•¯ °£°o«Y¥/¤ˆ±¡s«>© U¤2¥/±¡

¤2®s«¦¯G°<£H«©W¨¤~°4¤

œž² °<«Y¥/¤2±¡P«A©¨¤A°<¤PŸ¢¯w³?¤•¥FŸU¤2´ U¡2£$¤ˆ¯+¨μ¤2¥/¥/¤A¶?£$Ÿ'£$«¯ ¶

°4¡2®s¬«Y¥/¤2¥5¶

 2000 Prentice Hall, Inc. All rights reserved.

gVgih¸·Œ¹+ºd»½¼$¾Hm¾À¿ªv'»wn¾snÁtºdÃ

ÄÆÅÈÇ É;ÊË&ÌP͕΢ÎUÏ2ËÉ;ÐÍ~É4ÍPÇ É4ψÌ

ÑÓÒGÔ*Õ Ö4ר&Ù (^0) Ú•Û 1

ÜÆÝˆÞ ß<àOáãâåäæ ß4ç

èžé]êBë4ìVíïî7êKíïî>ðëfñdòoóñAðñòíïë~ð

ôuõ (^) ë4òoö ÷?ñAøBìö ùGö íïêBú2ø ë4íïíïë&ðêú~ñ~ûìVêoüë4òoö ñAø`êBý÷dþîø ê

ÿ     !#"$&%$! (')!!

*,+.-./10243 5 687:9;&<>=/10?

@ACBD&EFGIHKJFELM(ENFBPOQ4RSBGTNVUBPOGW

X,Y&Z[]Z_^VZ1`abZdcIefhg struct ij\k class l,m.n.o1pq4r st#uwvxoyqzod{|}r~rz€{:€‚s#p?ƒ1oy|sd„z}P| ‡†ˆ}|\oyqo#|‰‹Š „4Œr o1|

s1pq4r }4{ssŽSo_„V}v.bo1Š vsd ŽŠ V‘zŽ.s|>Š s\v.‹Š †’Š „o_‹Š }v“vŒ&p”s#|‰ƒ>vSo#p?s_ƒ od & |•sV€ƒ1sd‚„4–

—˜™š›yœKžŸ ¡ Ÿ¢8ž€›Pš£4¤.›¥¦ž›§Ÿž¥¨

©,ª.«.¬1®4¯ °±#®¬d²³‰´¯ ¯zμ’¶ ¯°

·¸ ¹4º.¹»¹¼4½¿¾ÁÀÂÃÄÅ ÃÆÂ½Pǹ4º.½PÈTÆVÉÇ ½¼

 2000 Prentice Hall, Inc. All rights reserved.

gVgih¸·Œ¹+ºd» ¼u¾Hm¾À¿ªv'» nÁ¾Pn tºdÃËʧjKjÍÌ

ÎχÐÒÑPÓÕÔÖØ×ÙÐÚ

Û,ܕÝ&Þ1ßà‹á â’á Þdã?äyåÞdæVçPåݓàbç‡âÍädædá è~á à‚äà‚ÞàéÞyåÞdà‹åêá Þë:ä1èzç4âãìÞæ&á â‹á æ

åÞdæzçPåÝ&ã?âíå‰çîïäâ’á èÞ

ðñóòCôÕõóòÒöø÷4ùÍúCû$ü_ùˆûbò

ý þ4ÿ     ‚ÿ ÿ

Judy

Judy Green

Sally Black

Tom Blue

Judy Green

Iris Orange

Randy Red

File

Record

Field

Byte (ASCII character J)

Bit

 2000 Prentice Hall, Inc. All rights reserved.

² ²³´¶μ¸·4¹»ºa¼½y¾¿ÀºÀÁ8¹ÃÂÅĹƾ¼½ÇºÅÈ6ÉËÊÃÊ̹ÃÍ/ÍÏÎ-½~Èǹ

ÐÒÑÔÓÕ¶Ö ×Ø/ÙØÛڎ×ÝÜ ÓÞÙ¸Øßà˜á âßá à˜Ù

ãå䇿èç2æéê æ/çŽæë8ì[í]î™æïì[ðñòê ç óFëê ôí

ãåõì[æöïì[ó:÷F÷írìø÷tù!ñúéüûì[æýrê ðíFëTêiô íFñúéìvù2î2éùìí

þÒÿ  

– FILE myPtr;  "!# FILE $&%(')&+-,/.

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

fopen >*?A@;B> C9DFE FILE G9HJI K9LNM OLNHQPRI SMTAG9MAUVI PRI MAW

XZY[ \

M

&T]LN^_H

[

O*`=ab]McK7LNT#dPRI SM#LNH#H=G7McK

[

K9W PRI SMHcGeM Kfb_HAW"M

XZg

P

=P ISMK9HALhHcGeMKeMAWVi NULL j*kAl;m"j n7kAo

– fprintf p]q r*s9t printfu/vwVx&vhy&z{7| }~z€h}N‚(ƒc„v/

Az| ~€ FILE

†A‡(ˆ ‰VŠ‹/Œ_Š7ŽA‹ˆ*‘ ‹’Œ‹“4‹/ˆ ”=ˆ ‰A•—–c˜Š-˜/™

– feof(FILE pointer) š_›œ7žJ› Ÿ&¡ true ¢£(¤/¥&¦¨§© £ª§£ª¢ «¤

¢¥V¦(¢ ¬®-©(¯]°R¥V©²±#©(¯¤¦ ®®-©²³ ¯©c¬¤´μ´¶F¢ ´#´μ¤®£©¨¯h®9·&¤´³&¤¬¢ £¢ ¤¦

 2000 Prentice Hall, Inc. All rights reserved.

² ²³´¸μ¶·4¹»ºè¼½~¾¿ÀºÀÁ8¹ÃÂ Ä ¹Æ¾¼½ÇºÅÈ É ÊÃÊ»¹ÃÍ:ÍÏÎ-½yȹ¹¸º;º »

– fclose(FILE pointer) ¼h½ ¾¿ÀÁÀÀÂ&Á½à Īà ÁÅÆÄªÃ*¾ Á

Ç2È7ÉcÊËÍÌcÊ Î]ÉAÏ ÐcÑ7ÒNÌJÎ]ÐAÒ;Ó Ô7ÐcÕ ÕÖר9ÉcÙ ÚÊÌÛJÊ*ÐcÎÜÉcÙeÏVÝ

ÇZÞÌVÌAÏÆÚÊßÐAÔªÒ;Ó Ô9ÉÒNÌQÔVÕ ÌݐÉ#ËRÓ ÕÉ4ÝFÉ&àÚÕ ÓÔÓ Ò;Õ Ö

áãâå䁿 çèéê

ëíìcîïð¨îñ/ò#óôò#ñõ÷öcîï øùóóôú&ïÆûªü ýù"óþ/ï¨ú&ùûªüý ùþ/ï(î]ò#ñúAõÿûüý ùó

ë Vñø ûü ýù òAó&ñ Vùñ/ú cú ü Vù2úVñhò#ùñ/ú Zü ýý&ñ ùôñ

üûûù/îù/úö&ï¨ü ú-ùî

: ; < = > =? @ A B C D B ; E

r Open a file for reading.

w Create a file for writing. If the file already exists, discard the current contents.

a Append; open or create a file for writing at end of file.

r+ Open a file for update (reading and writing).

w+ Create a file for update. If the file already exists, discard the current contents.

a+ Append; open or create a file for update; writing is done at the end of the file.

 2000 Prentice Hall, Inc. All rights reserved.

F(G9HI JKML

NO4P Q"R SRTU RVWXYT1Z[R T\1U W^]

T1Q"_

FILE `1a

R

Q

"S W1Z

NO NcbdR Q"efS g"W

`

1a

R

Q

"S W1Z"S

a

Tih RUW

jO4P Q

`

k

S

Y_T1S T

jONclmZ[R SWS

a

hRUW

n

fprintfo

pO4qU

a

]rWih RUW

1 /* Fig. 11.3: fig11_03.c 2 Create a sequential file */ 3 #include <stdio.h> 4 5 int main() 6 { 7 int account; 8 char name[ 30 ]; 9 double balance; 10 FILE cfPtr; / cfPtr = clients.dat file pointer */

11

12 if ( ( cfPtr = fopen( "clients.dat", "w" ) ) == NULL ) 13 printf( "File could not be opened\n" );

14 else {

15 printf( "Enter the account, name, and balance.\n" ); 16 printf( "Enter EOF to end input.\n" );

17 printf( "? " );

18 scanf( "%d%s%lf", &account, name, &balance );

19 20 while ( !feof( stdin ) ) {

21 fprintf( cfPtr, "%d %s %.2f\n",

22 account, name, balance ); 23 printf( "? " );

24 scanf( "%d%s%lf", &account, name, &balance );

25 } 26

27 fclose( cfPtr );

28 } 29

30 return 0;

31 }

 2000 Prentice Hall, Inc. All rights reserved.

F(G9HI JKML

sZ

a

1t

Z

[T1uwv

k

S

`

k

S

Enter the account, name, and balance. Enter EOF to end input. ? 100 Jones 24. ? 200 Doe 345. ? 300 White 0. ? 400 Stone -42. ? 500 Rich 224. ?

 2000 Prentice Hall, Inc. All rights reserved.

² ²³ãâåäü¹»ºæ~Žy¾¿èçúa¼º}é2·yê{ ºÀÁ8¹ÃÂÅĹ ¾¼½ÇºÅÈ

É ÊÃÊ»¹ÃÍ:ÍÏν~Èǹ

ë„ì(í6îï7ðãñóòôîöõí÷7ø7íñ‘ùðúîû*îiüü-íiõ õêýðúûãí

þ ÿ^ ^

FILE    !  "#%$&

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

fscanf .0/"12354671/ 8 9.;: 2 67<= 2

>@?BA CED

scanfFGHBIGJLK+MON PQK+RPSUT%VWGXLKYN QZR FILE [;\U] ^_#`a

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

b)cedfdhgji5dkml!gnporq i%sutvwvwtv sfnxiev k

b)y5t zi|{}nw~5t fLt n vm{ npt vfiegW€Wt vk t-dfi5~ ‚wƒ„†

‡eˆŠ‰‹‚‡5ŒŽ }%‡‰ }‡

ˆ‡‘’w“O”†ˆE• ‡e‚

–@—5˜™šœ›wž žŸ †¡L˜U¢ £;™#›¤šœ¥u¦§;™Š¤£¨¢ £L™#›©›š5ª}wž §L›†«¬¡L›®¢ ¯O¢ ›¬°¦Ÿ}™#›†ž ˜®™±¢ ˜£³²

–μ´pž ¬!˜¶®Lwž ž›·m¦LŸ}™#›¶˜¯¯j¬!›™

– rewind(myPtr) ¸W¹ºe» ¼¤½5¾ ¿L¾ ¼pÀ½¶Á7¾ º|»}¼w½5¾ ¿L¾ ¼uÀm» ¼p¾ À¿ºe¹Š¿¼"à ºÄ ¾ÀwÀ¾ ÀÄ

¼¤Áu¿LÅ ºÁ!¾ º|Ʊà dz¿º|ÈwÉ

 2000 Prentice Hall, Inc. All rights reserved.

ÊmˤÌÍ ÎÏeÐ

ÑBÒ±Ó ÔÕ ÖÕ ×Ø ÕÙOÚ Û}×5ÜÝÕ ×Þ5Ø Ú³ß

ÑBÒ ÑŠàEÕ Ôáãâ5ä5Õ ÔÖÚ%ÜÖäŽå ÕØÚ

æÒ±ç}Ú%×èŽè×Ö×μé

fscanfê

æÒъë}ÜœÕ ÔÖ

ìÒ±ípØ ä5ß0ڎå ÕØÚ

ëBÜÝä5î5ÜÝ×5ïñðWòÖâòÖ

1 /* Fig. 11.7: fig11_07.c 2 Reading and printing a sequential file */ 3 #include <stdio.h> 4 5 int main() 6 { 7 int account; 8 char name[ 30 ]; 9 double balance; 10 FILE cfPtr; / cfPtr = clients.dat file pointer */ 11 12 if ( ( cfPtr = fopen( "clients.dat", "r" ) ) == NULL ) 13 printf( "File could not be opened\n" ); 14 else { 15 printf( "%-10s%-13s%s\n", "Account", "Name", "Balance" ); 16 fscanf( cfPtr, "%d%s%lf", &account, name, &balance ); 17 18 while ( !feof( cfPtr ) ) { 19 printf( "%-10d%-13s%7.2f\n", account, name, balance ); 20 fscanf( cfPtr, "%d%s%lf", &account, name, &balance ); 21 } 22 23 fclose( cfPtr ); 24 } 25 26 return 0; 27 }

Account Name Balance 100 Jones 24. 200 Doe 345. 300 White 0. 400 Stone -42. 500 Rich 224.

 2000 Prentice Hall, Inc. All rights reserved.

ÊmˤÌÍ ÎÏeÐ

ÑBÒ±Ó ÔÕ ÖÕ ×Ø ÕÙOÚ Û}×5ÜÝÕ ×Þ5Ø Ú³ß

æÒ±ðWâÚ%Ôóå ÕØÚ

æÒÑŠÓ ÔâòÖ}ô%õä³Õ ô%Ú

æÒæWö ô5×5Ôóå ÕØÚ%ß

ìÒ±ëBÜÝÕ ÔÖ

1 /* Fig. 11.8: fig11_08.c 2 Credit inquiry program */ 3 #include <stdio.h> 4 5 int main() 6 { 7 int request, account; 8 double balance; 9 char name[ 30 ]; 10 FILE *cfPtr;

11 12 if ( ( cfPtr = fopen( "clients.dat", "r" ) ) == NULL )

13 printf( "File could not be opened\n" ); 14 else {

15 printf( "Enter request\n" 16 " 1 - List accounts with zero balances\n"

17 " 2 - List accounts with credit balances\n" 18 " 3 - List accounts with debit balances\n" 19 " 4 - End of run\n? " );

20 scanf( "%d", &request ); 21

22 while ( request != 4 ) { 23 fscanf( cfPtr, "%d%s%lf", &account, name,

24 &balance ); 25 26 switch ( request ) {

27 case 1: 28 printf( "\nAccounts with zero "

29 "balances:\n" ); 30

31 while ( !feof( cfPtr ) ) { 32

 2000 Prentice Hall, Inc. All rights reserved.

ÊmˤÌÍ ÎÏeÐ

æÒæWö ô5×5Ôóå ÕØÚ%ß

ìÒ±ëBÜÝÕ ÔÖ

33 if ( balance == 0 )

34 printf( "%-10d%-13s%7.2f\n", 35 account, name, balance );

36 37 fscanf( cfPtr, "%d%s%lf",

38 &account, name, &balance ); 39 } 40

41 break; 42 case 2:

43 printf( "\nAccounts with credit " 44 "balances:\n" );

45 46 while ( !feof( cfPtr ) ) { 47

48 if ( balance < 0 ) 49 printf( "%-10d%-13s%7.2f\n",

50 account, name, balance ); 51

52 fscanf( cfPtr, "%d%s%lf", 53 &account, name, &balance );

54 } 55 56 break;

57 case 3: 58 printf( "\nAccounts with debit "

59 "balances:\n" ); 60

61 while ( !feof( cfPtr ) ) { 62 63 if ( balance > 0 )

64 printf( "%-10d%-13s%7.2f\n",

 2000 Prentice Hall, Inc. All rights reserved.

÷¨÷øúùüû ýÿþ @þ Lþ  þ  ý¶ý Ýþ

ÿý !œý#"%$&$('

)+*-,/.10-,32547698/:8<;=;>,/?@?BAC6(:D,

EGFIHKJ!J!LNMPORQTSTL>URV WXVYQ@UZV M[!L=]^M-M[!QB_`Vba7cLNWdU!Q@aXM_eLgfhVbJ!i

LNM[!Q>_jURHkMH

300 White 0.00 400 Jones 32.87 (old data in file)

If we want to change White’s name to Worthington,

300 White 0.00 400 Jones 32.

300 Worthington 0.00ones 32.

300 Worthington 0.

Data gets overwritten

 2000 Prentice Hall, Inc. All rights reserved.

÷¨÷øúùüû ýÿþ @þ Lþ  þ  ý¶ý Ýþ

ÿý hDÝýl"%$m$n$9'

o+p<qrtsvu=wwyx/z{q|5wy}-|5w

~G€ ‚‚Xƒ>„eƒ@ ‡†P„eƒ@ˆR„eƒ>‰7ƒ@ ‡†Šk†€Y‹= Œ€Y ‚€bŽYƒ@‰Š> !‰7‘k„eƒKƒK †“’!ŠK

€Y ^†\ƒK„` RŠKŽ=„eƒ>ˆR„eƒ@‰7ƒ> g†“Š7†“€Y‹>

  • 1 ” 34 • -890 –>—e˜T–@™Y™^ intš 7›/œ!gžPŸ! 7¡h¢T£R¤^ ¥n¥¢@¦e¢>§^ž¨š7¤^ ©¢>š ª §«£R¤bš7¬

 2000 Prentice Hall, Inc. All rights reserved.

÷¨÷ø 9 ûŽþ3® ÿý hDÝý

¯+°/±>²R³!´=μ#±@¶N¶y·>¸C¸d¹XºY»b·>¸

¼¾½‡¿¿XÀ%ÁÁd Ã\ÄK ÅN ÄKÆ\LJÈyÉYÀ%¿XÊKÉYÄyÁˁ ÌÎÍ\ÊKÆÌ=ÁÀ%ÇÉY¿%ÍN ÃÏPÌÐÍkÉYÊ@ƓÏ@ÍÑÊ7ÌÐÍ\À‡É

ÉYÀ%¿XÊKÉYÄyÁ

¼GÒnÃ\ÁÌLJÃ\Ì=Ç%¿Ó¿À%ÁÁÌDÊ®ÉYÀC¿XÊKÉYÄyÁd ÃÑÇÕÔΠÈÀ

¼GÖÇ%ÌDÇÕ¿ÇÃP×À ÂÃÁÓÀ‡ÉYÌÀ%ÄPËÕ ÌÐÍ\Ê@Æ\ÌRÄ7ÀCÁÓÌÎÉYÊkØNÂÙÃÏPÊ7ÌÐÍ\À‡É‡ÄkÇtÌÇ

¼GÖÇ%ÌDǏÚyÉYÀ%Åg Ê@ƓÁCÈ ØÛÁÓÌDÊ@ÉYÀ%ÄÜ¿XLJÃÝ×XÀ ÆkÚXÄyÇ%ÌÀCÄÜÊ@É^ÄyÀÈ À%ÌDÀ%ÄPËÕ ÌÎÍÊ>ÆÌ

Ê7ÅtÀÉYˁÉÞ Ìe Ã\ÏKß

à+áâTã!äbåKâTå>ægç“å@èŒéRê7ëYæ!ìîíë ï!å>èäYåKæ!ìgç\ðŒñåKòyó=ñeèRê

ô¾õtö÷gøÐöyùeúnû ükýmþû ýöÿ¨ùúeüöþÞûöPý ökù ú  ö “ÿ 0 200 300 400 500

byte offsets

}

} } } } } }

bytes

bytes

bytes

bytes

bytes

bytes

 2000 Prentice Hall, Inc. All rights reserved.

÷¨÷ø ±ýÿþ  þ û þT    @ýÝK hœý



! #"$ &%('*),+.-0/& "",1.243,56"+-71829 85;:$-< >=@?AB",1.5#:CEDF'

  • 0 8'2+/G 8H>H1.5I5J)KDMLN1.

O!PQ QR#SUTVSEW#X.TY,ZE[6S]^ZET`_baZdc inte0fhgji8k*lIm$nBoEpq l]rts,e6lEuvl

e0n]o^l;o^lBo^ihkMw x!y q q

kMlUzKihk|{#e^ighu<vlEe6n*o^lEuVwpl}v$n~lEnEg€ mlU{‚q l]ƒ„#u7v x}

n Iu`n;ƒ$i#u†vbso^nBƒkMlInI{#n]‡q l

 2000 Prentice Hall, Inc. All rights reserved.

ÊmˤÌÍ ÎÏeÐ

ÑBÒžÚ5å ÕÔ³Ú ß0Öܜòô%Ö

ÑBÒ ÑŠÓ ÔÕ ÖÕ ×Ø ÕٱڎÛB×ÜœÕ ×5ÞØ Ú

ÑBÒ æ ÓÔÕ ÖÕ ×Ø Õٱڎß0Öܜòô5Ö

æÒ±ðWâÚ%Ôóå ÕØÚ

æÒÑ 9Ÿ ÜÝÕ ÖÚ Öä åÕØÚ òßÕ Ôî òÔå ä5ܜï¶×%ÖÖÚ5èŽä5òÖâ5òÖ

ìÒ±ípØ ä5ß0ڎå ÕØÚ

1 /* Fig. 11.11: fig11_11.c 2 Creating a randomly accessed file sequentially */ 3 #include <stdio.h> 4 5 struct clientData { 6 int acctNum; 7 char lastName[ 15 ]; 8 char firstName[ 10 ]; 9 double balance;

10 };

11

12 int main()

13 {

14 int i;

15 struct clientData blankClient = { 0, "", "", 0.0 };

16 FILE *cfPtr;

17

18 if ( ( cfPtr = fopen( "credit.dat", "w" ) ) == NULL )

19 printf( "File could not be opened.\n" );

20 else {

21

22 for ( i = 1; i <= 100; i++ )

23 fwrite( &blankClient,

24 sizeof( struct clientData ), 1, cfPtr );

25

26 fclose( cfPtr );

27 }

28

29 return 0;

30 }

 2000 Prentice Hall, Inc. All rights reserved.

÷¨÷ø6 u¡+Ó D hþ Lþ ûŽþ  £¢{ Ð þ û þT3 

ÿý hDÝý

  • fseek

¤o¥¦&§,¨©ª «¦[¬¨&ª §ª 8®7¬d ª®§,¦Š¯9§™-°*¨&¬¦{±ª ©ª ±]¬d¨Xª §ª ®

  • fseek( myPtr, offset, symbolic_constant);
    • myPtr ²2³ ´μ ¶·¹¸º&·¹´K»™μ ¼¸
    • offset ½X¾À¿ ÁÂÄà ÅÆh¿ Ç¿ ÅÈKÃÅ¿ ÈǹÂɊÊÌË*¿ ƾÀ¿ ÉÆǑÁ ÅhÍÎÇ¿ ÅÈhÏ
    • symbolic_constant Ð&ÑÒÓÔÕ ÖÀÕ Ó_ÑרÓÙÓ*Õ ÚÖÀÕ ÛÓ×ÜÓ ÝÙ ÓÄÙ ÓÝdÞ9Õ Úß

ÖUÙ àá

  • SEEK_SET âXãUää å—ã,æ ç5è£æÌã2çæé äê_ë ì5ìZë ìêîí ïGï0ë ðä
  • SEEK_CUR ñXòUóó ô—ò,õ ö5÷£õÌò2öõøùZ÷ ÷Ìó5úõû üZøÀöõ¹ý üZúý ú$þ ýûó
  • SEEK_END ÿ

    

 2000 Prentice Hall, Inc. All rights reserved.

 ÎÏ 

!" ž$#&% '()#+*-,/.1032,

!"! 54 (3' ,/' 6 7 ' 8 9#;: 6 .1' 6 &<7 #&*

="9>@?#(A% ' 7 #

="! 54 (3?03,CB&6,/

="=@D .',/#+,/E+% ' 7 #

1 /* Fig. 11.12: fig11_12.c 2 Writing to a random access file */ 3 #include <stdio.h> 4 5 struct clientData { 6 int acctNum; 7 char lastName[ 15 ]; 8 char firstName[ 10 ]; 9 double balance; 10 };

11 12 int main()

13 { 14 FILE *cfPtr;

15 struct clientData client = { 0, "", "", 0.0 }; 16

17 if ( ( cfPtr = fopen( "credit.dat", "r+" ) ) == NULL ) 18 printf( "File could not be opened.\n" ); 19 else {

20 printf( "Enter account number" 21 " ( 1 to 100, 0 to end input )\n? " );

22 scanf( "%d", &client.acctNum ); 23

24 while ( client.acctNum != 0 ) { 25 printf( "Enter lastname, firstname, balance\n? " ); 26 fscanf( stdin, "%s%s%lf", client.lastName,

27 client.firstName, &client.balance ); 28 fseek( cfPtr, ( client.acctNum - 1 ) *

29 sizeof( struct clientData ), SEEK_SET ); 30 fwrite( &client, sizeof( struct clientData ), 1,

31 cfPtr ); 32 printf( "Enter account number\n? " );

 2000 Prentice Hall, Inc. All rights reserved.

 FG 

H"9IJ7 E&*-#;% ' 7 #

K.E&L&. 6 &MN>@03,/?03,

33 scanf( "%d", &client.acctNum );

34 }

35

36 fclose( cfPtr );

37 }

38

39 return 0;

40 }

Enter account number (1 to 100, 0 to end input) ? 37 Enter lastname, firstname, balance ? Barker Doug 0. Enter account number ? 29 Enter lastname, firstname, balance ? Brown Nancy -24. Enter account number ? 96 Enter lastname, firstname, balance ? Stone Sam 34.

 2000 Prentice Hall, Inc. All rights reserved.

 FG 

!"9â$#&% '()#+*-,/.1032,

!"! 54 (3' ,/' 6 7 ' 8 9#;: 6 .1' 6 &<7 #&*

="9ãC#6BÀä freadå

="!5KC.1' (“,

1 /* Fig. 11.15: fig11_15.c 2 Reading a random access file sequentially */ 3 #include <stdio.h> 4 5 struct clientData { 6 int acctNum; 7 char lastName[ 15 ]; 8 char firstName[ 10 ]; 9 double balance; 10 };

11 12 int main()

13 { 14 FILE *cfPtr;

15 struct clientData client = { 0, "", "", 0.0 }; 16

17 if ( ( cfPtr = fopen( "credit.dat", "r" ) ) == NULL ) 18 printf( "File could not be opened.\n" ); 19 else {

20 printf( "%-6s%-16s%-11s%10s\n", "Acct", "Last Name", 21 "First Name", "Balance" );

22 23 while ( !feof( cfPtr ) ) {

24 fread( &client, sizeof( struct clientData ), 1, 25 cfPtr ); 26

27 if ( client.acctNum != 0 ) 28 printf( "%-6d%-16s%-11s%10.2f\n",

29 client.acctNum, client.lastName, 30 client.firstName, client.balance );

31 } 32

 2000 Prentice Hall, Inc. All rights reserved.

 FG 

H"9IJ7 E&*-#;% ' 7 #

K.E&L&. 6 &MN>@03,/?03,

33 fclose( cfPtr );

36 return 0;

37 }

Acct Last Name First Name Balance 29 Brown Nancy -24. 33 Dunn Stacey 314. 37 Barker Doug 0. 88 Smith Dave 258. 96 Stone Sam 34.

 2000 Prentice Hall, Inc. All rights reserved.

OPOQ3OJæUçáèéW\sëêdk VtìAuîí@orWï];x¨WZvXc)[ qt]ñðPorqbvÞVbxJx[]_^

ðPorqX^\o9W\s

òôóöõ÷áõùøúáûýü@þ ÿ ú  ÷¨õõ ÷¨õ pþ ú  ÷ {÷ û@õ púáû Êú  ÷áõõ

 øþ  ÷¨õõ û þ¸úýúáû õùú  (^) þ@û  ûCþ øÿïú  þ û

ò  ÷! "#

$&%'()+-, ,/.0 1 2305467)+889;: 4 *-

$=<((>4,/?@)+889;: 4 *-

$&AB,BC ,/-,)+889D: 4 -

$=EF9DG#, ) HI9;GKJL)+--,+(MC 01 230 4 679HN)C C)/88O9N: 4 *-1054P) F,/.QHR0 C,

 2000 Prentice Hall, Inc. All rights reserved.

 FG 

!"9â$#&% '()#+*-,/.1032,

!"! S903(32&,/' E&( ?&. E,/E,5TC?&#&*

!"= 4 (3' ,/' 6 7 ' 8 9#;: 6 .1' 6 &<7 #&*

!"H VU '(W ?&E&' (3,/#.36&(3B E?&#(A% ' 7 #

=" 94 (3?03,C2YX3E&' 2 &#

1 /* Fig. 11.16: fig11_16.c 2 This program reads a random access file sequentially, 3 updates data already written to the file, creates new 4 data to be placed in the file, and deletes data 5 already in the file. */ 6 #include <stdio.h> 7 8 struct clientData { 9 int acctNum; 10 char lastName[ 15 ];

11 char firstName[ 10 ]; 12 double balance;

13 }; 14 15 int enterChoice( void );

16 void textFile( FILE * ); 17 void updateRecord( FILE * );

18 void newRecord( FILE * ); 19 void deleteRecord( FILE * );

20 21 int main()

22 { 23 FILE *cfPtr; 24 int choice;

25 26 if ( ( cfPtr = fopen( "credit.dat", "r+" ) ) == NULL )

27 printf( "File could not be opened.\n" ); 28 else {

29 30 while ( ( choice = enterChoice() ) != 5 ) { 31

32 switch ( choice ) {

 2000 Prentice Hall, Inc. All rights reserved.

 FG 

H"! S903(32&,/' E&(AB#&% '(3' ,/' E&(3*

97 printf( "%-6d%-16s%-11s%10.2f\n\n",

98 client.acctNum, client.lastName,

99 client.firstName, client.balance );

100 printf( "Enter charge ( + ) or payment ( - ): " );

101 scanf( "%lf", &transaction );

102 client.balance += transaction;

103 printf( "%-6d%-16s%-11s%10.2f\n",

104 client.acctNum, client.lastName,

105 client.firstName, client.balance );

106 fseek( fPtr,

107 ( account - 1 ) * sizeof( struct clientData ),

108 SEEK_SET );

109 fwrite( &client, sizeof( struct clientData ), 1,

110 fPtr );

111 }

112 }

113

114 void deleteRecord( FILE *fPtr )

115 {

116 struct clientData client,

117 blankClient = { 0, "", "", 0 };

118 int accountNum;

119

120 printf( "Enter account number to "

121 "delete ( 1 - 100 ): " );

122 scanf( "%d", &accountNum );

123 fseek( fPtr,

124 ( accountNum - 1 ) * sizeof( struct clientData ),

125 SEEK_SET );

126 fread( &client, sizeof( struct clientData ), 1, fPtr );

 2000 Prentice Hall, Inc. All rights reserved.

 FG 

H"! S903(32&,/' E&(AB#&% '(3' ,/' E&(3*

128 if ( client.acctNum == 0 )

129 printf( "Account %d does not exist.\n", accountNum );

130 else {

131 fseek( fPtr,

132 ( accountNum - 1 ) * sizeof( struct clientData ),

133 SEEK_SET );

134 fwrite( &blankClient,

135 sizeof( struct clientData ), 1, fPtr );

136 }

137 }

138

139 void newRecord( FILE *fPtr )

140 {

141 struct clientData client = { 0, "", "", 0.0 };

142 int accountNum;

143 printf( "Enter new account number ( 1 - 100 ): " );

144 scanf( "%d", &accountNum );

145 fseek( fPtr,

146 ( accountNum - 1 ) * sizeof( struct clientData ),

147 SEEK_SET );

148 fread( &client, sizeof( struct clientData ), 1, fPtr );

149

150 if ( client.acctNum != 0 )

151 printf( "Account #%d already contains information.\n",

152 client.acctNum );

153 else {

154 printf( "Enter lastname, firstname, balance\n? " );

155 scanf( "%s%s%lf", &client.lastName, &client.firstName,

156 &client.balance );

 2000 Prentice Hall, Inc. All rights reserved.

 FG 

H"! S903(32&,/' E&(AB#&% '(3' ,/' E&(3*

157 client.acctNum = accountNum;

158 fseek( fPtr, ( client.acctNum - 1 ) *

159 sizeof( struct clientData ), SEEK_SET );

160 fwrite( &client,

161 sizeof( struct clientData ), 1, fPtr );

162 }

163 }

164

165 int enterChoice( void )

166 {

167 int menuChoice;

168

169 printf( "\nEnter your choice\n"

170 "1 - store a formatted text file of acounts called\n"

171 " "accounts.txt" for printing\n"

172 "2 - update an account\n"

173 "3 - add a new account\n"

174 "4 - delete an account\n"

175 "5 - end program\n? " );

176 scanf( "%d", &menuChoice );

177 return menuChoice;

178 }

 2000 Prentice Hall, Inc. All rights reserved.

 FG 

K.E&L&. 6 &MN>@03,/?03,

Enter account to update (1 - 100): 37 37 Barker Doug 0.

Enter charge (+) or payment (-): +87. 37 Barker Doug 87.

Enter new account number (1 - 100): 22 Enter lastname, firstname, balance ? Johnston Sarah 247.

After choosing option 1 accounts.txt contains:

Acct Last Name First Name Balance 29 Brown Nancy -24. 33 Dunn Stacey 314. 37 Barker Doug 0. 88 Smith Dave 258. 96 Stone Sam 34.