【发布时间】:2021-04-20 08:30:51
【问题描述】:
在我的 Arduino 程序中,我再次遇到了很多关于字符和字符数组的困难。我使用带有 SPIFFS 的 ESP32 设备,我在其中保存了一些我想保存到我的 ESP32 设备中的数据。在我的 SPIFFS 中,我有一个 wifi.txt 文件,用于保存 wifi 名称。然后我调用函数:
char* saved_networks; // create char array to save information from readfile
saved_networks = readFile(SPIFFS, "/wifi.txt");
readFile 函数描述如下:
char* readFile(fs::FS &fs, const char *path)
{
char* return_message; //create a variable return_message to hold the appended char data
Serial.printf("Reading file: %s\n", path);
File file = fs.open(path);
if (!file || file.isDirectory())
{
Serial.println("Failed to open file for reading");
return "FAIL";
}
Serial.print("Read from file: ");
while (file.available())
{
//Serial.write(file.read());
char c = file.read(); // save one character at a time and save it to a temporaray variable c
delayMicroseconds(100);
Serial.print(c); // this prints the wifi name as expected so everything is working ok up to this point
strcat(return_message, &c); // append char array (return_message)
}
Serial.print("printing final return message before closing file system=");
Serial.println(return_message); //prints loads of garbage
file.close();
return return_message;
}
打印return_message后,ESP32设备崩溃并返回错误消息:
Reading file: /wifi.txt
Read from file: Telia-33F8A3-Greitas
printing final return message before closing file sysetm=x⸮?⸮⸮?⸮⸮?T⸮ @?e⸮ @?l⸮ @?i⸮ @?a⸮ @?-⸮ @?3⸮ @?3⸮ @?F⸮ @?8⸮ @?A⸮ @?3⸮ @?-⸮ @?G⸮ @?r⸮ @?e⸮ @?i⸮ @?t⸮ @?a⸮
Stack smashing protect failure!
abort() was called at PC 0x401377bf on core 1
ELF file SHA256: 0000000000000000
Backtrace: 0x40088740:0x3ffb1480 0x400889bd:0x3ffb14a0 0x401377bf:0x3ffb14c0 0x400d1685:0x3ffb14e0 0x400d1872:0x3ffb1590 0x400d18b3:0x3ffb1f80 0x400d4cfe:0x3ffb1fb0 0x400899ce:0x3ffb1fd0
Rebooting...
ets Jun 8 2016 00:22:57
rst:0xc (SW_CPU_RESET),boot:0x13 (SPI_FAST_FLASH_BOOT)
configsip: 0, SPIWP:0xee
clk_drv:0x00,q_drv:0x00,d_drv:0x00,cs0_drv:0x00,hd_drv:0x00,wp_drv:0x00
mode:DIO, clock div:1
load:0x3fff0018,len:4
load:0x3fff001c,len:1216
ho 0 tail 12 room 4
load:0x40078000,len:10944
load:0x40080400,len:6388
entry 0x400806b4
非常感谢任何帮助。
更新1
通过引用将char数组传递给void函数时也是这样吗?:
如果我声明我的 readFile 函数如下:
void readFile(fs::FS &fs, const char *path, char* return_data)
{
Serial.printf("Reading file: %s\n", path);
File file = fs.open(path);
if (!file || file.isDirectory())
{
Serial.println("Failed to open file for reading");
return;
}
Serial.print("Read from file: ");
while (file.available())
{
//Serial.write(file.read());
char c = file.read();
delayMicroseconds(100);
Serial.print(c);
// Do I also must have allocated memory in order to append characters to my return_data char array
}
Serial.print("printing final return message before closing file sysetm=");
Serial.println(return_data);
file.close();
}
然后在我的主要,我打电话:
char* saved_networks = NULL; // create char array to save information from readfile
readFile(SPIFFS, "/wifi.txt",&saved_networks[0]);
更新2
我已经设法通过传递一个 char 数组作为参考来做到这一点。此方法不需要 malloc 也不需要 free。
void readFile123(fs::FS &fs, const char *path, char* return_data)
{
int n=0;
Serial.printf("Reading file: %s\n", path);
File file = fs.open(path);
if (!file || file.isDirectory())
{
Serial.println("Failed to open file for reading");
return;
}
Serial.print("Read from file: ");
while (file.available())
{
char c = file.read();
delayMicroseconds(100);
Serial.print(c);
//strcat(return_data, &c); //returns meditation error
return_data[n]=c; //returns meditation error
n=n+1;
}
file.close();
}
及主要代码:
char saved_networks[100];
readFile123(SPIFFS, "/wifi.txt",saved_networks);
Serial.print("saved networks=");
Serial.println(saved_networks);
这种实现方式对我来说更有意义。如果我可以通过引用传递数组,为什么还要使用 malloc?这实现起来更简单。
但是,我还有一个问题。正如您从上面的代码中看到的,我已将 char 数组的最大长度初始化为 100,但是,我不确定我的 SPIFFS 内容的大小是多少。有没有办法克服这个问题?我知道我可以只声明大小,例如 10000,并希望内容永远不会变得那么大,但这似乎不是解决这个问题的最有效方法。我希望有人可以对此发表评论。提前致谢。
【问题讨论】:
标签: arrays c character-encoding character esp32