lseek.html 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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. <body>
  12. <div class="para func">
  13. <h2>f_lseek</h2>
  14. <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>
  15. <pre>
  16. FRESULT f_lseek (
  17. FIL* <span class="arg">fp</span>, <span class="c">/* [IN] File object */</span>
  18. DWORD <span class="arg">ofs</span> <span class="c">/* [IN] File read/write pointer */</span>
  19. );
  20. </pre>
  21. </div>
  22. <div class="para arg">
  23. <h4>Parameters</h4>
  24. <dl class="par">
  25. <dt>fp</dt>
  26. <dd>Pointer to the open file object.</dd>
  27. <dt>ofs</dt>
  28. <dd>Byte offset from top of the file.</dd>
  29. </dl>
  30. </div>
  31. <div class="para ret">
  32. <h4>Return Values</h4>
  33. <p>
  34. <a href="rc.html#ok">FR_OK</a>,
  35. <a href="rc.html#de">FR_DISK_ERR</a>,
  36. <a href="rc.html#ie">FR_INT_ERR</a>,
  37. <a href="rc.html#nr">FR_NOT_READY</a>,
  38. <a href="rc.html#io">FR_INVALID_OBJECT</a>,
  39. <a href="rc.html#tm">FR_TIMEOUT</a>
  40. </p>
  41. </div>
  42. <div class="para desc">
  43. <h4>Description</h4>
  44. <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 undefined because no data is written to the file. This is suitable to pre-allocate a cluster chain quickly, for fast write operation. 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 current read/write pointer is not the expected value, either of followings has been occured.</p>
  45. <ul>
  46. <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>
  47. <li>Disk full. There is insufficient free space on the volume to expand the file.</li>
  48. </ul>
  49. <p>Fast seek feature is enabled when <tt>_USE_FASTSEEK</tt> is set to 1 and the member <tt>cltbl</tt> in the file object is not NULL. This feature enables fast backward/long seek operations without FAT access by using CLMT (cluster link map table). The fast seek feature is also applied to <tt>f_read()/f_write()</tt> function, however, the file size cannot be expanded by <tt>f_write()/f_lseek()</tt> function.</p>
  50. <p>The CLMT must be created in the user defined <tt>DWORD</tt> array prior to use the fast seek feature. To create the CLMT, set address of the <tt>DWORD</tt> array to the member <tt>cltbl</tt> in the file object, set the array size in unit of items into 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 at subsequent <tt>f_read()/f_write()/f_lseek()</tt> function to the file. If the function failed with <tt>FR_NOT_ENOUGH_CORE</tt>, the given array size is insufficient for the file and number of items required is returned into the first item of the array. The required array size is (number of fragments + 1) * 2 items. For example, when the file is fragmented in 5, 12 items will be required for the CLMT.</p>
  51. </div>
  52. <div class="para comp">
  53. <h4>QuickInfo</h4>
  54. <p>Available when <tt>_FS_MINIMIZE &lt;= 2</tt>.</p>
  55. </div>
  56. <div class="para use">
  57. <h4>Example</h4>
  58. <pre>
  59. <span class="c">/* Open file */</span>
  60. fp = malloc(sizeof (FIL));
  61. res = f_open(fp, "file.dat", FA_READ|FA_WRITE);
  62. if (res) ...
  63. <span class="c">/* Move to offset of 5000 from top of the file */</span>
  64. res = f_lseek(fp, 5000);
  65. <span class="c">/* Move to end of the file to append data */</span>
  66. res = f_lseek(fp, f_size(fp));
  67. <span class="c">/* Forward 3000 bytes */</span>
  68. res = f_lseek(fp, f_tell(fp) + 3000);
  69. <span class="c">/* Rewind 2000 bytes (take care on wraparound) */</span>
  70. res = f_lseek(fp, f_tell(fp) - 2000);
  71. </pre>
  72. <pre>
  73. <span class="c">/* Cluster pre-allocation (to prevent buffer overrun on streaming write) */</span>
  74. res = f_open(fp, recfile, FA_CREATE_NEW | FA_WRITE); <span class="c">/* Create a file */</span>
  75. res = f_lseek(fp, PRE_SIZE); <span class="c">/* Expand file size (cluster pre-allocation) */</span>
  76. if (res || f_tell(fp) != PRE_SIZE) ... <span class="c">/* Check if the file has been expanded */</span>
  77. res = f_lseek(fp, DATA_START); <span class="c">/* Record data stream WITHOUT cluster allocation delay */</span>
  78. ... <span class="c">/* DATA_START and write block size should be aligned to sector boundary */</span>
  79. res = f_truncate(fp); <span class="c">/* Truncate unused area */</span>
  80. res = f_lseek(fp, 0); <span class="c">/* Put file header */</span>
  81. ...
  82. res = f_close(fp);
  83. </pre>
  84. <pre>
  85. <span class="c">/* Using fast seek feature */</span>
  86. DWORD clmt[SZ_TBL]; <span class="c">/* Cluster link map table buffer */</span>
  87. res = f_lseek(fp, ofs1); <span class="c">/* This is normal seek (cltbl is nulled on file open) */</span>
  88. fp-&gt;cltbl = clmt; <span class="c">/* Enable fast seek feature (cltbl != NULL) */</span>
  89. clmt[0] = SZ_TBL; <span class="c">/* Set table size */</span>
  90. res = f_lseek(fp, CREATE_LINKMAP); <span class="c">/* Create CLMT */</span>
  91. ...
  92. res = f_lseek(fp, ofs2); <span class="c">/* This is fast seek */</span>
  93. </pre>
  94. </div>
  95. <div class="para ref">
  96. <h4>See Also</h4>
  97. <p><tt><a href="open.html">f_open</a>, <a href="truncate.html">f_truncate</a>, <a href="sfile.html">FIL</a></tt></p>
  98. </div>
  99. <p class="foot"><a href="../00index_e.html">Return</a></p>
  100. </body>
  101. </html>