我正在从SD卡读取STM32嵌入式应用程序的数据.该数据被写入文本文件中.
char resultFormat[100] = "%s\n%s\n%s\n%lf\n%lf\n%lf\n%i\n%i\n%i\n";
void readResult(int indexNumber)
{
FRESULT fr;
FIL MyFile; /* File object */
size_t read;
UINT bytesRead;
FATFS SDFatFs; /* File system object for SD disk logical drive */
fr = f_mount(&SDFatFs, (TCHAR const *)SDPath, 0); //Mounts the file
char fileName[20];
char directory[10] = "RESULTS";
sprintf(fileName, "%s/%i.TXT", directory, indexNumber);
fr = f_open(&MyFile, fileName, FA_READ); //Opens the file
read = f_read(&MyFile, resultBuffer, sizeof resultBuffer, &bytesRead); //Writes the txt from the txt file to a string.
vTaskDelay(200);
f_close(&MyFile); //Closes the file
sscanf(resultBuffer, resultFormat, resultDate, resultTime, presetName,
&correctedFlash, &observedFlash, &pressure, &flashedStatus,
&resultStartTemp, &status);
sendResultInfo(); //Processes the variables. Not relevant.
fr = f_mount(NULL, "", 0); //Unmounts the file.
}
我的主要问题是:
char resultFormat[100] = "%s\n%s\n%s\n%lf\n%lf\n%lf\n%i\n%i\n%i\n";
sscanf(resultBuffer, resultFormat, resultDate, resultTime, presetName,
&correctedFlash, &observedFlash, &pressure, &flashedStatus,
&resultStartTemp, &status);
我有相当多的变量需要从字符串中读取,如果我更改了任何内容,我还必须更改resultFormat
,这可能会导致错误.
现在,变量的数量是可管理的,但我想知道如果我有更多的数据要从文本文件中读取,比如我有here个不同类型的变量要读取,应该如何读取这些数据.sscanf
似乎太容易出现人为错误,无法在这种情况下可靠地使用,我想知道是否有从字符串读取数据的替代方法.前面的问题here提供了一个解决sscanf
长阅读的方案,但我想知道是否还有其他的解决方案.
我的第二个问题(不确定是否适合这个堆栈,但我的印象是它会被提出来)是在不使用文本文件的情况下存储这些数据,而不是将其存储为字符串.
我拥有的数据中没有多少是字符串,其中一些是整型和双精度型,但存储为字符串.
虽然我目前的系统工作正常(而且很适合调试,因为我可以很容易地从外部读取存储的数据),但我想知道应该如何格式化非字符串数据并将其存储在SD卡中.
如果需要更多的信息或详细说明,请让我知道.