Sie sind auf Seite 1von 4

fopen

Open file, or obtain information about open files

Syntax
f i l e I D=f o p e n ( f i l e n a m e ) f i l e I D=f o p e n ( f i l e n a m e ,p e r m i s s i o n ) f i l e I D=f o p e n ( f i l e n a m e ,p e r m i s s i o n ,m a c h i n e f o r m a t ) f i l e I D=f o p e n ( f i l e n a m e ,p e r m i s s i o n ,m a c h i n e f o r m a t ,e n c o d i n g ) [ f i l e I D ,m e s s a g e ]=f o p e n ( f i l e n a m e ,. . . ) f I D s=f o p e n ( ' a l l ' ) [ f i l e n a m e ,p e r m i s s i o n ,m a c h i n e f o r m a t ,e n c o d i n g ]=f o p e n ( f i l e I D )

Description
f i l e I D=f o p e n ( f i l e n a m e )opens the file f i l e n a m efor read access, and returns an integer file identifier. f i l e I D=f o p e n ( f i l e n a m e ,p e r m i s s i o n )opens the file w ith the specified p e r m i s s i o n . f i l e I D=f o p e n ( f i l e n a m e ,p e r m i s s i o n ,m a c h i n e f o r m a t )specifies the order for reading or w riting bytes or bits in the file. f i l e I D=f o p e n ( f i l e n a m e ,p e r m i s s i o n ,m a c h i n e f o r m a t ,e n c o d i n g )specifies the character encoding scheme associated w ith the file. [ f i l e I D ,m e s s a g e ]=f o p e n ( f i l e n a m e ,. . . )opens a file. If the operation fails, m e s s a g eis a system-dependent error message. Otherw ise, m e s s a g eis an empty string. f I D s=f o p e n ( ' a l l ' )returns a row vector containing the file identifiers of all open files. [ f i l e n a m e ,p e r m i s s i o n ,m a c h i n e f o r m a t ,e n c o d i n g ]=f o p e n ( f i l e I D )returns the file name, permission, machine format, and encoding that a previous call to f o p e nused w hen it opened the specified file. f o p e ndoes not read information from the file to determine these output values. An invalid f i l e I Dreturns empty strings for all output arguments.

Input Arguments
f i l e n a m e String in single quotation marks that specifies the name of the file to open. Can include a full or partial path. On UNIX systems, if f i l e n a m ebegins w ith ' ~ / 'or ' ~ u s e r n a m e / ' , the f o p e nfunction expands the path to the current or specified user's home directory, respectively. If you open a file w ith read access and f o p e ncannot find f i l e n a m ein the current folder, f o p e nsearches along the MATLAB search path. Otherw ise, f o p e ncreates a file in the current directory. p e r m i s s i o n String that describes the type of access for the file: read, w rite, append, or update. Also specifies w hether to open files in binary or text mode. To open files in binary mode, specify one of the follow ing:

' r ' ' w '

Open file for reading (default). Open or create new file for w riting. Discard existing contents, if any.

' a ' ' r + ' ' w + ' ' a + ' ' A ' ' W '

Open or create new file for w riting. Append data to the end of the file. Open file for reading and w riting. Open or create new file for reading and w riting. Discard existing contents, if any. Open or create new file for reading and w riting. Append data to the end of the file. Append w ithout automatic flushing. (Used w ith tape drives.) Write w ithout automatic flushing. (Used w ith tape drives.)

To read and w rite to the same file: Open the file in update mode (w ith a p e r m i s s i o nthat includes a plus sign, ' + ' ). Call f s e e kor f r e w i n dbetw een read and w rite operations. For example, do not call f r e a dfollow ed by f w r i t e , or f w r i t efollow ed by f r e a d , unless you call f s e e kor f r e w i n dbetw een them. To open files in text mode, attach the letter ' t 'to the p e r m i s s i o n , such as ' r t 'or ' w t + ' . For better performance, do not use text mode. The follow ing applies on Window s systems, in text mode: Read operations that encounter a carriage return follow ed by a new line character ( ' \ r \ n ' ) remove the carriage return from the input. Write operations insert a carriage return before any new line character in the output. This additional processing is unnecessary for most cases. All MATLAB import functions, and most text editors (including Microsoft Word and WordPad), recognize both ' \ r \ n 'and ' \ n 'as new line sequences. How ever, w hen you create files for use in Microsoft Notepad, end each line w ith ' \ r \ n ' . For an example, see f p r i n t f . m a c h i n e f o r m a t String that specifies the order for reading or w riting bytes or bits in the file. Possible values are:

' n 'or ' n a t i v e ' ' b 'or ' i e e e b e ' ' l 'or ' i e e e l e ' ' s 'or ' i e e e b e . l 6 4 ' ' a 'or ' i e e e l e . l 6 4 '

The byte ordering that your system uses (default) Big-endian ordering Little-endian ordering Big-endian ordering, 64-bit long data type Little-endian ordering, 64-bit long data type

By default, all currently supported platforms use little-endian ordering for new files. Existing binary files can use either big- or little-endian ordering.

e n c o d i n g

String that specifies the character encoding scheme to use for subsequent read and w rite operations, including f s c a n f ,f p r i n t f ,f g e t l ,f g e t s ,f r e a d , and f w r i t e . Supported values are:

' B i g 5 ' ' E U C J P ' ' G B K ' ' M a c i n t o s h ' ' S h i f t _ J I S ' ' U S A S C I I ' ' U T F 8 '

' I S O 8 8 5 9 1 ' ' I S O 8 8 5 9 2 ' ' I S O 8 8 5 9 3 ' ' I S O 8 8 5 9 4 ' ' I S O 8 8 5 9 9 ' ' I S O 8 8 5 9 1 3 ' ' I S O 8 8 5 9 1 5 '

' w i n d o w s 9 3 2 ' ' w i n d o w s 9 3 6 ' ' w i n d o w s 9 4 9 ' ' w i n d o w s 9 5 0 ' ' w i n d o w s 1 2 5 0 ' ' w i n d o w s 1 2 5 1 ' ' w i n d o w s 1 2 5 2 ' ' w i n d o w s 1 2 5 3 ' ' w i n d o w s 1 2 5 4 ' ' w i n d o w s 1 2 5 7 '

For a list of additional encoding character sets, see http://w w w .iana.org/assignments/character-sets. If you specify a value for encoding that is not in the list of supported values, MATLAB issues a w arning. Specifying other encodings sometimes (but not alw ays) produces correct results. Default: system-dependent

Output Arguments
f i l e I D An integer that identifies the file for all subsequent low -level file I/O operations. If f o p e ncannot open the file, f i l e I Dis 1 . MATLAB reserves file identifiers 0 ,1 , and 2for standard input, standard output (the screen), and standard error, respectively. When f o p e nsuccessfully opens a file, it returns a file identifier greater than or equal to 3 . m e s s a g e f I D s A system-dependent error message w hen f o p e ncannot open the specified file. Otherw ise, an empty string. Row vector containing the identifiers for all open files, except the identifiers reserved for standard input, output, and error. The number of elements in the vector is equal to the number of open files. f i l e n a m e p e r m i s s i o n Name of the file associated w ith the specified f i l e I D . The p e r m i s s i o nthat f o p e nassigned to the file specified by f i l e I D .

m a c h i n e f o r m a t e n c o d i n g

The value of m a c h i n e f o r m a tthat f o p e nused w hen it opened the file specified by f i l e I D . The character encoding scheme that f o p e nassociated w ith the file specified by f i l e I D . The value that f o p e nreturns for e n c o d i n gis a standard character encoding scheme name. It is not alw ays the same as the e n c o d i n gargument that you used in the call to f o p e nto open the file.

Examples
Open a file. Pass the file identifier, f i d , to other file I/O functions to read data and close the file. f i d=f o p e n ( ' f g e t l . m ' ) ; t l i n e=f g e t l ( f i d ) ; w h i l ei s c h a r ( t l i n e ) d i s p ( t l i n e ) ; t l i n e=f g e t l ( f i d ) ; e n d f c l o s e ( f i d ) ;

Create a prompt to request the name of a file to open. If f o p e ncannot open the file, display the relevant error message. f i d=1 ; m s g=' ' ; w h i l ef i d<0 d i s p ( m s g ) ; f i l e n a m e=i n p u t ( ' O p e nf i l e :' ,' s ' ) ; [ f i d , m s g ]=f o p e n ( f i l e n a m e ) ; e n d

Open a file to w rite Unicode characters to a file using the Shift-JIS character encoding scheme: f i d=f o p e n ( ' j a p a n e s e _ o u t . t x t ' ,' w ' ,' n ' ,' S h i f t _ J I S ' ) ;

See Also
f c l o s e| f e o f| f e r r o r| f p r i n t f| f r e a d| f s c a n f| f s e e k| f t e l l| f w r i t e

Was this topic helpful?

Yes

No

Das könnte Ihnen auch gefallen