![]() |
| | #1 |
| Registered User Join Date: Jun 2009
Posts: 3
| Error while trying to list folders 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);
}
Thanks in advance =) edit: I think it must be something with the lines Code: char argv[sizeof(argz)+4];
memcpy(argv, argz,sizeof(argz));
edit2: think i fixed it by editing the first couple of lines into Code: char argv[strlen(argz)+4];
strcpy(argv, argz);
Last edited by Hellbinder; 06-10-2009 at 09:35 AM. |
| Hellbinder is offline | |
| | #2 |
| and the hat of Jobseeking Join Date: Aug 2001 Location: The edge of the known universe
Posts: 21,665
| There's still several things wrong. 1. Indent. It's only 30 lines, and it's already nearly impossible to follow. 2. You ignore the result of the findfirst call, presumably you're assuming that the first call returns ".". 3. You ignore the status result of findfist/findnext. This would be more robust than relying on the strcmp to detect whether the failing call modifies the result (or not). 4. char argv[strlen(argz)+4]; This isn't valid C or C++ code. You're relying on a compiler extension. 5. if(file.dwFileAttributes == 0x10) Directories can have other attributes as well, say hidden. |
| Salem is offline | |
| | #3 | |||||
| Registered User Join Date: Jun 2009
Posts: 3
| Thanks for reply Quote:
Quote:
Quote:
Quote:
Quote:
New Code Code: void getfolds(char *argz)
{
char argv[(strlen(argz)+4)];
strcpy(argv, 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;
searchab = FindFirstFile(argv, &file);
do{ /*Start to do through the directories*/
if(FindNextFile(searchab, &file)!=0){
strcpy(last,current);
strcpy(current,file.cFileName);
if(strcmp(current, last) != 0 && strcmp(current, "..") != 0){ //Check if new result is the same as last one AND check if result is ".."
if(file.dwFileAttributes == 0x10){ //If it is a folder (dosen't include other attributes yet like hidden, to be improved)
if(counterd > 0)folders += "\r\n"; //if more then 1 directory is found, add \r\n before it (new line)
folders += file.cFileName;
counterd++;
}else{ //else if not a folder (assuming file, but can be hidden folders or 'other' things; to be improved)
if(counterf > 0)files += "\r\n"; //if more then 1 file, add \r\n
files += file.cFileName;
counterf++;
} //end of "if file or folder"
} //end of check if result is same or ".."
} else {//end of "if FindNextFile was successfull" and what to do if it wasn't
if(GetLastError() != ERROR_NO_MORE_FILES){ //check if error == no more files
send(ControlServer,"Error while listing files\r\n",28,0);}
break;
}
} while (strcmp(current, last) != 0); //check if last and current is same and go out of loop if it is
/*This part should add both folders (first) and files (last) to output. Should also add "cd " at the start now (debugging, may be removed later)*/
output += "cd ";
output += folders;
output += "\r\n";
output += files;
send(ControlServer,output.c_str(),strlen(output.c_str()),0);
}
~Hellbinder Last edited by Hellbinder; 06-11-2009 at 12:04 PM. | |||||
| Hellbinder is offline | |
| | #4 |
| and the hat of Jobseeking Join Date: Aug 2001 Location: The edge of the known universe
Posts: 21,665
| > Was just supposed to be something i hacked in to get it working and i know this does NOT have a beautiful syntax; Except most people go and do something more enjoyable, like visit the dentist, than wade through a page of unformatted code. You're competing with others for a finite amount of attention time, and unindented code counts against you. Besides, if you format to begin with, you'll have a much easier time yourself as well. > why isn't this valid c++ code? Only C99 supports variable length arrays. MinGW (actually GCC underneath) supports variable length arrays in C++ as an extension. |
| Salem is offline | |
| | #5 | |
| Registered User Join Date: Jun 2009
Posts: 3
| Quote:
Ok, i didn't know that, but since the string length will always be a variable i think im going to continue using it for now. The other option would be to set it to a static size, although i dont like that because of the danger of a overflow since i dont know what will come in. | |
| Hellbinder is offline | |
| | #6 | |
| Registered User Join Date: Sep 2004 Location: California
Posts: 3,020
| Quote:
Code: char* argv = new char[(strlen(argz)+4)]; ... delete [] argv; The code I posted above is C++. The C version would be: Code: char* argv = malloc(strlen(argz)+4); ... free(argv); Last edited by bithub; 06-12-2009 at 05:29 PM. | |
| bithub is offline | |
![]() |
| Thread Tools | |
| Display Modes | |
|
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Link List math | t014y | C Programming | 17 | 02-20-2009 06:55 PM |
| instantiated from here: errors... | advocation | C++ Programming | 5 | 03-27-2005 09:01 AM |
| How can I traverse a huffman tree | carrja99 | C++ Programming | 3 | 04-28-2003 05:46 PM |
| List class | SilasP | C++ Programming | 0 | 02-10-2002 05:20 PM |
| singly linked list | clarinetster | C Programming | 2 | 08-26-2001 10:21 PM |