字符串末尾的额外"null"



我一直在使用Arduino尝试在Spiffs中存储文件并稍后读取它。当我打印它时,我得到了我所期望的,当我将它添加到字符串时,我以"null"出乎我的意料。知道要改变什么吗?

代码如下:

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

下面是响应:

This is a testnull
This is a test

循环中的文件处理是错误的-打开,读取和关闭文件的操作与检查文件是否可供读取交替进行。不要把它们混在一起。它应该是这样的:

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);

解决了我的问题

最新更新