C++ file explorer

Hello fellas, i wanna do something like an cmd file explorer in C++, the scope is to open directoryes and run files, nothing else for now, but i managed to do it but whenever i try to enter to a folder it gets me on other ones, for example: i choose folder 2F from C:\ and it gets into it, after trying to get into another folder or run a file from the "2F" folder it gets me to for example, to Documents and Settings, and i dont know what im doing wrong, please help me!

Thanks in advance!

PS: I know that the code need changes, i'll do them after i fix the main problem.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#include <iostream>
#include <dirent.h> //for finding all files from an directory
#include <stdlib.h> //for system
#include <algorithm> //for replace
#include <string>

using namespace std;

string dloc; //document location
int uicf = 0; //user input choosed file

void rso() //replace slashes operation
{
    string old("\\"), rep("\\\\");
    for (unsigned int pos=0; (pos = dloc.find(old, pos)) != std::string::npos;  pos+=rep.length())
    {
        dloc.replace(pos, old.length(), rep);
    }
}

int cdo(string fn) //c dot operation (find the dot operation)
{
    bool wdon;
    int dcv = 0;

    char searchItem = '.';
    int length = fn.size();

    for(int  i = 0;i < length;i++)
    {
        if(fn[i] == searchItem){dcv++;}
    }
    if(dcv >= 1){wdon=true;} //it has
    else{wdon=false;} //it does not

    return wdon; //true or false
} //tell if the file has or not an dot in it

void mfo(string dloc, int opt) //many functions operation
{
    DIR *dir;
    struct dirent *ent;
    int fin = 0;


    if ((dir = opendir (dloc.c_str())) != NULL)
    {
        while ((ent = readdir (dir)) != NULL)
        {
            if(opt == 0) //showing the files and folders in an directory
            {
                fin++;
                cout<<fin<<": "<<ent->d_name<<endl;
            }
            else if(opt == 1) //entering to an user inputed file or folder
            {
                fin++;
                if(fin == uicf)
                {
                    bool ssc;
                    dloc += ent->d_name;
                    if(cdo(ent->d_name) == false){ssc=true; dloc+= "\\\\";} //an folder
                    if(ssc != true){system(dloc.c_str());} //an file

                    system("cls");
                    rso(); //replace slashes
                    mfo(dloc, 0); //show files again in the new folder
                }
            }
        }
        closedir (dir);
    }
    else
    {
        cout<<"ERROR!"<<endl;
    }
}

int main()
{
    cout<<"Introduce your directory location: ";
    getline(cin, dloc); //user input the location
    system("cls"); //delete screen

    rso(); //Replace slashes
    mfo(dloc, 0); //Show the files and folder in the user inputed directory

    while(1) //infinite loop
    {
        uicf = 0; //reseting the choosed value
        cout<<endl<<"Which one to open? (number): ";
        cin >> uicf; //take the input value again
        mfo(dloc, 1); //get into file operation
    }

    return 0;
}
Last edited on
You have a mix of POSIX file system calls (opendir, readdir, closedir), but expect it to run in a Windows (or DOS) shell. That's a little weird.

Those three letter function names, ... why?
+kbw Thanks for responding to me!

You have a mix of POSIX file system calls (opendir, readdir, closedir), but expect it to run in a Windows (or DOS) shell. That's a little weird.
Hmm, how do i can fix it then? Also, its better to use WIN32_FIND_DATA FindFileData; HANDLE hFind; ?

Those three letter function names, ... why?
It's a bit easier to be to write it faster and i like it more, i dont wanna make something like void replaceslashesoperation() instead of void rso(), i know what it mean, but also i know it will be harder for others to read the code, this isnt a main problem for now.
Consider using the standard (or boost) filesystem library.
For an example, see: http://www.cplusplus.com/forum/beginner/211374/#msg990138
"You have a mix of POSIX file system calls (opendir, readdir, closedir), but expect it to run in a Windows (or DOS) shell. That's a little weird."

It is a little weird, but some of us have unix emulation on our win box, allowing for some very strange batch files that invoke unix shell commands and similar oddities in short utility programs that exploit the best of both worlds.

I have a number of these, you get used to it -- I have a send-to batch file in my windows send-to folder for bzip2 (both compress and decompress).

It's a bit easier to be to write it faster and i like it more, i dont wanna make something like void replaceslashesoperation() instead of void rso(), i know what it mean, but also i know it will be harder for others to read the code, this isnt a main problem for now.


I would caution you against this practice. Yeah, you know what it means now, but what about when you put it aside to work on other things, and then get back to it in 18 months? Will rso, cdo and mfo still be meaningful? How much time does it take to type in the couple of calls to replaceSlashes? How much time will it take to figure out what your cryptic code means in the future?

I understand--I started by learning Applesoft where variables were 2-character names and I didn't understand why anybody needed more characters. And you're working on a little utility program that may never need to be upgraded. But it is good to get in the habit of writing maintainable software--your future self will love you for it, especially as your programs get more complex.
Why are you replacing single slashes with double slashes?? I think you may be confusing the need for \\ in a string constant with what's needed in the string.

Line 61: you're assuming that dloc ends with a slash. On the top level call, that might not be true.

Line 63: ssc might be uninitialized

i dont wanna make something like void replaceslashesoperation() instead of void rso()

I understand but with function names like rso() and cdo(), you might as well use xx() and xxx().
i know it will be harder for others to read the code, this isnt a main problem for now.

Yes it is. You're asking others on this forum to read your code now.
@doug4 @dhayden
Oops, i didnt know that shortening may cause such a problem, i'll change it, also, i think i spotted my mistake in the code, i also need to remake the entire program which i will do it now.

Line 63: ssc might be uninitialized
Umm, what? It is and i get no warning/error about that

Thanks anyway for help everyone!
Topic archived. No new replies allowed.