Ifstream is failing to load a file and it won't open

208 Views Asked by At

Some of this code may seem foreign to you since I make 3ds homebrew programs for fun but it's essentially the same but with extra lines of code you can put in. I'm trying to read a file called about.txt in a separate folder. I made it work when I put it in the same folder but i lost that file and then my partner said he wanted it in Scratch3ds-master\assets\english\text and not in Scratch3ds-master\source I keep getting the error I coded in. I'm new to stack-overflow so this might be too much code but well here's the code:

#include <fstream>
#include <string>
#include <iostream>

int main()
{
    // Initialize the services
    gfxInitDefault();
    consoleInit(GFX_TOP, NULL);

    int version_major;
    int version_minor;
    int version_patch;

    version_major = 0;
    version_minor = 0;
    version_patch = 2;




    printf("This is the placeholder for Scratch3ds\n\n");

    std::ifstream about_file;
    about_file.open("../assets/english/text/about.txt");


    if (about_file.fail())
    {
        std::cerr << "file has failed to load\n";
        exit(1);
    }
1

There are 1 best solutions below

2
PypeBros On

Chance are that you're using devkitpro packages. And chances are that the devkitpro team provide an equivalent of the NDS 'ARGV protocol' for 3DS programming. In which case, if you use

int main(int argc, char* argv[]);

you should have the full path to your executable in argv[0] if argc is non-zero.

https://devkitpro.org/wiki/Homebrew_Menu might help.

Your program has no a priori knowledge of what sort of arguments main() should receive, and in your question, you're using a main function that receives no argument at all.

Established standard for C/C++ programming is that main() will receive an array of constant C strings (typically named argv for arguments values) and the number of valid entries in that array (typically named argc for count). If you replace your original code with

#include <fstream>
#include <string>
#include <iostream>

int main(int argc, char* argv[])
{
   // Initialize the services
   // ... more code follows

then you're able to tell whether you received argument by testing argc > 0 and you'll be able to get these arguments values with argv[i].

With homebrew development, it is unlikely that you can pass arguments such as --force or --directory=/boot as on typical command-line tools, but there is one thing that is still useful: the very first entry in argv is supposed to be a full path for the running program. so you're welcome to try

std::cerr << ((argc > 0) ? argv[0] : "<no arguments>");

and see what you get.