C: SimpleIDE... Directory Listing of SD Card Files
idbruce
Posts: 6,197
I have been searching for hours..... Does anyone know of an an example for SimpleIDE, for recursively searching and/or listing SD card files and/or directories?
Basically, I want to list all the files and/or directories on an LCD display, so that a user can navigate this list, through the use of pushbuttons, to select a desired file.
Basically, I want to list all the files and/or directories on an LCD display, so that a user can navigate this list, through the use of pushbuttons, to select a desired file.
Comments
Just what I needed.
I am surprised this example is not more publicized.
Yea, the title is definitely misleading, because I stated it incorrectly. Bsaically, I just wanted a directory listing, which could be searched further, with chdir, etc...
Sorry. I'm being a bit of a wind up. It's because writing a recursive directory search that will list all files in all directories and subdirectories from the the root downwards is an interesting exercise. Many C programmers, or indeed users of other languages never get their heads around such recursion.
Is it what you want to do? Is it practical on the Prop? No idea.
In the past, I have worked with recursive directory searching on two occassions, with both of them involving a Tree control. I cannot remember if I ever got it working correctly, but I think so. However, yes it is very interesting, without it, you would never find the word "scudattle", located in a file named "podunk.txt", buried deeply within a highly complex directory structure, of F: drive
Nah, as indicated, I used the word recursive without thinking about correct usage.
I suppose it could be useful, depending on the project of course. Perhaps famous quotations of various authors, musicians, and politicians, being indexed according to the category, and then by person.
That can lead to a C function that calls itself recursively. That is a big conceptual headache for many.
It is really not all that different, once you have a directory, simply iterate, open, and search the files. Following the directories is the hard part. And then as Dave mentions, you have to watch out for "." and ".."
Then the issue of the base case, as Dave demonstrates above.
Edit: No wait: the "." and ".." things are a looping within the recursive structure one is searching recursively...A kind of meta-recursion.
Headache time!
Not that it is applicable, just you guys got me thinking about this stuff
If you get that tested on the Prop, please let us know the results. I would test it, but I only have three files on my SD card.
I do believe you have managed to sidetrack me a little. Please allow me to explain.
I have always prided myself with the ability to program and navigate files, folders, and disk paths, although in a Windows environment, where different functions were utilized. As it stands now, I feel like a duck out of water, with a completely new set of rules and functions, and I somehow feel obligated to learn these new rules and functions.
I was going to take the easy way out, and just store individual files within the root of the SD, but now that you have exposed me to various file and folder functions, I think I will create a directory tree and explore a little, just to acquire navigational skills. I don't know where this is leading, but hopefully I will be able to wrap my head around it fairly quickly.
That seems odd that it would only go one level deep in the root, but with a full path it would go further. Okay, time to go educate myself
Welcome to the freedom of the world outside Windows. Take a deep breath of the fresh air and dive in (Is that an odd mix of metaphors?)
It's a *****, you can spend years thinking you know how to program and every now and then be reminded you know nothing and all your assumptions may not be absolute truths. JavaScript gave me that jolt as it throws away some dearly held conceptions and introduces other subtle ways to program. Just now I'm playing with Erlang, again a language built around a totally different view of the programming problem.
Sorry, just rambling again...
Yes indeed.
LOL I have taken the leap.... As I swim about, it appears that it is not only direct C library functions that are being utilized. It is a llittle confusing, but muddling my way through it.
As far as I can determine.... remove, rename, chdir, rmdir, and mkdir all return 0 on success and -1 for failure. Is this correct?
And.... When making file paths, should a forwardslash or a backslash be used? I read that either can be used for SD. I would assume that the backslash is the norm for creating paths, with "\" being the delimiter, since that is the delimiter for print commands.
If you use anything other than "/" as a path separator I will have to come round your place and taunt you.
I wonder if they set errno when there is an error. Which can then be translated and printed as a string with perror() or just converted to a string with strerror().
I was there earlier, and their documentation is what I was basing my assumptions upon
That's good to know, since I assumed the "\".
If so, once again assuming... I would assume this would be a "last known error", which must be used immediately, before being replaced???
Since you are being kind enough to answer some of my lingering questions, let me ask you another.
Many of the functions have a parameter of "path", whereas "remove" and "rename" take file names. Do I have to be in the working directory to call these functions or can I also just supply a path to these functions?
#include <stdio.h>
int remove(const char *pathname);
I will just try it out and see what happens.
I am currently working on a file and folder functions wrapper for SD card access. I figure one file for all the necessary operations, just to make file access and manipulation a little easier.
Here is what I have so far, with the lower functions still needing to be defined. I have the mindless stuff done already Now it will get a little more difficult
However, in the past, I created something similar for Windows and found it to be extremely useful, instead of trying to remember all the various functions and includes. I would simply add one header file and one source file, and then I was ready to perform almost any file and folder operation that I wanted to do, with the header file being a handy quick reference at my disposal.
One include and you are good to go.