Create multiple folders from string

If you are a new Irrlicht Engine user, and have a newbie-question, this is the forum for you. You may also post general programming questions here.
Post Reply
LunaRebirth
Posts: 386
Joined: Sun May 11, 2014 12:13 am

Create multiple folders from string

Post by LunaRebirth »

Hello! So, Ive tried many (simple and complex) ways to create folders from a string variable.
Basically, I want to be able to type a string and if the folders exist, good! Otherwise, make new ones. I'm doing this to save a file.

So for example, I want to save file "Test.txt" in folder "Test/Ex/LM/" where none of those directories exist. So the output would be to "Test/Ex/LM/Test.txt"

I tried using getline to get slashes from the string, and then CreateDirectory based before each slash, and it didn't work out for me, as CreateDirectory is only to the specific exe location. I also tried with with CreateDirectoryEx, but nothing was being created.

Here's a little pseudo-code I need to do but can't figure out how.

Code: Select all

void createFiles(string Dir) {
    /*
    Use getline to find everything by /'s. 
    Save all of these words into an array. 
    Go array by array to see if the folders exist. 
        If not, use CreateDirectoryEx to make a new file into the last array's folder. 
        ^Having trouble figuring this function out. */
}
 
int main() {
    createFiles("Test/Ex/LM/Test.txt");
}
Is there anything I'm forgetting? I tried the above and it wouldn't output anything into the file I'm frying to save, and CreateDirectoryEx wouldn't create files. I think I was using it incorrectly.
I was doing:
CreateFileEx("","NewFile",NULL);
The first param is "" because I wanted to save it in the location of the program I'm running.

Any help is appreciated! Thanks!
LunaRebirth
Posts: 386
Joined: Sun May 11, 2014 12:13 am

Re: Create multiple folders from string

Post by LunaRebirth »

Code: Select all

void createFile(std::string file) {
    char* name = (char*)file.c_str();
    std::stringstream ss(name);
    std::string token;
    std::vector<std::string> directories;
    while (std::getline(ss,token,'/') {
        directories.push_back(token);
    }
    if (directories.size() > 0) {
        CreateDirectory((char*)directories[0].c_str(),NULL);
        std::string fullPath = directories[0]+"/";
        for  (int x = 1; x < directories.size()-1; x++) {
            if (CreateDirectoryEx((char*)fullPath.c_str(),(char*)(fullPath+directories[x]).c_str(),NULL)) { } else { break; }
            fullPath += directories[x];
            if (x < directories.size()-2) {
                fullPath += "/";
            }
        }
    }
    directories.clear();
    
    //The below is working correctly!!!
    std::ofstream myFile;
    myFile.open(header.name, std::ios::in | std::ios::out | std::ios::binary | std::ios::trunc);
    if (myFile.is_open()) {
        myFile.write(buffer,header.length);
        myFile.close();
    }
}


The above code is working correctly, but only sometimes does this crash the client. I'm not for sure exactly when, but I'm lead to believe it's because the folders already exist. And it's not all the time does it crash, just occasionally from this.

Any suggestions or reasons this may be crashing the client? Debugger shows on the above on line 3 (std::stringstream).

Thanks!!!!!!

sorry if the above has any misspellings I missed. No internet on my PC so I typed this all out on my phone. Exact code though.
LunaRebirth
Posts: 386
Joined: Sun May 11, 2014 12:13 am

Re: Create multiple folders from string

Post by LunaRebirth »

I think I found the source to the problem. Firstly, I'm sending from the server the data saying "Incoming file." The client receives it then waits for the file. The server sends the file length and version, and the client receives it. Then the server sends the file and the client receives it. All in one line of code.
However, it seems large files are receiving all at once and it's mixing up the length version and file everywhere and it's being stubborn. I tried adding Sleep(100); after file receives (since large files don't send all at once, but in pieces) but it's definitely mixing them together as the version should stay 0 and it is jumping around.

Any tips or pointers?
Thanks again!
CuteAlien
Admin
Posts: 9734
Joined: Mon Mar 06, 2006 2:25 pm
Location: Tübingen, Germany
Contact:

Re: Create multiple folders from string

Post by CuteAlien »

That sounds like the problem is still in the network file transmissions? Maybe continue that in your other thread with current code.
IRC: #irrlicht on irc.libera.chat
Code snippet repository: https://github.com/mzeilfelder/irr-playground-micha
Free racer made with Irrlicht: http://www.irrgheist.com/hcraftsource.htm
mongoose7
Posts: 1227
Joined: Wed Apr 06, 2011 12:13 pm

Re: Create multiple folders from string

Post by mongoose7 »

Do you have multiple threads sending?
LunaRebirth
Posts: 386
Joined: Sun May 11, 2014 12:13 am

Re: Create multiple folders from string

Post by LunaRebirth »

CuteAlien wrote:That sounds like the problem is still in the network file transmissions? Maybe continue that in your other thread with current code.
Sorry, okay
mongoose7 wrote:Do you have multiple threads sending?
Yes, each client adds a thread. Each server thread blocks until data received.
Client is non-blocking receives.
mongoose7
Posts: 1227
Joined: Wed Apr 06, 2011 12:13 pm

Re: Create multiple folders from string

Post by mongoose7 »

If each client has a specific thread in the server there shouldn't be a problem. However, if it is random which thread is sending to which client, there will be a problem where data becomes interleaved. This is the most likely cause for this type of problem.
LunaRebirth
Posts: 386
Joined: Sun May 11, 2014 12:13 am

Re: Create multiple folders from string

Post by LunaRebirth »

Yeah, each client has his/her own thread in the server with their own specific socket
Post Reply