-
Showing the *.* files
I'm writing a win32 console program and I must have a function which displays the *.lng files. Now, when I was writing for DOS there were a ffblk structure, function findfirst and findnext in "dir.h" library for this purpose, but now I'm writing in Windows and I don't know what function am I supposed to use.
Please help.
-
I think the windows versions work about the same and are named similarly, ie:
FindFirst(), et al....
But the DOS ones work fine too in Windows.
-
DOS ones are not working. At least ffblk structure. Is there any similar structure in windows?
-
Not that I know of...
Does it work in DOS mode for you?
If not, post your code.
-
your going to hate me for this
(it works on Linux but it might work on Win32 or DOS)
Code:
#include <stdio.h>
int main()
{
system("dir *.exe");
printf("\n");
return 0;
}
// replace *.exe with *.extension or whatever you want, it still works.
-
This code was compiled with Turbo C++ 3.0 and it is working. But if I compile this with VC++ 6.0 it is not working. The error that is reported is that the compiler can't find the "dir.h" library.
Code:
# include <dir.h>
void ShowLNGFiles()
{
struct ffblk File01;
int Search = 0;
Search = findfirst ("*.lng", &File01, 0);
while (!Search)
{
printf("\n%s", File01.ff_name);
Search = findnext (&File01);
}
}
Now I just need the replacement for this function, so that will work in win32 console program (compiled with VC++ 6.0).
EDIT:
Lynux: Thanks, but no thanks. I'm not looking for that kind of solution.
-
-
Thanks no-one. Now I wrote this and I can't get it working.
Code:
# include <io.h>
void ShowLNGFiles()
{
struct _finddata_t File01;
long int Search, i=0;
Search= _findfirst("*.*", &File01);
while (!Search)
{
i++;
printf("%s", File01.name);
Search = _findnext(0, &File01);
}
if (i == 0)
{
printf("\nNot working.");
}
}
-
ok other than not including stdio.h the only problem i see right of is you need to change your find string to something with a directory!
Search= _findfirst("C:\\*.*", &File01);
-
Try this:
void ShowLNGFiles()
{
DIR d = opendir("c:\\");
struct _finddata_t File01;
long int Search = 1, i=0;
long int Handle;
Handle = _findfirst("*.*", &File01);
while (Search)
{
i++;
printf("%s", File01.name);
Search = _findnext(Handle, &File01);
}
if (i == 0)
{
printf("\nNot working.");
}
closedir( d );
/*...might need to use findclose() too, look at your documentation, maybe in io.h, maybe dirent.h...*/
}
Let me know if that works...
-
By the way, Salem posted an excellent example some time back. Do a search on the boards to find it...
...Here it is. One thing though, it needed a slight revision...
Code:
//...Author: Salem A.K.A. "stee"
#include <stdio.h>
#include <dir.h>
#include <string.h>
#define ALL_ATTS ( \
FA_DIREC | \
FA_ARCH )
void walker ( char *path ) {
struct ffblk finder;
unsigned int res;
chdir(path); //...added this so you don't crash...
for ( res = findfirst ( "*.*", &finder, ALL_ATTS );
res == 0;
res = findnext( &finder ) ) {
if ( strcmp(finder.ff_name, ".") == 0 ) continue; // current dir
if ( strcmp(finder.ff_name, "..") == 0 ) continue; // parent dir
// if its a directory, examine it
// else compare the filename with the one we're looking for
if ( finder.ff_attrib & FA_DIREC ) {
char newpath[MAXPATH];
strcpy( newpath, path );
strcat( newpath, "\\" );
strcat( newpath, finder.ff_name);
chdir( finder.ff_name );
walker( newpath );
chdir( ".." );
} else {
if ( strcmp( finder.ff_name, "words.txt" ) == 0 ) {
printf( "Found!!!" );
}
}
}
}
int main ( ) {
char *root = "\\";
chdir( root );
walker( root );
return 0;
}
To make it fit your needs, just add another parameter to the function...Tho now that I think about it, this looks a little incomplete...still, try it.
-
This example is for DOS based program and not for the win32 console program. Anyway I fixed the code and it is working now.
Here's the code:
Code:
void ShowLNGFiles ()
{
struct _finddata_t File;
long int Search= 0, Handle = 0;
int i = 0;
BOOL Loop = TRUE;
Handle = _findfirst("C:\\*.lng", &File); // returns -1 if there is no file, otherwise returns 6488923
if (Handle != -1)
{
while (Loop)
{
printf("%s", File.name);
Search = _findnext(Handle, &File); // returns 0 if the file is found, otherwise returns -1
i++;
if (Search == -1) Loop = FALSE; // if there is no files more to be found then exit the while loop
}
}
else
{
printf ("File(s) with extension \"*.lng\" can not be found.");
}
}
But I have one more question. When I was programming in Turbo C++ 3.0 (for DOS) and if I want to define my current directory I had to write this ".\myprog\". My problem is that this is not working with VC++ 6.0. How to define my current directory?
-
This may work:
char buff[500];
getcwd(buff, 500);
-
You miss understood.
Code:
Handle = _findfirst("C:\\*.lng", &File);
// how to rewrite "C:\\*.lng" into something independent ??
// something like this "./myprog/*.lng" -> this works only under Turbo C++ 3.0 compiler.
EDIT:
Ok, I fixed it. It is working now.
Thanks guys for you answers.