Load a .csv file at runtime with c++

Hi all, as above. Is it possible to load a .csv at runtime? I want to load it into an array of transforms and use it to spawn.

I don’t know of a pre-defined function to load transforms from a CSV at runtime.
However, the CSV file format is very simple to parse “by code” especially if it just contains numbers (as transforms.)
So, read the file as a normal data file, and then write a function that walks each line, breaking it by commas, and calling strtod() on each value to turn it into floats.

Yes, I read them as a text file, line by line, using comma as separator to get an array of values from each line, and it also allows to use header info in CSV files. Later I can put here a code snippet if I’m back to my PC.

Thanks! I am bashing something together, but it won’t get past the line where I convert to a float. No error, it just skips over whatever code comes next. Strange.

I have:


std::ifstream infile("D:/PointData.csv"); 

	std::string line = "";
	while (getline(infile, line)) {
		std::stringstream strstr(line);
		std::string word = "";
		FVector temp;
		int tmp = 0;
		std::stringstream ss(line);

		while (ss.good())
		{

			std::string substr;
			getline(ss, substr, ',');

			floatTmp = strtod(substr.c_str(), NULL); //jumps from here
			

			if (tmp == 0)
			{
				 
				temp.X = floatTmp;
			}
			if (tmp == 1)
			{
				temp.Y = floatTmp;
			}
			if (tmp == 2)
			{
				temp.Z = floatTmp;
			}

			tmp += 1; 
			
		}// to here
	}

What am I messing up here? Thanks again.

I personally used no std stuff only UE4 helper functions I found on the wiki or answerhug

Got it.

Thanks for your help.


TArray<FString> take;
	FFileHelper::LoadANSITextFileToStrings(*path, NULL, take);

	FVector temp;

	for (int i = 0; i < take.Num(); i++)
	{
		FString aString = take*;

		TArray<FString> stringArray = {};

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

		temp.X = FCString::Atof(*stringArray[0]);
		temp.Y = FCString::Atof(*stringArray[1]);
		temp.Z = FCString::Atof(*stringArray[2]);

				

	}

1 Like

great, very similar to mine.
don’t you need &IFileManager::Get() instead of NULL in LoadANSITextFileToStrings?

1 Like

InFileManager–The filemanager to use - NULL will use &IFileManager::Get()

This is copied from the source!

NaĂŻve split string at line terminators and commas

	FString fPath = "C:\\test_data.csv";
	TArray<TArray<FString>> parsedCSV;

	if (FPaths::FileExists(fPath))
	{
		FString FileContent;
		//Read the csv file
		FFileHelper::LoadFileToString(FileContent, *fPath);
		//UE_LOG(LogTemp, Error, TEXT("%s"), *FileContent);
		
		const TCHAR* Terminators[] = { L"\r", L"\n" }; //LINE_TERMINATOR
		const TCHAR* CSVDelimeters[] = {TEXT(",")};

		TArray<FString> CSVLines;
		FileContent.ParseIntoArray(CSVLines, Terminators, 2);

		TArray<FString> temp_array;
		for (int i = 0; i < CSVLines.Num(); i++) {
			temp_array.Empty();
			CSVLines[i].ParseIntoArray(temp_array, CSVDelimeters, 1);
			parsedCSV.Add(temp_array);
		}

		//TArray<FString> GetCSVDataToString(FString fPath);
		//FCsvParser* csvfiles = new FCsvParser(FileContent); alternate method?
	}


	for (int i = 0; i < parsedCSV.Num(); i++) {
		for (int j = 0; j < parsedCSV[i].Num(); j++) {
			UE_LOG(LogTemp, Error, TEXT("%s"), *parsedCSV[i][j]);
		}
		UE_LOG(LogTemp, Error, TEXT("%s"), "\n");
	}