It seems to be working now. I think I was supplying an invalid directory name to the scandir function, and the error that was being returned was a red herring. Thanks for your help, SimonF!
p.s. Turns out that wasn't the root cause of the problem. I was pasting in some code snippets, and what looked to be spaces in the code were actually a non-space character. Once I figured that out and over-typed with actual spaces, everything started working smoothly. I sure was getting some strange results until I found the actual problem.