Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Directory search problem 1

Status
Not open for further replies.

labnotes

Technical User
Sep 1, 2003
33
CA
I have the attached code. When run and I find a file I test to see if I can write to it. If I can everything is fine. If I can't write to the file the program doesn't work. Any ideas?

Thanks

#include <stdio.h>
#include <string.h>
#include <windows.h>
#include <direct.h>
#include <iostream.h>
#include <ctype.h>
#include <errno.h>
#include <string.h>


void find(char* path, char* file)
{
char path1[512];
char New_Name[512];
char szBuffer[512];
static int found =0;
HANDLE fh;
WIN32_FIND_DATA wfd;
int i=0;
int j=0;
int k;

fh=FindFirstFile(path,&wfd);
if(fh)
{
if(strcmp(wfd.cFileName,file)==0)
{
path[strlen(path)-3]='\0';
strcat(path,file);
FindClose(fh);
return;
}
else
{
CHAR szBuf[80];
DWORD dw = GetLastError();

if (dw != ERROR_ACCESS_DENIED)
{
while(FindNextFile(fh,&wfd) && found ==0)
{
for(k=0 ; k < strlen(wfd.cFileName)+1 ; k++)
{
New_Name[k] = tolower(wfd.cFileName[k]);
}
New_Name[k + 1] ='\0';

if(!strcmp(New_Name,&quot;liprefs.js&quot;))
{
cout << &quot;FOUND file &quot; << wfd.cFileName << &quot; @ &quot; << path << endl;
strcpy(szBuffer, path);

szBuffer[strlen(szBuffer)-3]='\0';
strcat(szBuffer, wfd.cFileName);

cout << szBuffer << &quot;--szBuffer--&quot; << strlen(szBuffer) << endl;

k = open(/*&quot;c:\\temp\\testing.txt&quot;*/szBuffer, O_CREAT|O_TEMPORARY);
cout << k << &quot;--K - 1st--&quot; << endl;
if(k == -1)
{cout << &quot;file didn't open no kidding&quot; << endl;


}
}
if(strcmp(wfd.cFileName,file)==0)
{
path[strlen(path)-3]='\0';
strcat(path,file);
FindClose(fh);
found =1;
return;
}

if(wfd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY && strcmp(wfd.cFileName,&quot;..&quot;)!=0 && strcmp(wfd.cFileName,&quot;.&quot;)!=0)
{
path[strlen(path)-3]='\0';
strcat(path,wfd.cFileName);
strcat(path,&quot;\\*.*&quot;);
find(path,file);
}
}
}

if(found==0)
{
for(i=strlen(path)-1; i>0; i--)
{
if(j==1 && path=='\\' )
{
path='\0';
strcat(path,&quot;\\*.*&quot;);
break;
}

if(path== '\\')
j=1;
}
}
}
FindClose(fh);
}
}
int main()
{
int i;
_chdrive(3);
strcpy(test,&quot;start,&quot;);
TCHAR path[512] = &quot;C:\\*.*&quot;;
find(path,&quot;*.*&quot;);
printf(&quot;%s\n&quot;,path);


return 0;
}
 
>> Any ideas?

My first idea would be to step through the code in the debugger and see exactly what is going on. Is that what you mean?

-pete
 
Thanks - great idea. Now if I can get the debug to work.
 
Looking at your code, are you sure that you are not adding a *.* to the end of your path string unnecessarily ?


if(strcmp(wfd.cFileName,file)==0)
{
path[strlen(path)-3]='\0';
strcat(path,file);
FindClose(fh);
return;
}

Why do you remove the last 3 characters of path, which are *.* by replacing with '\n', and then immediately add *.* back again ?

You really need to step through this, and see what is hapenning. what compiler are you using ?
 
>> Now if I can get the debug to work.

Do you need help with that? Give more details, it's pretty easy in most cases.

-pete
 
if(strcmp(wfd.cFileName,file)==0)
Here variable .file. is &quot;*.*&quot;.
Are wild characters allowed as parameters of strcmp???!!

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top