blob: 3639f643d23eca5ff3c218f937896568ced926f3 [file] [log] [blame]
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<meta http-equiv="Content-Style-Type" content="text/css">
<link rel="up" title="FatFs" href="../00index_e.html">
<link rel="stylesheet" href="../css_e.css" type="text/css" media="screen" title="ELM Default">
<title>FatFs - f_lseek</title>
</head>
<body>
<div class="para">
<h2>f_lseek</h2>
<p>The f_lseek function moves the file read/write pointer of an open file object. It can also be used to increase the file size (cluster pre-allocation).</p>
<pre>
FRESULT f_lseek (
FIL* <em>FileObject</em>, /* Pointer to the file object structure */
DWORD <em>Offset</em> /* File offset in unit of byte */
);
</pre>
</div>
<div class="para">
<h4>Parameters</h4>
<dl class="par">
<dt>FileObject</dt>
<dd>Pointer to the open file object.</dd>
<dt>Offset</dt>
<dd>Number of bytes where from start of the file</dd>
</dl>
</div>
<div class="para">
<h4>Return Values</h4>
<dl class="ret">
<dt>FR_OK (0)</dt>
<dd>The function succeeded.</dd>
<dt>FR_DISK_ERR</dt>
<dd>The function failed due to an error in the disk function.</dd>
<dt>FR_INT_ERR</dt>
<dd>The function failed due to a wrong FAT structure or an internal error.</dd>
<dt>FR_NOT_READY</dt>
<dd>The disk drive cannot work due to no medium in the drive or any other reason.</dd>
<dt>FR_INVALID_OBJECT</dt>
<dd>The file object is invalid.</dd>
</dl>
</div>
<div class="para">
<h4>Description</h4>
<p>The f_lseek function moves the file R/W pointer of an open file. The offset can be specified in only origin from top of the file. When an offset above the file size is specified in write mode, the file size is increased and the data in the expanded area is undefined. This is suitable to create a large file quickly, for fast write operation. After the f_lseek function succeeded, member fptr in the file object should be checked in order to make sure the R/W pointer has been moved correctry. In case of fptr is not the expected value, either of followings has been occured.</p>
<ul>
<li>End of file. The specified Offset was clipped at the file size because the file has been opened in read-only mode.</li>
<li>Disk full. There is insufficient free space on the volume to expand the file size.</li>
</ul>
</div>
<div class="para">
<h4>QuickInfo</h4>
<p>Available when <tt>_FS_MINIMIZE &lt;= 2</tt>.</p>
</div>
<div class="para">
<h4>Example</h4>
<pre>
/* Move to offset of 5000 from top of the file */
res = f_lseek(file, 5000);
/* Move to end of the file to append data */
res = f_lseek(file, file-&gt;fsize);
/* Forward 3000 bytes */
res = f_lseek(file, file-&gt;fptr + 3000);
/* Rewind 2000 bytes (take care on overflow) */
res = f_lseek(file, file-&gt;fptr - 2000);
</pre>
<pre>
/* Cluster pre-allocation (to prevent buffer overrun on streaming write) */
res = f_open(file, recfile, FA_CREATE_NEW | FA_WRITE); /* Create a file */
res = f_lseek(file, PRE_SIZE); /* Pre-allocate clusters */
if (res || file-&gt;fptr != PRE_SIZE) ... /* Check if the file size has been increased correctly */
res = f_lseek(file, DATA_START); /* Record data stream without cluster allocation delay */
...
res = f_truncate(file); /* Truncate unused area */
res = f_lseek(file, 0); /* Put file header */
...
res = f_close(file);
</pre>
</div>
<div class="para">
<h4>See Also</h4>
<p><tt><a href="open.html">f_open</a>, <a href="truncate.html">f_truncate</a>, <a href="sfile.html">FIL</a></tt></p>
</div>
<p class="foot"><a href="../00index_e.html">Return</a></p>
</body>
</html>