Download

Import list of floats from a .txt file into a 2D array in C++ UE4

Hello everyone, i’m trying to import a big number of floats from a .txt file. These are coordinates, so is something like this:

1.4 2.5 5.6
-3.4 5.1 4.7
… … …

I want to store this into a 2D array in C++ of Unreal becouse i want to do some operations internally in C++ (not in the editor) with these. I’m trying to accomplish that in the same way that i would do in a proyect of C++ (in Code::blocks, for example) but I’m having problems opening the file. (It doesn’t open). Here’s the code i’ve implemented for this:

void AAsteroidMesh::FilePoints()
{
using namespace std;

ifstream f;

f.open("prueba.txt",ios::in);

if (f.fail()) {

    exit(1);

}

It’s implemented in a function of a C++ class that i’ve created to do a mesh with the points of the txt file.

I’ve added the prueba.txt file to the proyect folder and also from visual studio in the source folder. Can anyone tell me why isn’t working this or maybe another method to accomplish that?

It’s important to say that i have not compilation errors, when i run the game it gets closed (the file opening failed).

Maybe try using a data table instead (google data table ue4).

Thank you for the answer @MarkcusD . I’ve tried with data table via structs and blueprints too. It didn’t work becouse i had many problems when importing the .txt data into excel or google sheets to create the .csv file needed for the data table so this is not a solution for the problem. I wanna know if there is a way to read a .txt file and store the data directly into a C++ array, maybe a specific function of UE C++ or a change i must do in my code. I have seen many special functions for reading strings for dialogs or whatever… But not for numbers. If anyone can help me with this…

It’s very likely the problem is that the “working directory” is in a different spot than where the file is.
It’s also important to realize that when you package your game, you will need to specially handle this file to copy it over to the finished product.

Indeed that was the problem :sweat_smile:. Thank you.

Here I go again. I solved the problem I had for a moment, using absolute Path putting the .txt file i wanted to read in a folder out of the proyect. As you said to me @jwatte, now I’m trying to add the .txt within the proyect in order to package it. Then I have the same problen when using relative path. I have seen that the current directory of a visual studio proyect is where .vcproj project file resides. I have tried to put the .txt there but it doesn’t work. I have also tried to write a file in order to create it and see where it is but for some reason it is not created anywhere. Any help please?

Easy solution: Import it as a data table, and let the engine asset/file system support it.

Harder solution: When starting up, discover what the current working directory is, then look in that directory, and in each parent directory of that, for a folder named “Content” (or whatever your content folder name is.) Then use whatever path you discover as a prefix for the filename.
Add your text file to the “files to be included in packaging” setting in the project.
I forget what the exact name is – but there’s a way to include files/dependencies/packages that are local to your game, rather than the engine.

I recommend the easy solution. You have to learn how to use data tables once, but that will serve you well forever! And it’s a lot less hassle for built projects.

Can you go c++?

Takes like 5 seconds.
Read the text file, parse the lines.
Each line is added to an array.
Return the array.

File I/O is the core of any coding. If you can’t do this, maybe you could learn some c++ basics first. You’ll only benefit from it.

That said.
The alternative is to convert the txt to CSV.
Easy enough just using notepad++ and its standard regex.
That also takes about 2 minutes when first learning to do it…
(This would address this bit)

Csv is literally text in a specific (comma separated) format.
Create the table file as you need. Study its structure by opening it in notepad++.
Make your TXT file take on the same structure via regex.

Oh, and the easiest way to include the file in c++ is to literally copy and paste its contents into a variable.
Just follow best practices destroy the variable after converting it to an array.
Because yes, reading files that aren’t packaged becomes an issue in any coding language…

Thank you both for the responses @MostHost_LA @jwatte. Most. I will try to do it using datatable, but the last time I wasn’t able to do in that way. @jwatte for the harder way you told me (sorry I’m a little bit masoch) I don’t see any content folder (apart for the engine content folder that I think that won’t be this). Also, I don´t totally understand what do you mean in this sentence.