Benutzer-Werkzeuge

Webseiten-Werkzeuge


edv:prg:cpp:example:fileutils

Fileutils

Beispiel 1 - TextFile lesen

Als Beispiel eine Klasse, die die Fileoperationen behandelt:

fileutils.h

namespace utils
{
	class fileutils
	{
	public:
		fileutils(const fileutils&) = delete;
		fileutils& operator=(const fileutils&) = delete;
 
	public:
		static const string	ASCII;
 
	public:
		static string	read_file_content(const string& filePath);	// liefert den inhalt der datei
		static time_t	get_timestamp(const string& filePath);		// liefert den zeitstempel der datei (sekundengenau)
		static int	get_file_size(const string& filePath);		// liefert die dateigröße
		static bool	exists(const string& filePath);
		static string	get_bom_encoding(const string& content);
	}; // class fileutils
 
	inline bool	fileutils::exists(const string& filePath) { return get_file_size(filePath) >= 0; }
} // namespace utils

fileutils.cpp

namespace utils
{
	const string fileutils::ASCII = "ascii";
 
	string fileutils::read_file_content(const string& filePath)
	{
		std::ifstream ifs(filePath);
		string content((std::istreambuf_iterator<char>(ifs)), (std::istreambuf_iterator<char>()));
		return content;
	}
 
	time_t fileutils::get_timestamp(const string& filePath)
	{
		struct stat stat_buf;
		int rc = stat(filePath.c_str(), &stat_buf);
		if (rc == 0)
			return stat_buf.st_mtime;
		return 0;
	}
 
	int fileutils::get_file_size(const string& filePath)
	{
		struct stat stat_buf;
		int rc = stat(filePath.c_str(), &stat_buf);
		if (rc == 0)
			return stat_buf.st_size;
		return -1; // not found
	}
 
	string fileutils::get_bom_encoding(const string& content)
	{
		// http://de.wikipedia.org/wiki/Byte_Order_Mark
 
		const char* ENCODING_NAME[] =
		{
			"UTF-EBCDIC",
			"UTF-32 (BE)",
			"UTF-32 (LE)",
			"UTF-7",
			"UTF-7",
			"UTF-7",
			"UTF-7",
			"UTF-8",
			"UTF-1",
			"UTF-16 (BE)",
			"UTF-16 (LE)",
			nullptr
		};
		const char* ENCODING_BOM[] =
		{
			"\xDD\x73\x66\x73",
			"\x00\x00\xFE\xFF",
			"\xFF\xFE\x00\x00",
			"\x2B\x2F\x76\x38",
			"\x2B\x2F\x76\x39",
			"\x2B\x2F\x76\x2B",
			"\x2B\x2F\x76\x2F",
			"\xEF\xBB\xBF",
			"\xF7\x64\x4C",
			"\xFE\xFF",
			"\xFF\xFE",
			nullptr
		};
		const size_t ENCODING_BOM_LEN[] =
		{
			4,
			4,
			4,
			4,
			4,
			4,
			4,
			3,
			3,
			2,
			2,
			0
		};
 
		for (int i = 0; ENCODING_BOM_LEN[i] != 0; ++i)
		{
			auto len = ENCODING_BOM_LEN[i];
			if (content.length() >= len)
			{
				if (strncmp(content.c_str(), ENCODING_BOM[i], len) == 0)
				{
					return ENCODING_NAME[i];
				}
			}
		}
		return fileutils::ASCII;
	}
} // namespace utils

Beispiel 2 - TextFile schreiben

Text in ein File ausgeben:

void IrgendEineClasse::create_text_file(const char* content)
{
	string file_name = get_filename();
	std::ofstream textFile(file_name);
	textFile << content;
}

Stand: 21.01.2020
: Jürgen Kreick

EOF

edv/prg/cpp/example/fileutils.txt · Zuletzt geändert: 2020/01/21 17:26 von 127.0.0.1

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki