Mistake on this page?
Report an issue in GitHub or email us
File.h
1 /* mbed Microcontroller Library
2  * Copyright (c) 2015 ARM Limited
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #ifndef FILE_H
18 #define FILE_H
19 
20 #include "features/storage/filesystem/FileSystem.h"
21 #include "platform/FileHandle.h"
22 
23 namespace mbed {
24 /** \addtogroup filesystem */
25 /** @{*/
26 
27 
28 /** File class
29  */
30 class File : public FileHandle {
31 public:
32  /** Create an uninitialized file
33  *
34  * Must call open to initialize the file on a file system
35  */
36  File();
37 
38  /** Create a file on a filesystem
39  *
40  * Creates and opens a file on a filesystem
41  *
42  * @param fs Filesystem as target for the file
43  * @param path The name of the file to open
44  * @param flags The flags to open the file in, one of O_RDONLY, O_WRONLY, O_RDWR,
45  * bitwise or'd with one of O_CREAT, O_TRUNC, O_APPEND
46  */
47  File(FileSystem *fs, const char *path, int flags = O_RDONLY);
48 
49  /** Destroy a file
50  *
51  * Closes file if the file is still open
52  */
53  virtual ~File();
54 
55  /** Open a file on the filesystem
56  *
57  * @param fs Filesystem as target for the file
58  * @param path The name of the file to open
59  * @param flags The flags to open the file in, one of O_RDONLY, O_WRONLY, O_RDWR,
60  * bitwise or'd with one of O_CREAT, O_TRUNC, O_APPEND
61  * @return 0 on success, negative error code on failure
62  */
63  virtual int open(FileSystem *fs, const char *path, int flags = O_RDONLY);
64 
65  /** Close a file
66  *
67  * @return 0 on success, negative error code on failure
68  */
69  virtual int close();
70 
71  /** Read the contents of a file into a buffer
72  *
73  * @param buffer The buffer to read in to
74  * @param size The number of bytes to read
75  * @return The number of bytes read, 0 at end of file, negative error on failure
76  */
77 
78  virtual ssize_t read(void *buffer, size_t size);
79 
80  /** Write the contents of a buffer to a file
81  *
82  * @param buffer The buffer to write from
83  * @param size The number of bytes to write
84  * @return The number of bytes written, negative error on failure
85  */
86  virtual ssize_t write(const void *buffer, size_t size);
87 
88  /** Flush any buffers associated with the file
89  *
90  * @return 0 on success, negative error code on failure
91  */
92  virtual int sync();
93 
94  /** Check if the file in an interactive terminal device
95  *
96  * @return True if the file is a terminal
97  */
98  virtual int isatty();
99 
100  /** Move the file position to a given offset from from a given location
101  *
102  * @param offset The offset from whence to move to
103  * @param whence The start of where to seek
104  * SEEK_SET to start from beginning of file,
105  * SEEK_CUR to start from current position in file,
106  * SEEK_END to start from end of file
107  * @return The new offset of the file
108  */
109  virtual off_t seek(off_t offset, int whence = SEEK_SET);
110 
111  /** Get the file position of the file
112  *
113  * @return The current offset in the file
114  */
115  virtual off_t tell();
116 
117  /** Rewind the file position to the beginning of the file
118  *
119  * @note This is equivalent to file_seek(file, 0, FS_SEEK_SET)
120  */
121  virtual void rewind();
122 
123  /** Get the size of the file
124  *
125  * @return Size of the file in bytes
126  */
127  virtual off_t size();
128 
129  /** Truncate or extend a file.
130  *
131  * The file's length is set to the specified value. The seek pointer is
132  * not changed. If the file is extended, the extended area appears as if
133  * it were zero-filled.
134  *
135  * @param length The requested new length for the file
136  *
137  * @return Zero on success, negative error code on failure
138  */
139  virtual int truncate(off_t length);
140 
141 private:
142  FileSystem *_fs;
143  fs_file_t _file;
144 };
145 
146 
147 /** @}*/
148 } // namespace mbed
149 
150 #endif
virtual ~File()
Destroy a file.
virtual int truncate(off_t length)
Truncate or extend a file.
virtual int sync()
Flush any buffers associated with the file.
Class FileHandle.
Definition: FileHandle.h:46
virtual int isatty()
Check if the file in an interactive terminal device.
virtual void rewind()
Rewind the file position to the beginning of the file.
virtual off_t size()
Get the size of the file.
virtual int open(FileSystem *fs, const char *path, int flags=O_RDONLY)
Open a file on the filesystem.
File class.
Definition: File.h:30
A file system object.
Definition: FileSystem.h:49
virtual ssize_t read(void *buffer, size_t size)
Read the contents of a file into a buffer.
File()
Create an uninitialized file.
virtual int close()
Close a file.
virtual off_t seek(off_t offset, int whence=SEEK_SET)
Move the file position to a given offset from from a given location.
virtual ssize_t write(const void *buffer, size_t size)
Write the contents of a buffer to a file.
virtual off_t tell()
Get the file position of the file.
Important Information for this Arm website

This site uses cookies to store information on your computer. By continuing to use our site, you consent to our cookies. If you are not happy with the use of these cookies, please review our Cookie Policy to learn how they can be disabled. By disabling cookies, some features of the site will not work.