operator 发表于 2024-3-31 12:18:55

有没有大佬帮我看看这是怎么回事,关于sd库

<h3>以下是完整代码:</h3><p>&nbsp;</p><p><i>#include&lt;SD.h&gt;</i></p><p><i>#include&lt;SPI.h&gt;</i></p><p><i>#include&lt;String.h&gt;</i></p><p><i>unsigned long fileLength=0;//文件长度</i></p><p><i>unsigned long poipos=0;//文件指针位置</i></p><p><i>File file;</i></p><p><i>String matchPinyin(String);</i></p><p><i>String search(String,unsigned long,unsigned long);</i></p><p><i>void setup() {</i></p><p><i>&nbsp; // put your setup code here, to run once:</i></p><p><i>&nbsp; Serial.begin(115200);</i></p><p><i>&nbsp; Serial.println(SD.begin(SPI_HALF_SPEED, 4));</i></p><p><i>&nbsp; File file=SD.open("1.txt",FILE_READ);</i></p><p><i>&nbsp; fileLength=file.available();</i></p><p><i>&nbsp; file.close();</i></p><p><i>&nbsp; Serial.println("汉字");</i></p><p><i>&nbsp; Serial.println(matchPinyin("a "));</i></p><p><i>&nbsp; Serial.println(matchPinyin("zuo "));</i></p><p><i>&nbsp; Serial.println(matchPinyin("hao "));</i></p><p><i>&nbsp; Serial.println("done");</i></p><p><i>}</i></p><p><i>String matchPinyin(String val)</i></p><p><i>{</i></p><p><i>&nbsp; return search(val,0,fileLength);</i></p><p><i>}</i></p><p><i>String search(String val,unsigned long a,unsigned long b)//传递原值,首位,末位,识别位数</i></p><p><i>{</i></p><p><i>&nbsp; char word=0;//缓存所取</i></p><p><i>&nbsp; int sub=0;//比较下标</i></p><p><i>&nbsp; File file=SD.open("1.txt",FILE_READ);</i></p><p><i>&nbsp; unsigned long mid=a/2+b/2;</i></p><p><i>&nbsp; Serial.print(a);</i></p><p><i>&nbsp; Serial.print(",");</i></p><p><i>&nbsp; Serial.print(b);</i></p><p><i>&nbsp; Serial.print(":");</i></p><p><i>&nbsp; Serial.println(mid);</i></p><p><i>&nbsp; if((b-a)&lt;2)</i></p><p><i>&nbsp; {</i></p><p><i>&nbsp; &nbsp; return "";</i></p><p><i>&nbsp; }</i></p><p><i>&nbsp; //Serial.println("front find");</i></p><p><i>&nbsp; file.seek(mid);</i></p><p><i>&nbsp; while(file.peek()!=10/*不是换行*/)//连续向上查询</i></p><p><i>&nbsp; {</i></p><p><i>&nbsp; &nbsp; /*Serial.print(" &nbsp; &nbsp;");</i></p><p><i>&nbsp; &nbsp; Serial.print(mid);</i></p><p><i>&nbsp; &nbsp; Serial.print(" ");</i></p><p><i>&nbsp; &nbsp; Serial.println(file.peek());*/</i></p><p><i>&nbsp; &nbsp; mid--;</i></p><p><i>&nbsp; &nbsp; file.seek(mid);</i></p><p><i>&nbsp; }</i></p><p><i>&nbsp; //Serial.println("back find");</i></p><p><i>&nbsp; poipos=mid;</i></p><p><i>&nbsp; while(file.peek()!=9/*不是空格*/)//连续向下查询</i></p><p><i>&nbsp; {</i></p><p><i>&nbsp; &nbsp; /*Serial.print(" &nbsp; &nbsp;");</i></p><p><i>&nbsp; &nbsp; Serial.print(file.peek());</i></p><p><i>&nbsp; &nbsp; Serial.print(" ");</i></p><p><i>&nbsp; &nbsp; Serial.println(char(file.peek()));*/</i></p><p><i>&nbsp; &nbsp; file.seek(++poipos);</i></p><p><i>&nbsp; }</i></p><p><i>&nbsp; while(file.seek(++poipos),(word=file.peek())!='\r'/*不是回车*/)//依次比较</i></p><p><i>&nbsp; {</i></p><p><i>&nbsp; &nbsp; /*Serial.print(" &nbsp; ");</i></p><p><i>&nbsp; &nbsp; Serial.print(char(val));</i></p><p><i>&nbsp; &nbsp; Serial.print("==");</i></p><p><i>&nbsp; &nbsp; Serial.print(char(word));*/</i></p><p><i>&nbsp; &nbsp; if(val==word)</i></p><p><i>&nbsp; &nbsp; {</i></p><p><i>&nbsp; &nbsp; &nbsp; sub++;</i></p><p><i>&nbsp; &nbsp; }</i></p><p><i>&nbsp; &nbsp; else if(word&gt;val)//从前寻找</i></p><p><i>&nbsp; &nbsp; {</i></p><p><i>&nbsp; &nbsp; &nbsp; Serial.println("front");</i></p><p><i>&nbsp; &nbsp; &nbsp; yield();</i></p><p><i>&nbsp; &nbsp; &nbsp; file.close();</i></p><p><i>&nbsp; &nbsp; &nbsp; return(search(val,a,(a+b)/2));</i></p><p><i>&nbsp; &nbsp; }</i></p><p><i>&nbsp; &nbsp; else if(word&lt;val)//从后寻找</i></p><p><i>&nbsp; &nbsp; {</i></p><p><i>&nbsp; &nbsp; &nbsp; Serial.println("back");</i></p><p><i>&nbsp; &nbsp; &nbsp; yield();</i></p><p><i>&nbsp; &nbsp; &nbsp; file.close();</i></p><p><i>&nbsp; &nbsp; &nbsp; return(search(val,(a+b)/2,b));</i></p><p><i>&nbsp; &nbsp; }</i></p><p><i>&nbsp; }</i></p><p><i>&nbsp; //此次选择正确</i></p><p><i>&nbsp; String ret="";</i></p><p><i>&nbsp; file.seek(++mid);</i></p><p><i>&nbsp; while(file.peek()!=9)</i></p><p><i>&nbsp; {</i></p><p><i>&nbsp; &nbsp; ret+=char(file.read());</i></p><p><i>&nbsp; }</i></p><p><i>&nbsp; file.close();</i></p><p><i>&nbsp; return ret;</i></p><p><i>}</i></p><p><i>void loop() {</i></p><p><i>&nbsp; // put your main code here, to run repeatedly:</i></p><p><i>&nbsp;&nbsp;</i></p><p><i>}</i></p><p>&nbsp;</p><h3>想做一个从SD卡里的文件用拼音查询汉字的程序,但是<i>File file=SD.open("1.txt",FILE_READ); </i>这一句只能写到递归函数里,写到初始化里在递归函数里就不能操作文件了,是什么情况?怎么改进呢?</h3><p>&nbsp;</p><p>&nbsp;</p><p>&nbsp;</p><p>&nbsp;</p><p>&nbsp;</p><p>&nbsp;</p><p>&nbsp;</p><p>&nbsp;</p><figure class="image"><img></figure><p>&nbsp;</p><p>&nbsp;</p><p>&nbsp;</p><p>&nbsp;</p><h3>&nbsp;</h3><h3>以下是打开的文件(部分)</h3><p>&nbsp;</p><p>&nbsp;</p><p>&nbsp;</p><figure class="image"><img></figure>

