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).
Why are you using CString instead of just std::string? What are you using a c-style string for input instead of just std::string? There's now also std::format which helps to create strings from other strings and numbers etc. Consider:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <string>
#include <format>
#include <iostream>

int main() {
	std::string File_Path;
	int X { 10 };

	std::cout << "Enter Path to extract sprite graphics to: ";
	std::getline(std::cin, File_Path);

	const auto combined_file_path{std::format("{}{}.png", File_Path, X)};

	std::cout << combined_file_path << '\n';
}

Why are you using CString instead of just std::string? What are you using a c-style string for input instead of just std::string?

Already asked in another topic and never answered.

https://cplusplus.com/forum/beginner/285888/#msg1243492
Thanks Guys,

Sorry I'm a slow learner and easily overwhelmed. So I'm not sure how to answer all your questions.


My project is a few years old. I don't remember why I used CString. I'm currently using Microsoft Visual Studio 2019 and don't want to upgrade and risk messing up my project as it was lots of work setting it up!

And yea I thought using <windows.h> was none standard but it works.

What version of C++ am I using?

I feel dumb lol saying it doesn't support adding of folders. I know I'm a moron. :(
Upgrading to VS 2022 won't muck up your project. FYI, you can have both 2019 and 2022 installed "in parallel."

I wouldn't hesitate to get 2022 since 2019 is no longer being as actively supported as 2022. There are newer C++ features 2019 doesn't support or only kinda somewhat supported.

IMO 2022 is a much smoother and better experience than 2019. 2019 for me after a while started getting all twitchy and having problems. I upgraded to 2022 and none of my older projects "broke". A couple required letting the IDE make some automatic modifications behind the screens, though. Builds became speedier after the change.

What version of C++ am I using?

2019 and 2022 default to ISO C++14 language standard. You can manually change the setting in the 2019 IDE to be C++17 or C++latest (C++20). In 2022 the language standard can be changed from the default to C++17, C++20 or C++latest (C++23).

When your project is open in the IDE select Project -> Properties (or ALT + Enter) - > Configuration Properties -> General -> C++ Language Standard.

When changing that property I suggest you have Configuration: All Configurations & Platform: All Platforms selected at the top of the dialog so you need change it one time for Debug/Release and Win32/x64.

And yea I thought using <windows.h> was none standard but it works.

C and C++ have changed considerably over the years, a project started "years ago" is antiquated and considered legacy code with all the new features C and C++ have added since then.

If you are using the 2019 IDE default C++ language standard then trying to use <filesystem> would not have been possible since the default language standard is C++14.

Trying to maintain and expand older legacy code, especially from a learning experience, is a massive source of frustration. Eventually that legacy code will break from some new change to Windows and you're stuck.

Now might be a good time to rethink your design choices, and look around for better possible alternatives.

I periodically look at older code I have created in the past and more often than not I cringe at the less than stellar choice(s) I made then. Then my fingers get itchy wanting to make the improvements.

Side note: it is possible to change the default so any new solution/project in 2019/2022 uses C++20 or C++23 as default without having to manually change the setting. Beyond a somewhat obscure one-time change. I did it a while back and now I don't have to think about making the change.

The main reason, if not the sole reason, to not update from 2019 to 2022 is if your Win OS is 32-bit. VS 2022 requires x64.

Even if updating to 2022 from 2019 does cause problems the time and effort to "clean up the mess" might be beneficial to what you are doing. The manual and automatic conversion might spot problems you never noticed before. And aid in refactoring the code to work more efficiently.

Even C++11 added features you should be using. Just to name a few: the C++ containers such as std::vector or std::array, smart pointers and a whole slew of pseudo random generation features.

I'm a slow learner

Been there, done that, am that. I have been self-teaching myself C++ since before 1998, I'm still trying mash a a lot of changes from each new language standard into my cranium. First with books and then surfing around the bowels of the interwebz.

Dealing with file system related tasks are still somewhat of a mystery to me from time to time.

For that matter a lot of how to mash up WinAPI code has changed over the years. Oy, the pain. The pain. A couple of rudimentary "create Windows graphical games" books from 2003/2004 I own have been a source of "what can I do to update the code to modern standards"? I've put up my efforts into a Guthub repo:

https://github.com/GeorgePimpleton/Win32-games

Along with some miscellaneous files and C++ snippets I've mashed up over the years. One is a library that for most simple uses makes using the C++ <random> library as easy as the C library pseudo random functions. Along with a few other tidbits I've found useful.

https://github.com/GeorgePimpleton/cpp_misc_files
seeplus already mentioned about the <format> library in C++20, a major change for formatting strings and output from previous language standards.

Another major change is modules. Less error prone than using header files. I've repo'd some rudimentary examples of the differences between source/header files and modules:

https://github.com/GeorgePimpleton/module_testing

C++23 augmented formatting support and module usage as you can see.

T'ain't pretty, t'ain't comprehensive about using modules and formatting, but it does give a brief look at how modules aren't as scary as the idea appears at first glance.

+-----+

I am currently doing a deep-dive into this 3-part blog post about modules:

https://vector-of-bool.github.io/2019/03/10/modules-1.html

It looks like I will have some fodder for doing some "module testing"....
Last edited on
Registered users can post here. Sign in or register to post.