• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

programming question about searching for files..

hans007

Lifer
ok my project partner seems to be unix god, so he's asking me to write a parser for him.


One part of it, ineed to check a certain number of paths, for a file.
anyways, here is my idea, i havent tried it yet, as i am tryiing to install linux mandrake 7.2 on my other laptop. however for some reason X wont load, so i am even having problems with that.


FILE* myfile = NULL;
myfile = fopen("path1/thefileyouarelookingfor", "r")
if(myfile == NULL) check other paths


anyone think i'm right? i feel like such a dumbass , since he's like god coder and unix man, and like basically laid out the project and just gave me a parser to run, which is super easy. oh well, at least i didnt get stuck with a partner more clueless than i am at unix.
 
That should work, there's multiple ways to do it though (of course). What do you plan on doing if there is a copy of the file in more than one of your search paths?

Since you seem to know the search paths at compile time, I would probably use an array of char arrays and do something like:

int found = 0;
while (!found) {
myfile = fopen(*search_paths[search_position], "r");
search_position++;
if (myfile) { found = 1; }
elsif (search_position > search_list_size) { break; }
}

That way you can keep adding entries into the search_paths array and it'll still work without having to f' with the search loop.

edit: I'm tired and can't think straight, hopefully you can figure out what I mean =)
 


<< int found = 0;
while (!found) {
myfile = fopen(*search_paths[search_position], "r");
search_position++;
if (myfile) { found = 1; }
elsif (search_position > search_list_size) { break; }
}
>>



Argh, let's clean up some of that mess. 😉

int found = 0;

while (!found) {
myfile = fopen(*search_paths[search_position], "r");
search_position++;

if (myfile) {
found = 1;
}
else if (search_position > search_list_size) {
break;
}
}
 
Argh, let's clean up some of that mess.

Thanks, I knew what I wanted it to look like but I just couldn't convey it =)

And for the life of me I coudln't remember C's syntax for 'else if' because of too, well atleast I didn't end it with 'fi' =)
 
Another perspective...
I'm learning some Python right now, and it is fantastic for this sort of thing.
Here's how you would do it.

#!/usr/bin/python
#A program to recursively search the current directory for a file named myfile.dat
import os

def check_my_file(arg, dirname, names):
&nbsp&nbsp&nbsp&nbsp for name in names:
&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp if name == "myfile.dat":
&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp arg.append(dirname + '/' + name)

os.path.walk('.', check_my_file, files)

for file in files:
&nbsp&nbsp&nbsp&nbsp print file

 
The forum here will allow you up to 2 spaces. any more than that and it removes them.
 


<< The forum here will allow you up to 2 spaces. any more than that and it removes them. >>



Yea, that's my point. It looks like Damascus got it to preserve much more then 2 spaces when he posted that code.
 


<< intersesting. he did, and they aren't even special spaces.
0
1
2
3
4
5
6
7
we shall see!
>>



Didn't work for you either!
 
my CS partner told me to use a function called stat() that would also work. anyone know what .h file that goes with. i normally program in C++ so i need to reacclimate myself with C .
 
You can add spaces to your text by using &NBSP; (write the letters in lowercase though)
like this:

1
2
3
4
5
6
 


<< You can add spaces to your text by using &NBSP; (write the letters in lowercase though)
like this:

1
2
3
4
5
6
>>



Ok,now I get it. Thanks!
 
Heh everyone is so interested in spaces. 🙂

Beau is correct. &NBSP; (in lowercase) will add a Non-Breakable SPace to your
text, as I think the browser discards extra spaces if you enter them with your
space bar.
 
Back
Top