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,"liprefs.js"
)
{
cout << "FOUND file " << wfd.cFileName << " @ " << path << endl;
strcpy(szBuffer, path);
szBuffer[strlen(szBuffer)-3]='\0';
strcat(szBuffer, wfd.cFileName);
cout << szBuffer << "--szBuffer--" << strlen(szBuffer) << endl;
k = open(/*"c:\\temp\\testing.txt"*/szBuffer, O_CREAT|O_TEMPORARY);
cout << k << "--K - 1st--" << endl;
if(k == -1)
{cout << "file didn't open no kidding" << 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,".."
!=0 && strcmp(wfd.cFileName,"."
!=0)
{
path[strlen(path)-3]='\0';
strcat(path,wfd.cFileName);
strcat(path,"\\*.*"
;
find(path,file);
}
}
}
if(found==0)
{
for(i=strlen(path)-1; i>0; i--)
{
if(j==1 && path=='\\' )
{
path='\0';
strcat(path,"\\*.*"
;
break;
}
if(path== '\\')
j=1;
}
}
}
FindClose(fh);
}
}
int main()
{
int i;
_chdrive(3);
strcpy(test,"start,"
;
TCHAR path[512] = "C:\\*.*";
find(path,"*.*"
;
printf("%s\n",path);
return 0;
}
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,"liprefs.js"
{
cout << "FOUND file " << wfd.cFileName << " @ " << path << endl;
strcpy(szBuffer, path);
szBuffer[strlen(szBuffer)-3]='\0';
strcat(szBuffer, wfd.cFileName);
cout << szBuffer << "--szBuffer--" << strlen(szBuffer) << endl;
k = open(/*"c:\\temp\\testing.txt"*/szBuffer, O_CREAT|O_TEMPORARY);
cout << k << "--K - 1st--" << endl;
if(k == -1)
{cout << "file didn't open no kidding" << 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,".."
{
path[strlen(path)-3]='\0';
strcat(path,wfd.cFileName);
strcat(path,"\\*.*"
find(path,file);
}
}
}
if(found==0)
{
for(i=strlen(path)-1; i>0; i--)
{
if(j==1 && path=='\\' )
{
path='\0';
strcat(path,"\\*.*"
break;
}
if(path== '\\')
j=1;
}
}
}
FindClose(fh);
}
}
int main()
{
int i;
_chdrive(3);
strcpy(test,"start,"
TCHAR path[512] = "C:\\*.*";
find(path,"*.*"
printf("%s\n",path);
return 0;
}