Error while trying to list folders
Hi, i am currently making a project where a part of the project is supposed to send a list of files and folders in the "current directory" (first argument, declared earlier in the project) to the socket "ControlServer"; however im currently only recieving a partially successfull result. It sends "C:\" correctly but if i change directory to for example "C:\Temp\" it will either send nothing or 'something' that is probably binary like "".
Here is the part of the project code that is relevant:
Code:
void getfolds(char *argz)
{
//prepare variables
char argv[sizeof(argz)+4];
memcpy(argv, argz,sizeof(argz));
int counterf=0;
int counterd=0;
string output;
string files;
string folders;
HANDLE searchab;
char last[1024];
char current[1024];
strcat(argv,"\\*");
WIN32_FIND_DATAA file;
//do the search
searchab = FindFirstFile(argv, &file);
do{
FindNextFile(searchab, &file);
strcpy(last,current);
strcpy(current,file.cFileName);
if(strcmp(current, last) != 0 && strcmp(current, "..") != 0){
if(file.dwFileAttributes == 0x10){
if(counterd > 0)folders += "\r\n";
folders += file.cFileName;
counterd++;
}else{
if(counterf > 0)files += "\r\n";
files += file.cFileName;
counterf++;
}
}
} while (strcmp(current, last) != 0);
//paste together and send to teh interwebs
output += "cd ";
output += folders;
output += "\r\n";
output += files;
send(ControlServer,output.c_str(),strlen(output.c_str()),0);
}
I am sure the socket works since a whole lot of other functions that use it work perfectly (including openening a file and sending the content over the socket); but i dont know why it refuses to send the folders correctly (might it be that i dont have the privilegies to see the folders?) It has been tested on Windows 7-64 bit and XP-32 bit. I am currently using dev-cpp with MinGW.
Thanks in advance =)
edit: I think it must be something with the lines
Code:
char argv[sizeof(argz)+4];
memcpy(argv, argz,sizeof(argz));
(when current directory is "C:\temp" it acctually searches "C:\tå@\*" (4 characters before random stuff)
edit2: think i fixed it by editing the first couple of lines into
Code:
char argv[strlen(argz)+4];
strcpy(argv, argz);