Download

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]);

				

	}

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