Teen Programmers Unite  
 

 

Return to forum top

simple search code in c++

Posted by whizkide [send private reply] at July 25, 2002, 07:08:55 PM

how do i write a code in c++ that searches through a folder for a file.
All posts should include sample code pls
whizzzzzzzzzzz

Posted by unknown_lamer [send private reply] at July 25, 2002, 08:19:45 PM

Why do you keep posting stuff like this?

In POSIX C, you want to use the opendir () and readdir () procedures:

#include <sys/types.h>
#include <dirent.h>

DIR *opendir(const char *name);
struct dirent *readdir(DIR *dir);

struct dirent {
  long            d_ino;  /* inode number */
  off_t           d_off;  /* offset to the next dirent */
  unsigned short  d_reclen;/* length of this record */
  unsigned char   d_type;         /* type of file */
  char            d_name[256];    /* filename */
};


Posted by Zandalf [send private reply] at July 26, 2002, 06:28:00 PM

of you can use the system command and use the dir (ls) command with the appripriate tags, and then route it to a file, that you can then search using normal methods (although I'm thinking the above example is better, though I've never tried that approach)

oh, and since you don't know the routing operator, you're going to have some trouble doing that ^_^ but since I'm such a nice guy (on occasion)...

system(dir -> temp.txt)
Posted by unknown_lamer [send private reply] at July 26, 2002, 07:26:53 PM

Well, calling system has a lot of overhead: your process has to fork and then exec the program in the child and then call wait () on the child. The overhead is pretty high. In fact, the child actually calls /bin/sh to execute the command, which adds more overhead. You should avoid system if you can.

Posted by whizkide [send private reply] at July 26, 2002, 07:40:11 PM

Chill Zandalf,
u lost me there. What value does the system() command return if the file is not found or if it is found.
Thanx for the help though.
And i took your advice. Guess i was kinda proud. But Gian, u are such a prick( no offense). Next time dont take such too personal. Aight
still me

Posted by unknown_lamer [send private reply] at July 26, 2002, 09:52:37 PM

From the man page:

RETURN VALUE
The value returned is -1 on error (e.g. fork failed), and
the return status of the command otherwise. This latter
return status is in the format specified in wait(2).
Thus, the exit code of the command will be WEXITSTA&#65533;
TUS(status). In case /bin/sh could not be executed, the
exit status will be that of a command that does exit(127).

If the value of string is NULL, system() returns nonzero
if the shell is available, and zero if not.

system() does not affect the wait status of any other
children.

Posted by Zandalf [send private reply] at July 27, 2002, 01:44:43 AM

yeah, tis true, I'd fogotten how much overhead there was for that :(

but, it's an easy way if you're trying to hack a solution.

and whiz - sorry to be rash in the last coupla posts, I was in an aggressive mood this morning. it won't not find the file because you're making one, not reading one.

Posted by whizkide [send private reply] at August 05, 2002, 03:47:42 PM

ok! thanx for all the help but there's another problem. Im trying to use some non standard functions like findfirst() which are in "dir.h". I managed to get a copy of "dir.h" but it includes another header file "_defs.h" which aint on my compiler. I use VC++6.0. could someone pls send me a copy of "_defs.h" and any other header files it includes.
Thanx
whizzzzzzzzzzzzzzzzzzzzzzzzzz

Posted by Neumann [send private reply] at August 05, 2002, 05:26:05 PM

dir.h doesn't include code so your program will not compile. Header file rarely do include code. To get dir.h working under VC++6.0, you'd need to get a copy of the library source and compile them as a .LIB to make it work in your program.

Posted by CodeRed [send private reply] at August 05, 2002, 11:45:45 PM

"All posts should include sample code"

LOL...

Posted by whizkide [send private reply] at August 06, 2002, 12:16:41 PM

yeah yeah, code red i appreciate ur sense of sick humour but lets get serious. How do i get a copy of the lib. source
whizzzzzzzzzzz

Posted by CodeRed [send private reply] at August 06, 2002, 01:56:38 PM

Sick humor? I was laughing at how you demanded that every post meet a certain condition, as if you owned this message board. How about saying "Sample code appreciated" instead?

Posted by Neumann [send private reply] at August 06, 2002, 03:53:07 PM

whizkide: Look for it on the web. I don't know what you are using and I don't want to know because it looks like your are not the kind of guy that likes to find things by himself.

You must be logged in to post messages and see which you have already read.

Log on
Username:
Password:
Save for later automatic logon

Register as a new user
 
Copyright TPU 2002. See the Credits and About TPU for more information.