Problems using std::ofstream

We are trying to use a custom CVS logger we wrote and are running into problems getting it to work on our roboRIO

This is the logger class

#pragma once

#include <array>
#include <string>
#include <variant>
#include <fstream>

template <size_t N>
class CSVLogger
{
public:

	typedef std::variant<long, double, std::string> CSVLoggerData;
	enum VarientIndex
	{
		INTEGER = 0,
		FLOATING = 1,
		STRING = 2
	};

	CSVLogger() = delete;
	CSVLogger(std::array<std::string, N> columns, std::string path);

	void logData(std::array<CSVLoggerData, N> data);
	void flush();

private:
	const size_t column_count = N;
	const std::array<std::string, N> columns;
	const std::string path;
	const size_t last = N - 1;//So we only need to calculate it once
	std::ofstream file;
};

template <size_t N>
CSVLogger<N>::CSVLogger(std::array<std::string, N> columns, std::string path) :
	columns{ columns },
	path{ path },
	file{ path }
{
	for(size_t i = 0; i < N; i++)
	{
		file << columns[i];
		if(i != last)
		{
			file << ",";
		}
	}
	file << std::endl;
}

template <size_t N>
void CSVLogger<N>::logData(std::array<CSVLoggerData, N> data)
{
	for(int i = 0; i < N; i++)
	{
		switch(data[i].index())
		{
			case INTEGER:
			{
				file << std::get<long>(data[i]);
			}break;
			case FLOATING:
			{
				file << std::get<double>(data[i]);
			}break;
			case STRING:
			{
				file << std::get<std::string>(data[i]);
			}break;
		}
		if(i != last)
		{
			file << ",";
		}
	}
	file << std::endl;
}

template <size_t N>
void CSVLogger<N>::flush()
{
	file.flush();
}

We have tried saving to both “~/log.csv” and “/log.csv” but no file is produced. We have a case of “but it works on my machine” because I can get it to work on both windows with VS2022 and an Ubuntu docker container but not on the RoboRIO. Do we need to enable some kind of setting to let us save files on the RIO?

We figured it out, you need an absolute path to the lvuser’s home. That path is “/home/lvuser” so our complete file path is “/home/lvuser/log.csv”.

For a function that gives you a path that works automatically in simulation and on the Rio, see frc::filesystem::GetOperatingDirectory().

1 Like

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.