FileUtilities Manual
类 maxon::FileUtilities provides several functions to easily access the content of a given file.
These functions automatically read the content of a file or stream into the given memory. The memory (typically a maxon::BaseArray ) is automatically resized.
// get input stream const maxon::InputStreamRef inputStream = url.OpenInputStream() iferr_return ;
// using ReadFileToMemory() MAXON_SCOPE { maxon::BaseArray<maxon::Char> charArray; maxon::FileUtilities::ReadFileToMemory (inputStream, charArray) iferr_return ;
// reset stream inputStream.Seek(0) iferr_return ;
// using ReadToArray() MAXON_SCOPE { maxon::BaseArray<maxon::Char> charArray; maxon::FileUtilities::ReadToArray (inputStream, charArray) iferr_return ;
maxon::FileUtilities::ReadFileToMemory() can be used to retrieve the answer from a web server using POST parameters.
// This example retrieve the result from a POST and print it. maxon::Url theServer { "http://localhost:8080" _s };// Prepares the data that will be post maxon::String postData = "foo=bar&bin=go" _s; theServer.Set(maxon::URLFLAGS::HTTP_POSTMETHOD, maxon::HTTPMETHOD::POST ) iferr_return ; theServer.Set(maxon::URLFLAGS::HTTP_POSTDATA, maxon::CString (postData, maxon::StringEncodings::Utf8())) iferr_return ; maxon::BaseArray<maxon::Char> memReq;
// Retrieves the answer and read it to memory iferr ( maxon::FileUtilities::ReadFileToMemory (theServer, memReq)) DiagnosticOutput ( "@" , err);
// Prints the answer from server ApplicationOutput ( "answer @" , memReq);
// Prints the answer as a string maxon::String result(memReq); ApplicationOutput ( "result @" , result);
These functions handle the file's data as maxon::Utf32Char text:
另请参阅 Stream Conversions Manual .
The size and content of two files or streams is easily compared with:
// compare streams const maxon::Result<void> res = maxon::FileUtilities::CompareFiles (inputStreamA, inputStreamB);
// check if streams are not the identical if (res == maxon::FAILED ) { // print error message with description of the difference const maxon::String errorMessage = res. GetError ().GetMessage(); DiagnosticOutput ( "Files are different: @" , errorMessage); }