operator 发表于 2024-3-31 12:42:59

补两张图片


DFEDU 发表于 2024-4-1 17:56:08

试试这样
#include <SD.h>
#include <SPI.h>
#include <String.h>

unsigned long fileLength = 0; //文件长度
unsigned long poipos = 0;    //文件指针位置
File file;

String matchPinyin(String);
String search(String, unsigned long, unsigned long, File &);

void setup()
{
Serial.begin(115200);
Serial.println(SD.begin(SPI_HALF_SPEED, 4));

file = SD.open("1.txt", FILE_READ);
fileLength = file.available();
file.close();

Serial.println("汉字");
Serial.println(matchPinyin("a "));
Serial.println(matchPinyin("zuo "));
Serial.println(matchPinyin("hao "));
Serial.println("done");
}

String matchPinyin(String val)
{
file = SD.open("1.txt", FILE_READ);
return search(val, 0, fileLength, file);
}

String search(String val, unsigned long a, unsigned long b, File &file)
{
char word = 0;      //缓存所取
int sub = 0;      //比较下标
unsigned long mid = a / 2 + b / 2;

Serial.print(a);
Serial.print(",");
Serial.print(b);
Serial.print(":");
Serial.println(mid);

if ((b - a) < 2)
{
    return "";
}

file.seek(mid);
while (file.peek() != 10)
{
    mid--;
    file.seek(mid);
}

poipos = mid;
while (file.peek() != 9)
{
    file.seek(++poipos);
}

while ((file.seek(++poipos), (word = file.peek())) != '\r')
{
    if (val == word)
    {
      sub++;
    }
    else if (word > val)
    {
      Serial.println("front");
      yield();
      file.close();
      return (search(val, a, (a + b) / 2, file));
    }
    else if (word < val)
    {
      Serial.println("back");
      yield();
      file.close();
      return (search(val, (a + b) / 2, b, file));
    }
}

String ret = "";
file.seek(++mid);
while (file.peek() != 9)
{
    ret += char(file.read());
}
return ret;
}

void loop()
{
}

operator 发表于 2024-4-2 22:56:06

DFEDU 发表于 2024-4-1 17:56
试试这样
#include
#include


问题解决了,谢谢!但是file是全局变量,为什么在初始化里赋值不可以?

开在边缘 发表于 2024-4-8 12:33:07

operator 发表于 2024-4-2 22:56
问题解决了,谢谢!但是file是全局变量,为什么在初始化里赋值不可以?

void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
Serial.println(SD.begin(SPI_HALF_SPEED, 4));
File file=SD.open("1.txt",FILE_READ);



String search(String val,unsigned long a,unsigned long b)//传递原值,首位,末位,识别位数
{
char word=0;//缓存所取
int sub=0;//比较下标
File file=SD.open("1.txt",FILE_READ);


因为你在函数里面又重新定义了一次file,所以函数里面的file就变成了函数内部的局部变量,不再是你之前定义的那个全局变量了。
页: [1]
查看完整版本: 有没有大佬帮我看看这是怎么回事,关于sd库