Correct way of using getline for sd card reading
Posted: Tue Feb 05, 2019 8:42 pm
Hi all,
I try a basic code example for experiment. I send a character via my smartphone over BT Classic to esp32 after that. My esp32 will take some string values from microSD card and send back to my smartphone over BT Classic.
Bluetooth works great. I copy the example esp_spp_acceptor and initiator and make some revisions.
SD card readings working when I use fgets command. I can read data from sdcard and send the smartphone.
But I want to use getline command which is defined in stdio.h.
This is my sample function. When a defined char comes from smartphone over BT, my esp_spp_cb funtion (ESP_SPP_DATA_IND_EVT) calls my special function.
I take compile errors when I want to use getline but I dont take any errors when I use fgets. In my code a task is reading some datas from adc and writing sdcard. This data is one line always. This is not in our scope now.
I try a basic code example for experiment. I send a character via my smartphone over BT Classic to esp32 after that. My esp32 will take some string values from microSD card and send back to my smartphone over BT Classic.
Bluetooth works great. I copy the example esp_spp_acceptor and initiator and make some revisions.
SD card readings working when I use fgets command. I can read data from sdcard and send the smartphone.
But I want to use getline command which is defined in stdio.h.
This is my sample function. When a defined char comes from smartphone over BT, my esp_spp_cb funtion (ESP_SPP_DATA_IND_EVT) calls my special function.
I take compile errors when I want to use getline but I dont take any errors when I use fgets. In my code a task is reading some datas from adc and writing sdcard. This data is one line always. This is not in our scope now.
Code: Select all
void bt_prepareData()
{
char *buffer;
int bufsize = 32;
int bytesRead;
FILE *fptrDataSD_forBT;
buffer = (char *)malloc(bufsize * sizeof(char));
if( buffer == NULL)
{
ESP_LOGE(TAG, "malloc fail");
}
else
{
fptrDataSD_forBT = fopen("/sdcard/WATEVT.txt", "r");
if (fptrDataSD_forBT == NULL)
{
ESP_LOGE(TAG, "Failed to open file watEVT for reading");
//return;
}
else
{
bytesRead = getline(&buffer,&bufsize,fptrDataSD_forBT);
if(bytesRead == -1)
{
printf("getline function error\n");
}
else
{
printf("getline read: %d bytes\n",bytesRead);
printf("getline Data: '%s'\n",buffer);
}
fclose(fptrDataSD_forBT);
}
}