0

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);
    }
N3R4ZZuRR0
  • 2,400
  • 4
  • 18
  • 32
  • You are opening the file using a *relative* path, so the most likely culprit is the calling process's *current working directory* is not what you are expecting it to be. Always use *absolute* paths when opening files. If the location of the file is relative to the location of the EXE file, dynamically retrieve the absolute path to the EXE first, strip off the filename, ans append the relative path. – Remy Lebeau Sep 28 '19 at 17:02
  • @RemyLebeau how would i go about doing this when i wont know the exact position of the file? since it's on the 3ds (im pretty sure the drive is called 0:/ but that causes an error in the assembler) i don't know the entire path – Sand Turtle Sep 30 '19 at 22:47
  • "*how would i go about doing this when i wont know the exact position of the file?*" - like I said before, query the full path of the current process at runtime, and then modify it as needed. I'm sure the 3DS has some API you can use for that. Or, since the 3DS is a fairly closed system, the folder path is likely fixed so just hard-code it. – Remy Lebeau Oct 01 '19 at 00:43
  • Are you loading this from a SD card? Where is the file on the SD card? – user253751 Feb 15 '21 at 16:04

1 Answers1

-1

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.

PypeBros
  • 2,607
  • 24
  • 37