123
-=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- (c) WidthPadding Industries 1987 0|555|0 -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=-
Socoder -> C/C++/C#/Other -> navigating directories using strings?

Sun, 16 Sep 2007, 14:37
spinal
eh?

I have a list of folders in strings in a struct - eg. file.filename[255]. I would like to use this name to navigate throught a directory, but im crap at using strings for anything in c. I also have a directory name as folder[255].

e.g.

folder="/"; // root directory
file[0].filename="data"; first directory

im trying thing like
sprintf(folder,"%s/",file.filename);
to get the new folder name, but that doesn't seem to work, also I will need to get BACK from that folder. I'm crap at gooleing and im tired. can anyone do this for me?

* notice how honest I am, instead of asking for help with it, im asking for someone to do it for me, that sort of honesty has to count for something, right?

-=-=-
Check out my excellent homepage!
Sun, 16 Sep 2007, 15:51
mike_g
To set the filename the easiest way is probably to use strcpy() pass the pointer to the string as the first param, then the text to set it to as the second. IE:

If you want to append to a string you can use strcat(). IE:

Oh and you would need to include string.h as well.
Mon, 17 Sep 2007, 17:52
spinal
I have had a little success with the following code.


however the last line -
if(strlen(folder) == 0)folder="/";

seems to be doing nothing. If I try to get back to the root ("/") the function detects the last (only) "/" and removes it. This causes to directory to not be read. This line is supposed to set folder to"/" if it has a length of 0.

-=-=-
Check out my excellent homepage!
Mon, 17 Sep 2007, 18:36
mike_g
I'm suprised it compiles. You may want to turn your warining levels up. They can tell you a lot of useful stuff.

If you change this line:

to this:


I think it should work. Thats assuming 'folder' is initialised to null before hand for testing reasons.

Here: folder="/";
You were trying to set a pointer to a string. Which wont work. You want to set what the pointer points to so you put the * infront of it. Also to set a character you use single quotes.

Hope that helps.

|edit| Actually thats a really bad idea because youre going to overwrite the null terminator.

Here do that instead:

|edit|


Tue, 18 Sep 2007, 04:37
spinal
Neither of those solutions worked, so I cheated a little.


Seems to work just fine, I keep forgetting that C strings aren't really strings at all.

-=-=-
Check out my excellent homepage!
Tue, 18 Sep 2007, 06:50
mike_g
The last one I posted should have worked, as long as 'folder' was initialized to null. But if what you have works then use it. One thing tho is that it would be more portable using:

As this would work with any character encoding not just ASCII.
Tue, 18 Sep 2007, 09:06
mike_g
Oh yeah, I dident spot that o_0. I think it was probably a typo tho. Originally it was to check if the contents of the folder was:

if(strcmp(folder, "") == 0)