Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/arduino/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Arduino 额外的;空";在字符串末尾_Arduino_Esp32 - Fatal编程技术网

Arduino 额外的;空";在字符串末尾

Arduino 额外的;空";在字符串末尾,arduino,esp32,Arduino,Esp32,我一直在使用Arduino尝试将一个文件存储在Spiffs中,稍后再将其读回。当我打印它时,我得到了我所期望的,当我把它添加到一个字符串中时,我在我期望的后面加上“null”。你知道要改变什么吗 代码如下: File File=SPIFFS.open(“/myfile.txt”); 字符串响应; while(file.available()){ response=file.readStringUntil('\n'); file.close(); file=SPIFFS.open(“/myfil

我一直在使用Arduino尝试将一个文件存储在Spiffs中,稍后再将其读回。当我打印它时,我得到了我所期望的,当我把它添加到一个字符串中时,我在我期望的后面加上“null”。你知道要改变什么吗

代码如下:

File File=SPIFFS.open(“/myfile.txt”);
字符串响应;
while(file.available()){
response=file.readStringUntil('\n');
file.close();
file=SPIFFS.open(“/myfile.txt”);
Serial.println(文件.readStringUntil('\n'));
序列号println(应答);

}
循环中的文件处理存在缺陷-打开、读取和关闭文件的操作与检查文件是否可读取交织在一起。不要把它们混在一起。它应该是这样的:

File file = SPIFFS.open("/myfile.txt");
String response;
while(file.available()) {
  response = file.readStringUntil('\n');
  Serial.println(response);
}
file.close();

问题不在于文件的读取。这与写作有关。我有

file.print(mystring);
并将其替换为

file.println(mystring);

解决了我的问题。

我意识到,当我继续努力的时候。我确实按照你的建议改变了它,但我的主要问题仍然存在。当我把文件读到一个字符串变量时,我得到一个额外的空值,而当我直接打印到serial时,我得到我输入的值。