# How do I load a file with binary data in ue4 in a way that I can align it with an editor

**URL:** <https://forums.unrealengine.com/t/how-do-i-load-a-file-with-binary-data-in-ue4-in-a-way-that-i-can-align-it-with-an-editor/1255253>\
**Category:** Programming & Scripting\
**Tags:** unreal-engine\
**Created:** [August 9, 2023, 3:54am UTC](https://forums.unrealengine.com/t/how-do-i-load-a-file-with-binary-data-in-ue4-in-a-way-that-i-can-align-it-with-an-editor/1255253 "2023-08-09T03:54:38Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![doug\_Scale](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@doug\_Scale](https://forums.unrealengine.com/u/doug_Scale)\
**Post date:** [August 9, 2023, 3:54am UTC](https://forums.unrealengine.com/t/how-do-i-load-a-file-with-binary-data-in-ue4-in-a-way-that-i-can-align-it-with-an-editor/1255253/1 "2023-08-09T03:54:38Z")

</div>

I haven’t found anything about working with files. It would be easy if I could just load it as a struct. I would like to build it with Visual Studio, Import it at runtime and have the values the same.

---

<div class="post-metadata">

**Author:** ![Chatouille](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/chatouille/32/1194622_2.png) [@Chatouille](https://forums.unrealengine.com/u/Chatouille)\
**Post date:** [August 9, 2023, 8:44am UTC](https://forums.unrealengine.com/t/how-do-i-load-a-file-with-binary-data-in-ue4-in-a-way-that-i-can-align-it-with-an-editor/1255253/2 "2023-08-09T08:44:06Z")

</div>

If your binary data matches the way UE serializes data, then you can write a matching struct and parse the data with UE reflection serializer. Something like this

```cpp
USTRUCT()
struct FMyData
{
    UPROPERTY() int32 a;
    UPROPERTY() FString b;
    UPROPERTY() TArray<float> c;
};

```

Here is a long version example that _could_ work :

```cpp
//FString FilePath;

UScriptStruct* StructInfo = FMyData::StaticStruct();

// load file
TArray<uint8> Bytes;
FFileHelper::LoadFileToArray(Bytes, *FilePath);
FMemoryReader Ar(Bytes);

// init memory
void* Container = FMemory::Malloc(StructInfo->GetStructureSize());
StructInfo->InitializeStruct(Container);

// parse bytes into struct
StructInfo->SerializeBin(Ar, Container);

```

Here is a shorter version that _could_ also work

```cpp
TArray<uint8> Bytes;
FFileHelper::LoadFileToArray(Bytes, *FilePath);
FMyData Data = *(FMyStruct*)(Bytes.GetData());

```

In both cases you risk running into alignment issues or data issues, due to how the binary is laid out in the file vs. how it is laid out in memory vs. how the serializer transforms it (if it does).  
So it all very much depends on _how_ the binary data was saved in the first place.

If you want to have better control over the deserialization process, write your own serializer :

```cpp
//reflection not required in this case
struct FMyData
{
    int32 a;
    FString b;
    TArray<float> c;

    bool Serialize(FArchive& Ar);
};

template<> struct TStructOpsTypeTraits<FMyData> : public TStructOpsTypeTraitsBase2<FMyData>
{
    enum { WithSerializer = true };
};

bool FMyData::Serialize(FArchive& Ar)
{
    Ar << a;
    Ar << b;
    Ar << c;
    return true;
}

```

Then you should be able to use it like first example, or even directly like this

```cpp
TArray<uint8> Bytes;
FFileHelper::LoadFileToArray(Bytes, *FilePath);
FMemoryReader Ar(Bytes);

FMyData Result;
Result.Serialize(Ar);

return Result;

```

---

<div class="post-metadata">

**Author:** ![doug\_Scale](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@doug\_Scale](https://forums.unrealengine.com/u/doug_Scale)\
**Post date:** [August 9, 2023, 6:24pm UTC](https://forums.unrealengine.com/t/how-do-i-load-a-file-with-binary-data-in-ue4-in-a-way-that-i-can-align-it-with-an-editor/1255253/3 "2023-08-09T18:24:45Z")

</div>

> [@Chatouille](#):
>
> ```auto
> TArray<uint8> Bytes;
> FFileHelper::LoadFileToArray(Bytes, *FilePath);
> 
> ```

I found that I can use FStream and it at least compiles (I do not know if it will work yet) I just forgot to use the namespace std
