FileUtilities Manual

内容表

关于

maxon::FileUtilities provides several functions to easily access the content of a given file.

Utilities

These functions automatically read the content of a file or stream into the given memory. The memory (typically a maxon::BaseArray ) is automatically resized.

// This example stores the content of the given stream in a maxon::BaseArray.

// 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 ;

maxon::String content(charArray); DiagnosticOutput ( "File Content: @" , content); }

// reset stream inputStream.Seek(0) iferr_return ;

// using ReadToArray() MAXON_SCOPE { maxon::BaseArray<maxon::Char> charArray; maxon::FileUtilities::ReadToArray (inputStream, charArray) iferr_return ;

maxon::String content(charArray); DiagnosticOutput ( "File Content: @" , content); }

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:

// This example compares the two given input streams. If the content of the streams // is not identical, the error message describing the difference is printed.

// 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); }

延伸阅读

maxon::FileUtilities::ReadToArray
static Result< void > ReadToArray(UrlOrInputStream &&url, BaseArray< T > &dest)
定义: file_utilities.h:98
maxon::FileUtilities::ReadFileToMemory
static MAXON_METHOD Result< void > ReadFileToMemory(UrlOrInputStream &&name, WritableArrayInterface< Char > &arr)
maxon::String
定义: string.h:1197
iferr_return
#define iferr_return
定义: resultbase.h:1434
maxon::BaseArray
定义: basearray.h:366
maxon::HTTPMETHOD::POST
@ POST
DiagnosticOutput
#define DiagnosticOutput(formatString,...)
定义: debugdiagnostics.h:166
maxon::Url
定义: url.h:819
maxon::Result< void >
maxon::FAILED
static const ERROR_FAILED FAILED
定义: resultbase.h:68
ApplicationOutput
#define ApplicationOutput(formatString,...)
定义: debugdiagnostics.h:207
maxon::Result::GetError
const Error & GetError() const
定义: resultbase.h:1026
iferr
#define iferr(...)
定义: errorbase.h:380
maxon::FileUtilities::CompareFiles
static MAXON_METHOD Result< void > CompareFiles(UrlOrInputStream &&file1, UrlOrInputStream &&file2)
MAXON_SCOPE
#define MAXON_SCOPE
定义: apibase.h:2645
maxon::CString
定义: string.h:1436

Copyright  © 2014-2025 乐数软件    

工业和信息化部: 粤ICP备14079481号-1