Issue with FString and loading from file

Greetings,

So I am currently trying to load a list of individuals from a .csv file.
Now it loads everything fine, displays it correctly in the debug log HOWEVER, it always crashes when I attempt to put it in an FString.


csv is formatted as such, was created and saved with Excel:

firstName, middleName, lastName, date, month, year

Code:

TArray<FString> AFileHandler::LoadIndividualsFile() {
	TArray<FString> data;
	FString temp;

	FString fileLocation = "C:\\FULLPATH-HERE-FOR-TESTING-PURPOSES-ONLY\\Data\\testIndividuals.csv";

	TArray<FString> fromFile;
	FFileHelper::LoadANSITextFileToStrings(*fileLocation, NULL, fromFile);

	for (int i = 0; i < fromFile.Num(); i++) {
		FString aString = fromFile[i];

		TArray<FString> stringArray = {};

		aString.ParseIntoArray(stringArray, TEXT(","), false);

		for (int j = 0; j < stringArray.Num(); j++) {
			UE_LOG(LogTemp, Warning, TEXT("%d == %s"), j, *stringArray[j]);
		}
		temp = stringArray[2];  // <--- this breaks it 
		//temp += stringArray[0] + " ";
		//temp += stringArray[1];
		
		//data.Add(temp);
	}
	return data;
}

I want to display the name in: Last, First Middle - hence the three lines for it
It always crashes when I attempt to put stringArray[someIndex] into a FString (or add it to data for that matter), however the log works fine.
From all the examples I have see this should be working.

Any ideas on what I am doing wrong?

Thanks!

Hey there, what is the error you are getting? You might be accessing an invalid position, first check what is stringArray.Num() to see if it’s parsing correctly.

I do, at line 17 - I iterate through stringArray and display the contents to the log - So there is a value there and I see it in the log.
The issue is getting that value into another FString

Oh, also I am not getting an error, the editor just crashes.

Hmm… okay that is odd.
This works
for (int j = 0; j < stringArray.Num(); j++) {
temp += stringArray[j];
}

This does not
temp = stringArray[2] + ", ";
//temp += stringArray[0] + " ";
//temp += stringArray[1];

Formatting issues…


Works


for (int j = 0; j < stringArray.Num(); j++) {
UE_LOG(LogTemp, Warning, TEXT("%d == %s"), j, *stringArray[j]);
}




Doesn’t

temp = stringArray[2]; // + ", ";
temp += stringArray[0] + " ";
temp += stringArray[1];

Please ignore some comments, could not get the code sections to work, or edit/remove comments for that matter

After some additional Testing:

This works

		for (int j = 0; j < stringArray.Num(); j++) {
			temp += stringArray[j];
		}

But this does not


temp = stringArray[2] + ", ";
temp += stringArray[0] + " ";
temp += stringArray[1];

temp = stringArray[2] + ", ";
temp += stringArray[0] + " ";
temp += stringArray[1];

Have you tried stringArray[index].Append ?

temp.Append(stringArray[index]);
and
stringArray[index].Append(temp);

Both cause the editor to crash.

If you print stringArray.Num() what do you get?

if(GEngine)

GEngine->AddOnScreenDebugMessage(-1, 15.0f, FColor::Red, FString::Printf(TEXT("%d"),
stringArray.Num()));

Prints: 6



for (int j = 0; j < stringArray.Num(); j++) {

UE_LOG(LogTemp, Warning, TEXT("%d == %s"), j, *stringArray[j]);

}

Prints:

0 == firstname

1 == middlename

2 == lastname

3 == day

4 == month

5 == year



UE_LOG(LogTemp, Warning, TEXT("%d"), stringArray.Num());

prints: 6

It seems to be printing fine, if after that for loop you do:

FString first = stringArray[0];

Does it crash? If it does, what is the error? Try this as last resort:

FString second = FString::Printf(TEXT("%s"), *stringArray[0]);

Neither worked. Both crashed the editor - in the error log everything looks good up to the last two entries.

[2018.01.02-03.11.31:798][484]LogOutputDevice:Warning:

Script Stack:
ManagerBP_C.RetriveListOfItems
MainWidgetBP_C.ExecuteUbergraph_MainWidgetBP
MainWidgetBP_C.BndEvt__IndividualsButton_Button_K2Node_ComponentBoundEvent_526_OnButtonClickedEvent__DelegateSignature

[2018.01.02-03.11.31:802][484]LogWindows: Windows GetLastError: The operation completed successfully. (0)

Send the entire c++ call stack.

Okay, how do I do that… quick google search (unless I used the wrong terms) does not work for me

If you execute the editor using visual studio it will stop execution before crashing and below you should have a panel called call stack like this, check that and either copy all the lines or send a screenshot.

That’s not it, you have to select the call stack option on the bottom.