I am a recent graduate in computer programming and failed so far to get an internship or a job in the business so I am trying my best to learn from online source. I am trying to implement a dir-like command on Windows, going through a folder and list all files and folder.
What I've learned is to avoid using namespace std;
and making the includes in alphabetical orders.
In this code, I am obviously mixing c and c++, good or bad? I am also using Digital Mars as a compiler, I'm trying to avoid installing heavy IDEs and compilers like VS.
All thoughts and critiques are welcome.
#include <iostream>
#include <io.h>
#include <string>
#include <sys/stat.h>
#include <sys/types.h>
#include <windows.h>
bool DirectoryExists( const char* absolutePath ){
if( _access( absolutePath, 0 ) == 0 ){
struct stat status;
stat( absolutePath, &status );
return (status.st_mode & S_IFDIR) != 0;
}
return false;
}
char* replace(const char *s){
char* p = new char[strlen(s)+1];
int i=0;
for (i=0;s[i];i++)
if (s[i]=='\\')
p[i] ='/';
else
p[i]=s[i];
if (p[i-1] == '/')
p[i]='\0';
else
{
p[i]='/';
p[i+1]='\0';
}
return p;
}
void dirc (const char* destpath){
HANDLE hFind;
WIN32_FIND_DATA FindFileData;
char filename[256];
size_t i=1;
if((hFind = FindFirstFile(destpath, &FindFileData)) != INVALID_HANDLE_VALUE)
{
do {
sprintf (filename, "echo %d-%s", i, FindFileData.cFileName);
system (filename);
i++;
}
while(FindNextFile(hFind, &FindFileData));
FindClose(hFind);
}
}
int main(int argc, char**argv) {
const char* path = argv[1];
char* fspath;
if (argv[1] == NULL)
{
std::cout <<"No path provided"<<std::endl;
return 0;
}
else
if ( DirectoryExists(path) )
std::cout <<"Provided path is "<<path<<std::endl;
else
{
std::cout <<"Path doesn't exist"<<std::endl;
return 0;
}
fspath = replace(path);
char* destpath = (char *) malloc (strlen(fspath)+6);
destpath = strcat (fspath,"*.*");
dirc (destpath);
free (destpath);
return 0;
}
C
? \$\endgroup\$ – JVApen Apr 30 '20 at 18:31