Converting data types

I’m using SdFat to read a text file and extract the data I need on each line by doing this:

    char *fileName = strtok(fileData, "|");
    imgRefresh = atof(strtok(NULL, "|"));
    imgTimeout = atof(strtok(NULL, "|"));
    displayPause = atof(strtok(NULL, "\r"));

After that, I open the file indicated by the ‘fileName’ variable:

myFile.open(fileName, O_READ)

However, I am running into a small snag. If the text file has TABs or spaces in a line, I need to remove those. For example, this line works great:

ff001.led|0|8|0.1

However, if I have:

ff001.led |0|8|0.1

That fails because the filename is now 'ff001.led ’ with the extra space(s). The same applies if it’s a TAB.

I actually want the spaces/TABs in the file, I just need to strip them in my program. So how can I remove them?

It is called ‘parsing’ and there are a number of ways to do this which depend on what library string functions your compiler supports. The brute force method is to go through the string one character at a time ans test if the character is a space or tab (or conversely a good character) then write a good character to a new string and skip to the next character if a space or tab.

You compiler may have some useful string functions so read the manual to see what it has and how to use them.

Like wise, do a web search (google) for C language string parsing functions to learn more.

The computer would be the GCC that comes with the Arduino IDE.

There should be lots of info on the GCC compiler (not computer) available on the web.

I haven’t used it so am unfamiliar with what functions it has in it’s libraries but they should be listed in the string.h header.

And you’re right, I was looking at using the String object within the Arduino environment which is where my initial subject line came from, it changes the data type, and I can’t pass it to SD.open anymore. I don’t know how to convert it back.

Well, on a desktop PC, I’d probably use a regular expression to parse the line easily, but a quick googling reveals that is still not implemented on gcc. You can try strtok(fileData, "|\t ") – that’s “pipe, tab, space” and that seems like it should work.

Ok, that does seem to work, however it fails if there are two TABs one after the other. This would be so much easier with proper regexp … but I digress.