michalmonday / CSV-Parser-for-Arduino

It turns CSV string into an associative array (like dict in python)
MIT License
57 stars 12 forks source link

Reading from SDCard example #2

Closed hmronline closed 4 years ago

hmronline commented 4 years ago

I'm having issues to read a csv file from sdcard in chunks; it randomly fails to parse file, using the following code:

    objFile = SD.open("file.csv", "r");
    if (!objFile)
    {
        Serial.print(F("ERROR, unable to open csv file"));
        Serial.println(F(""));
        while (1)
            ;
    }

    */ file contents:
    key,value\n
    102,750\n
    101,750\n
    103,750\n
    104,750\n
    */

    CSV_Parser csvParser(/*format*/ "dd", /*has_header*/ true, /*delimiter*/ ',');

    char buffer[600];
    while (objFile.position() < objFile.size())
    {
        objFile.readBytes(buffer, sizeof(buffer));
        csvParser << buffer;
    }

    csvParser.parseLeftover();
    objFile.close();

I have verified that everything works fine if csv file contents is provided all at once as first argument to csvParser.

Could you please tell the appropriate method to read csv chunks from sdcard ?

This could possible be included in the provided example to help others that may experience the same challenge.

Thanks in advance! Hernán.-

michalmonday commented 4 years ago

Thank you for reporting this. There are 2 problems:

I think it could be good idea to supply the file character by character, this way there's not need for using a buffer at all, I added example like this: https://github.com/michalmonday/CSV-Parser-for-Arduino/blob/master/examples/reading_from_sd_card/reading_from_sd_card.ino

hmronline commented 4 years ago

Hi Michal, thank you for your prompt response!

I've tested the new version and I can confirm it now works properly following the new example code.