简体   繁体   English

SdFat 写入浮点数到文件 ESP32

[英]SdFat write floats to file ESP32

I Need to write float or Strings value into the SDVolume Cache in SDFAT library, I'm using ESP32 with SdCard module.我需要将浮点数或字符串值写入 SDFAT 库中的 SDVolume Cache,我使用的是带有 SdCard 模块的 ESP32。

  uint8_t* pCache = (uint8_t*)sd.vol()->cacheClear();
  memset(pCache, ' ', 512);
  for (uint16_t i = 0; i < 512; i += 4) {
    
    pCache[i + 0] = 'r'; // I Need to write a Float value or String into this cell
    pCache[i + 1] = ',';
    pCache[i + 2] = '0';
    pCache[i + 3] = '\n';
  }

Libray link: https://github.com/greiman/SdFat库链接: https : //github.com/greiman/SdFat

First:第一的:

 // I Need to write a Float value or String into this cell

makes no sense - that "cell" is a single character, like 'e' .没有任何意义 - “单元格”是单个字符,例如'e' How do you write a full float value into a single character?如何将完整的浮点值写入单个字符?

You probably just want to fill pCache with a string representation of your float.您可能只想用浮点数的字符串表示来填充pCache So do that!所以这样做!

We've got all C++ at our disposal, so let's use a somewhat memory efficient method:我们已经掌握了所有的 C++,所以让我们使用一种稍微节省内存的方法:

std::to_chars is the C++ way to convert numeric values to strings. std::to_chars是将数值转换为字符串的 C++ 方式。 Something like就像是

#include <charconv>
…
    std::to_chars(pCache, pCache+512-1, floatvalue);

would be appropriate.会是合适的。

Couple of things:几件事:

  • You need to zero-terminate strings if your needs to be able to know where they end.如果您需要能够知道它们在哪里结束,您需要零终止字符串。 So, instead of memset(pCache, ' ', 512) , memset(pCache, 0, 512) ;因此,代替memset(pCache, ' ', 512)memset(pCache, 0, 512) ; would be more sensible.会更明智。
  • Are you sure your even using the right functions?您确定您甚至使用了正确的功能吗? vol()->cacheClear() is pretty unambigously marked as "do not use in your application" here ! vol()->cacheClear()是相当unambigously标记为“不要在你的应用程序中使用” 在这里 It also seems to be very unusual to me that you would write your string to some block device cache instead of getting a buffer for a file from the file system, or passing a buffer to the file system to write to a file.对我来说,将字符串写入某个块设备缓存而不是从文件系统获取文件缓冲区,或将缓冲区传递给文件系统以写入文件似乎也很不寻常。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM