Reading imported text file into string

I have a project that I am building with Unreal Engine 4 and now I am going to localize strings; I have all the data for each localization in JSON form in text files that I added to the project source code.

The file structure looks like this in my solution explorer:

Games
    MyProject (Target)
        Source
            MyProject
                Localization
                    LOC_ES.txt
                    ...
                Accessor.h
                Accessor.cpp

      

I added files to this directory

  • Right click on MyProject (second) and click add -> new filter

  • Right click on the localization filter and click add -> existing item

  • Select all txt files from another directory on my PC.

I'm not sure, in the first place, if this is the correct way to add files to a project.


I followed the instructions in this post , but I am getting this error from the engine when trying to run this code:

First opportunity exception at 0x00007FFBF1B68B9C in UE4Editor.exe: Microsoft C ++ exception: std :: length_error at memory location 0x000000C38BB2A0C0.

Here is the code as I entered it. I have tried referencing the text file to both "LOC_ES.txt" and "Localization / LOC_ES.txt" and both give me this error.

std::ifstream t(TCHAR_TO_UTF8(*("Localization/LOC_" + locale + ".txt")));
std::string str;

t.seekg(0, std::ios::end);
str.reserve(t.tellg());
t.seekg(0, std::ios::beg);

str.assign((std::istreambuf_iterator<char>(t)), std::istreambuf_iterator<char>());

      

The actual content of this file is JSON only. Here is an excerpt.

{
    "Single Player":    "Solo Jugador",
    "Multiplayer":      "Multijugador",
    "Options":      "Opciones",
    "Exit":         "Dejar",
    "Back":         "Volver",

    "Normal":       "Normal",
    "Hard":         "Duro",
    "Brutal":       "Extremo"
}

      


I've never read from a file before, so I'm not sure what the problem is.

+3


source to share


2 answers


As you wrote in the comments, the problem is with the file path. My advice for adding the file correctly in Visual Studio:

  • First, right-click LOC_ES.txt

    and select Delete. Take care not to delete the file from the file system.
  • Copy the specified file to a folder inside your project.
  • Add the file as an existing item as you did before, but now select the file inside the project folder.


Then you have to use the relative path to the file (depending on where the executable is running).

+1


source


You are probably providing the stream constructor with a relative path that does not match the location of your resource file. This error message might say, "I didn't find a stream, so I can't start searching at position 0." Look at the build / debug target where your executable is located. Then find the relative path from the executable to the resource and use that. Hope it works.



I am answering this as a C ++ programmer but dont know what unreal-engine4 is. Perhaps the resource files (Localization / LOC_ES.txt, ...) will not be copied to the resulting runtime, i.e. The scope of the executable.

0


source







All Articles