Specifying a file address?

Hi, I am getting an error when trying to specify a file path for my program.
It says "failed to open file for writing". I searched around google but couldn't find a solution. I know its a silly question but I did try... I tried the tips found here...
https://stackoverflow.com/questions/8960087/how-to-convert-a-char-array-to-a-string

Problem code
1
2
  string tmp_str = "C:\\folder\\" + to_string(X) + ".png";
            encodeOneStep(tmp_str.c_str(), Image_PNG[X], width, height);  // Write the .png image file. 


Code that works when user types in the address in the console.

1
2
3
4
5
6
7
8
9
10
CString File_Path;
    char sztmp[1024];
    const char* filepath = " ";

    cin.clear();
    cout << "Enter Path to extract sprite graphics to: " << endl;
    cin.getline(sztmp, sizeof(sztmp), '\n');
    filepath = sztmp;
string combined_file_path(string(filepath) + to_string(X) + ".png");
            encodeOneStep(combined_file_path.c_str(), Image_PNG[X], width, height);  // Write the .png image file. 


I want to be able to create sub directories and folders when the program creates the images,




This is the library I'm using
https://lodev.org/lodepng/
Last edited on
Fixed. As it turns out C++ doesn't support creating of folders.

The code below does what I wanted,

1
2
#include <Windows.h>
CreateDirectory("C:\\ZZZZZZ", NULL);
C++ doesn't support creating of folders
*BUZZZZZZZZZZZZZZZZ!* Not true!

C++17 added the <filesystem> library, that includes creating directories.

https://en.cppreference.com/w/cpp/filesystem/create_directory

Plus a lot more file system related.

https://en.cppreference.com/w/cpp/filesystem

Using <windows.h> is non-standard and restricts your app to the MS ecosystem.

FYI, the SO topic you linked is over 12 years old, so is missing a lot of newer info the C++ stdlib has. Here's a quickie example snippet for using <filesystem> (need to specify C++17 or later):
1
2
3
4
5
6
7
#include<iostream>
#include <filesystem>

int main( )
{
    std::filesystem::create_directories("C:\\newfolder\\morons");
}

There's an alternate way to specify subfolders on Windows. Instead of double backslashes use single forward slashes: "C:/newfolder/morons".

The example snippet was found here:

https://stackoverflow.com/questions/8931196/how-to-create-a-directory-in-c

You might want to spend some time poking around the C++ stdlib to see what you can do, there's a lot of surprises and goodies in it.
FYI, loadpng hasn't been updated since 2021. It is available via vcpkg, there are several other PNG libs available that have been been updated more recently. pngpp, libspng and libpng.

If you were using vcpkg (it's a package manager) you can integrate any downloaded libraries, MSBuild projects can now #include any headers from the installed libraries and linking is handled automatically. No need to manually change project settings, and installing new libraries make them instantly available.

Browse the available vcpkg PNG libs here: https://vcpkg.io/en/packages (Enter png in the search box).
Registered users can post here. Sign in or register to post.