18-2 �}�Ҥ������ɮ�

µL½×¬O­nŪ¼g ASCII ©Î¤G¶i¨îÀɮסA³£¥²»Ý¥ý¥Î fopen ¨ç¼Æ¨Ó¶}±ÒÀɮסA¨ä¨Ï¥Î»yªk¦p¤U¡G

fid = fopen(filename, permission)

¨ä¤¤ filename ªí¥Ü±ýŪ¼gªºÀɮצWºÙ¡Apermission «hªí¥Ü±ý¹ïÀɮ׶i¦æªº³B²z¤è¦¡¡A¥i¥H¬O¤U¦C¥ô¤@¦r¦ê¡G

¦pªG§Ú­Ì¬Ù²¤ fopen ªº²Ä¤G­Ó¿é¤J¤Þ¼Æ¡A«h permission ªº¹w³]­È´N¬O ¡¥r¡¦¡C

Hint
  • ¦b·L³n¤½¥qªºµøµ¡§@·~¨t²Î¤U¡Apermission ¦r¦ê¥²»Ý¯à°÷¤À¿ë¤G¶i¨îÀɮשΠASCII ÀɮסC¨Ò¦p¡A­Y­nŪ¨ú¤G¶i¨îÀɮסA«h permission ¦r¦ê¥²»Ý¬O¡§rb¡¨¡A±ýª¾¸Ô±¡¡A¥i¦b MATLAB «ü¥Oµøµ¡«á¡A¿é¤J¡uhelp fopen¡v¡C
  • ¨Æ¹ê¤W¡Afopen «ü¥O¤ä´©§ó¦hªº permission ¦r¦ê¡AŪªÌ¥i¿é¤J ¡uhelp fopen¡v ±o¨ì§¹¾ãªº¸ê°T¡C

fopen ¨ç¼Æ¶Ç¦^¤@­ÓÀɮ׿ëÃѽX¡]File Identifier¡^¡A³q±`¬O¤@­Ó«D­tªº¾ã¼Æ¡A§Ú­Ì¥i¥Î¦¹¿ëÃѽX¨Ó¹ï¦¹Àɮ׶i¦æ¦UºØŪ¼gªº³B²z¡C¦pªG¶Ç¦^ªºÀɮ׿ëÃѽX¬O -1¡A«h¥Nªí fopen µLªk¶}±ÒÀɮסA¨ä­ì¦]¥i¯à¬OÀɮפ£¦s¦b¡A©Î¬O¨Ï¥ÎªÌÅv­­¤£¨¬¦ÓµLªk¶}±Ò¦¹ÀɮסC­Y±ýª¾µLªk¶}±ÒÀɮתº­ì¦]¡A¥iÅý fopen ¶Ç¦^²Ä¤G­Ó¤Þ¼Æ¨ÓÅã¥Ü¿ù»~°T®§¡C¬°¤F¦w¥þ°_¨£¡A³Ì¦n¦b¨C¦¸¨Ï¥Î fopen ®É¡A³£´ú¸Õ¨ä¶Ç¦^ªº fid ¬O§_¬°¦³®Ä­È¡C¦b¤U¦C½d¨Ò¤¤¡A§Ú­Ì¸ÕµÛ¶}±Ò¤@­Ó¤£¦s¦bªºÀɮסA¨Ã¦L¥X¦^¶Çªº°T®§¡G

Example 1: 18-ÀÉ®×Ū¼g/fopen01.m[fid, message] = fopen('no_such_file', 'r'); fprintf('fid = %d\n', fid); fprintf('message = %s\n', message);fid = -1 message = No such file or directory

¥Ñ¤W­z½d¨Ò¥iª¾·íÀɮפ£¦s¦b®É¡A¦^¶Çªº fid ¬O -1¡A¦P®É message ·|¥]§t¬ÛÃöªº¿ù»~¸ê°T¡C­Y¶}±Ò¦¨¥\¡A«h¶Ç¦^ªº fid ¬O¤@­Ó¤j©ó 2 ªº¾ã¼Æ¡A¦Ó¥B¶Ç¦^ªº message ¬O¤@­ÓªÅ¦r¦ê¡A¨Ò¦p¡G

Example 2: 18-ÀÉ®×Ū¼g/fopen02.m[fid, message] = fopen('fopen02.m', 'r'); fprintf('fid = %d\n', fid); fprintf('message = %s\n', message); fid = 5 message =

Hint
MATLAB ¨Ï¥Î fid=1 ¨Ó¥Nªí¡u¼Ð·Ç¿é¥X¡v¡]Standard Output¡^¡Afid=2 ¥Nªí¡u¼Ð·Ç¿ù»~¿é¥X¡v¡]Standard Error¡^,¦]¦¹¦b¨Ï¥Î³o¨â­Ó fid ªº­È®É¡A¥i¥H¤£¥²¨Ï¥Î fopen ¨Ó¶}±ÒÀɮסC

¤@¥¹§¹¦¨ÀɮתºÅª¼g¤§«á¡A¥i¥Î fclose ¨ç¼Æ¨ÓÃö³¬ÀɮסA¨ä¨Ï¥Î»yªk¦p¤U¡G

status = fclose(fid);

­Y¤@¤Á¶¶§Q¡Afclose ¶Ç¦^ 0¡C­YµLªk¶¶§QÃö³¬ÀɮסA«h fclose ¶Ç¦^ -1¡C¤@¯ë±¡ªp¤U¡A¦b§¹¦¨ÀɮתºÅª¼g«á¡A§YÀ³¨Ï¥Î fclose ¨ÓÃö³¬ÀɮסA¥H§K¶}±ÒÀÉ®×¹L¦h¡A³y¦¨¨t²Î¸ê·½ªº®ö¶O¡C

­Y­n¤@¦¸Ãö³¬©Ò¦³¶}±ÒªºÀɮסA¥i¥Î fclose('all') ©Î¬O fclose all¡C

Hint
¶}±Ò¤ÎÃö³¬Àɮ׳£¬O¤ñ¸û¯Ó®Éªº¨ç¼Æ¡A¦]¦¹ºÉ¶q¤£­n±N¥L­Ì¸m©ó°j°é¤§¤¤¡A¥H´£°ªµ{¦¡°õ¦æ®Ä²v¡C


MATLABµ{¦¡³]­p¡G¤Jªù½g