]> cloudbase.mooo.com Git - z180-stamp.git/blob - fatfs/doc/en/lseek.html
Import fatfs R0.12b
[z180-stamp.git] / fatfs / doc / en / lseek.html
1 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
2 <html lang="en">
3 <head>
4 <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
5 <meta http-equiv="Content-Style-Type" content="text/css">
6 <link rel="up" title="FatFs" href="../00index_e.html">
7 <link rel="alternate" hreflang="ja" title="Japanese" href="../ja/lseek.html">
8 <link rel="stylesheet" href="../css_e.css" type="text/css" media="screen" title="ELM Default">
9 <title>FatFs - f_lseek</title>
10 </head>
11
12 <body>
13
14 <div class="para func">
15 <h2>f_lseek</h2>
16 <p>The f_lseek function moves the file read/write pointer of an open file object. It can also be used to expand the file size (cluster pre-allocation). </p>
17
18 <pre>
19 FRESULT f_lseek (
20 FIL* <span class="arg">fp</span>, <span class="c">/* [IN] File object */</span>
21 FSIZE_t <span class="arg">ofs</span> <span class="c">/* [IN] File read/write pointer */</span>
22 );
23 </pre>
24 </div>
25
26 <div class="para arg">
27 <h4>Parameters</h4>
28 <dl class="par">
29 <dt>fp</dt>
30 <dd>Pointer to the open file object.</dd>
31 <dt>ofs</dt>
32 <dd>Byte offset from top of the file. The data type <tt>FSIZE_t</tt> is an alias of either <tt>DWORD</tt>(32-bit) or <tt>QWORD</tt>(64-bit) depends on the configuration option <tt>_FS_EXFAT</tt>.</dd>
33 </dl>
34 </div>
35
36
37 <div class="para ret">
38 <h4>Return Values</h4>
39 <p>
40 <a href="rc.html#ok">FR_OK</a>,
41 <a href="rc.html#de">FR_DISK_ERR</a>,
42 <a href="rc.html#ie">FR_INT_ERR</a>,
43 <a href="rc.html#io">FR_INVALID_OBJECT</a>,
44 <a href="rc.html#tm">FR_TIMEOUT</a>
45 </p>
46 </div>
47
48
49 <div class="para desc">
50 <h4>Description</h4>
51 <p>The <tt>f_lseek</tt> function moves the file read/write pointer of an open file. The offset can be specified in only origin from top of the file. When an offset beyond the file size is specified at write mode, the file size is expanded to the specified offset. The file data in the expanded area is <em>undefined</em> because no data is written to the file in this process. This is suitable to pre-allocate a cluster chain quickly, for fast write operation. When a contiguous data area needs to be allocated to the file, use <tt>f_expand</tt> function instead. After the <tt>f_lseek</tt> function succeeded, the current read/write pointer should be checked in order to make sure the read/write pointer has been moved correctry. In case of the read/write pointer is not the expected value, either of followings has been occured.</p>
52 <ul>
53 <li>End of file. The specified <tt class="arg">ofs</tt> was clipped at end of the file because the file has been opened in read-only mode.</li>
54 <li>Disk full. There is no free space on the volume to expand the file.</li>
55 </ul>
56 <p>The fast seek function enables fast backward/long seek operations without FAT access by using an on-memory CLMT (cluster link map table). It is applied to <tt>f_read</tt> and <tt>f_write</tt> function as well, however, the file size cannot be expanded by <tt>f_write</tt>, <tt>f_lseek</tt> function while the file is in fast seek mode.</p>
57 <p>The fast seek function is enabled when the member <tt>cltbl</tt> in the file object is not NULL. The CLMT must be created into the <tt>DWORD</tt> array prior to use the fast seek function. To create the CLMT, set address of the <tt>DWORD</tt> array to the member <tt>cltbl</tt> in the open file object, set the size of array in unit of items to the first item and call the <tt>f_lseek</tt> function with <tt class="arg">ofs</tt><tt> = CREATE_LINKMAP</tt>. After the function succeeded and CLMT is created, no FAT access is occured in subsequent <tt>f_read</tt>, <tt>f_write</tt>, <tt>f_lseek</tt> function to the file. The number of items used or required is returned into the first item of the array. The number of items to be used is (number of the file fragments + 1) * 2. For example, when the file is fragmented in 5, 12 items in the array will be used. If the function failed with <tt>FR_NOT_ENOUGH_CORE</tt>, the given array size is insufficient for the file.</p>
58 </div>
59
60
61 <div class="para comp">
62 <h4>QuickInfo</h4>
63 <p>Available when <tt>_FS_MINIMIZE &lt;= 2</tt>. To use fast seek function, <tt>_USE_FASTSEEK</tt> needs to be set 1.</p>
64 </div>
65
66
67 <div class="para use">
68 <h4>Example</h4>
69 <pre>
70 <span class="c">/* Open file */</span>
71 fp = malloc(sizeof (FIL));
72 res = f_open(fp, "file.dat", FA_READ|FA_WRITE);
73 if (res) ...
74
75 <span class="c">/* Move to offset of 5000 from top of the file */</span>
76 res = f_lseek(fp, 5000);
77
78 <span class="c">/* Move to end of the file to append data */</span>
79 res = f_lseek(fp, f_size(fp));
80
81 <span class="c">/* Forward 3000 bytes */</span>
82 res = f_lseek(fp, f_tell(fp) + 3000);
83
84 <span class="c">/* Rewind 2000 bytes (take care on wraparound) */</span>
85 res = f_lseek(fp, f_tell(fp) - 2000);
86 </pre>
87 <pre>
88 <span class="c">/* Cluster pre-allocation (to prevent buffer overrun on streaming write) */</span>
89
90 res = f_open(fp, recfile, FA_CREATE_NEW | FA_WRITE); <span class="c">/* Create a file */</span>
91
92 res = f_lseek(fp, PRE_SIZE); <span class="c">/* Expand file size (cluster pre-allocation) */</span>
93 if (res || f_tell(fp) != PRE_SIZE) ... <span class="c">/* Check if the file has been expanded */</span>
94
95 res = f_lseek(fp, DATA_START); <span class="c">/* Record data stream WITHOUT cluster allocation delay */</span>
96 ... <span class="c">/* Write operation should be aligned to sector boundary to optimize the write throughput */</span>
97
98 res = f_truncate(fp); <span class="c">/* Truncate unused area */</span>
99 res = f_lseek(fp, 0); <span class="c">/* Put file header */</span>
100 ...
101
102 res = f_close(fp);
103 </pre>
104 <pre>
105 <span class="c">/* Using fast seek function */</span>
106
107 DWORD clmt[SZ_TBL]; <span class="c">/* Cluster link map table buffer */</span>
108
109 res = f_open(fp, fname, FA_READ | FA_WRITE); <span class="c">/* Open a file */</span>
110
111 res = f_lseek(fp, ofs1); <span class="c">/* This is normal seek (cltbl is nulled on file open) */</span>
112
113 fp-&gt;cltbl = clmt; <span class="c">/* Enable fast seek function (cltbl != NULL) */</span>
114 clmt[0] = SZ_TBL; <span class="c">/* Set table size */</span>
115 res = f_lseek(fp, CREATE_LINKMAP); <span class="c">/* Create CLMT */</span>
116 ...
117
118 res = f_lseek(fp, ofs2); <span class="c">/* This is fast seek */</span>
119 </pre>
120 </div>
121
122
123 <div class="para ref">
124 <h4>See Also</h4>
125 <p><tt><a href="open.html">f_open</a>, <a href="truncate.html">f_truncate</a>, <a href="expand.html">f_expand</a>, <a href="sfile.html">FIL</a></tt></p>
126 </div>
127
128 <p class="foot"><a href="../00index_e.html">Return</a></p>
129 </body>
130 </html>