点击返回Arduino-SD库页面
seek
描述
在文件中寻找新位置,该位置必须在0到文件的大小(含最大)之间。
语法
file.seek(pos)
参数
file:File实例化对象(由SD.open()返回)
pos: 查找的位置 (unsigned long)
返回值
返回值类型:bool
操作成功则返回true,操作失败则返回false
示例程序
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 | /********************************************************************** 程序名称/Program name     : SD_File_seek 团队/Team                 : 太极创客团队 / Taichi-Maker (www.taichi-maker.com) 作者/Author               : Dapenson 日期/Date(YYYYMMDD)     : 2020/06/20 程序目的/Purpose          :   在一个大于1k的文本文件中,演示如何将SD卡内的文件信息从指定位置开始读取,  并通过串口进行打印, 同时将当前读写状态在文件中的位置进行打印 ----------------------------------------------------------------------- 修订历史/Revision History   日期/Date    作者/Author      参考号/Ref    修订说明/Revision Description ----------------------------------------------------------------------- 其它说明: ***********************************************************************/ #include <SPI.h> #include <SD.h> // 创建File实例化对象 File myFile; void setup() {     // 初始化硬件串口并设置波特率为9600     Serial.begin(9600);     while (!Serial)     {         ; //等待串口打开     }     Serial.print("Initializing SD card...");     // 检测是否初始化完成     if (!SD.begin())     {         Serial.println("initialization failed!");         return;     }     Serial.println("initialization done.");     // 查看是否存在"example"文件     if (SD.exists("example.md"))     {         Serial.println("example.md exists.");     }     else     {         Serial.println("example.md doesn't exist.");     }     // 打开example文件     myFile = SD.open("example.md");     if (myFile)     {         Serial.println("example.md:");         // 从第十个字节开始读取文件         if (myFile.seek(10))         {             //从文件里读取数据并打印到串口,直到读取完成             while (myFile.available())             {                 Serial.write(myFile.read());             }             // 关闭文件打开状态             myFile.close();             // 向串口打印文件名称             Serial.print(String("") + "file name = " + myFile.name());         }     }     else     {         //如果文件没有打开,则向串口输出文件打开错误         Serial.println("error opening example.md");     } } void loop() { } | 
效果演示
本程序将从第十个字节的位置开始读取数据,因此前面的数据将不会被打印,也可参考 SD-position() 函数内容
