Create a file using C ++ Builder's memory stream

xiaoxiao2021-03-06  68

C Builder provides us with a lot of useful components, this doesn't have to say more, I wrote this article because I just finished a database for a database, some of which product data management (PDM) ), Put each Pro / E project map in a field in the database, the database is SQLSever. I would like to explain the composition of the Pro / E project, and Pro / E is a Ding Ding 3D modeling software in our machinery industry. A Pro / e project file consists of multiple suffixes of PRT and suffix asM. That is to say to deposit a Pro / e project file into the database. You must package all files after packing. Of course, remove it from the database to the hard disk folder, the table in the database. This looks like this:

When the mounting map, use the file flow to merge multiple files of the PRO / E assembled into a file package, then store the file into a field in the database. When the client is displayed, the assembly graph file in the database is quickly downloaded to a folder in a folder, and then display it in the program. Key Technologies is that the file packaging and unpacking, my approach is to package a file package for this operation, the form of this package is as follows:

MemoryHeader

FileHeader1

Document 1 content

FileHeader2

File 2 content

FileHeader3

In this package, there are two head structures to MemoryHeader and FileHeader, which they are defined as two structures in programs, and the structural forms are as follows:

Typedef struct memoryheader

{

Intness;

} MemoryHeader, * Pmemoryheader;

Typedef struct FileHeader

{

Char filename [50];

Double long isize;

FileHeader, * PfileHeader;

There is only one INT type variable ifilecount in the MemoryHeader structure, which is used to record the number of files in this file. There are two variables in the FileHeader header file, which is used to record the name of the file, and the isize is used to record the size of the file. The lengths of the two structures are 4 and 72 bytes, respectively, and the size of the files recorded in the FileHead can determine the size of each file and the location of the file in the file package.

The file package implements a class package, and the main function of this class is as follows:

Class filepackage

{

Private:

// Private member, used for temporary storage in memory in memory, in subsequent operation, store the // file stored in the memory stream on the database or borrow the package on the hard disk

TMEMORYSTREAM * MEMDATA;

PUBLIC:

// Try to return data Members MEMDATA

TMEMORYSTREAM * C_MEM ();

// Add files named STR to the file package

Bool LoadFromfile (ANSISSIING STR);

// Used to initialize the file package

BOOL init ();

/ / Clear data in the memory stream

Bool memclear ();

// Point data member MEMDATA to external memory flow MEM

Bool CopyMem (TMemoryStream * MEM);

// Store the file package to store all files to folder strsavefolder

Bool Savetofile (Ansistring StrsaveFolder);

}

The following is only specific to the function loadFromfile and SaveTofile to specifically explain how to use the memory flow.

The LoadFromFile function is implemented as follows:

Bool Filepackage :: loadFromfile (ANSISTRING STR) {

Bool bsuCcess;

Bsuccess = true;

// Declare a temporary memory stream for loading file STR and sets the memory flow reading position to 0

TMEMORYSTREAM * TEMPMEM = New TMEMORYSTREAM ();

Tempmem-> Position = 0;

// Load the file content into the temporary memory stream

Tempmem-> LoadFromFile (STR);

// Temporary string is used to obtain a file name

Ansistring straTemp;

Strtemp = ExtractFileName (STR);

// FileHeader file structure MyHeader is used to store this file name and size

FileHeader myheader;

Stropy (MyHeader.FileName, strTemp.c_str ());

MyHeader.isize = Tempmem-> size;

// Inside the header file MarkHeader is used to obtain the number in the file package

MemoryHeader Markheader;

Memdata-> Position = 0;

Memdata-> Readbuffer (& MarkHeader, Sizeof (MemoryHeader);

// Temporary header file is used to determine the location of the file in the memory package

FileHeader Tmpfheader;

For (int i = 0; i

{

Memdata-> Readbuffer (& TMPFHEADER, SIZEOF (FileHeader);

Memdata-> Position = Tmpfheader.isize;

}

// write the name and content of the file into the file package

Memdata-> WriteBuffer (& myHeader);

Tempmem-> Position = 0;

Memdata-> CopyFrom (Tempmem, Tempmem-> size);

// Add a total number of files to the file package

MarkHeader.iFileCount = 1;

Memdata-> Position = 0;

Memdata-> Writebuffer (& Markheader); MemoryHeader);

// Release temporary memory flow

Tempmem-> free ();

Return bsuccess;

}

The function of the SaveTofile is as follows:

Bool Filepackage :: SaveTofile (Ansistring StrsaveFolder)

{

Bool bflag;

Bflag = true;

// If the memory package is less than 76, there is no file.

IF (this-> Memdata-> SIZE <76)

{

ShowMessage ("There is no file in this package !!");

Return bflag;

}

// MemTemp Store a single file from the package

TMEMORYSTREAM * MEMTEMP = New TMEMORYSTREAM ();

Memoryheader theheader;

FileHeader Tmpfheader;

Memdata-> Position = 0;

Try

{

Memdata-> Readbuffer (& theheader); "the HEADER);

// Read each file in the package and save it to the hard disk folder

For (int i = 0; i

Memdata-> Readbuffer (& TMPFHEADER, SIZEOF (FileHeader);

MemTemp-> Position = 0;

Memtemp-> CopyFrom (Memdata, TMPFHEADER.ISize);

MemTemp-> Position = 0;

Memtemp-> Savetofile (StrsaveFolder "//" TMPFHEADER.FILENAME);

}

}

Catch (...)

{

Bflag = false;

}

MemTemp-> free ();

Return bflag;

}

Finally, put all the source code, and have fun.

WxfMemoryStream.h

///

Typedef struct memoryheader

{

Intness;

} MemoryHeader, * Pmemoryheader;

Typedef struct FileHeader

{

Char filename [50];

Double long isize;

FileHeader, * PfileHeader;

Class wxffilepackage

{

Private:

TMEMORYSTREAM * MEMDATA;

PUBLIC:

wxffilepackage ();

~ wxffilepackage ();

Void Showhello ();

TMEMORYSTREAM * C_MEM ();

Bool LoadFromfile (ANSISSIING STR);

BOOL init ();

Bool memclear ();

Bool CopyMem (TMemoryStream * MEM);

Bool Savetofile (Ansistring StrsaveFolder);

}

/ wxfmemorystream.cpp /// wxffilepackage :: ~ wxffilepackage () {

Memdata-> free ();

/ / -------------------------------------------------------------------------------------------- --------------------------- wxffilepackage :: wxffilepackage () {memData = new tmemorystream (); memoryheader memory; memory; Memdata-> WriteBuffer (Memheader);} // ---------------------------------------------------------------------------------------------------------------------------------------------- ---------------------------------------- Bool wxffilepackage :: CopyMem (TMemoryStream * MEM ) {Bool bflag = true; this-> memdata = mem; return bflag;} Bool wxffilepackage :: init () {bool bflag; bflag = true; true;

MemoryHeader memheader; memdata-> writebuffer (Memheader);} catch (...) {bflag = false;} return bflag;} // --------- -------------------------------------------------- ---------------- Bool wxffilepackage :: memclear () {bool bflag; bflag = true; try {this-> memdata-> clear ();} catch (...) {BFLAG = false;} return bflag;} // -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- ------------------------------------- void wxffilepackage :: Showhello () {Ansistring Strs; strs = "Hello World"; ShowMessage (strs);} // ----------------------------------------------------------------------------------------------------------------------------------------------------------------------- -------------------------------------- TmemoryStream * wxffilepackage :: c_mem () {Return Memdata; } // ----------------------------------------------------------------------------------------------------------------------------------------------------- ---------------------------- Bool wxffilepackage :: loadFromfile (ANSISTRING STR) {BOOL BSUCCESS; BSUCCESS = true; test // int K = MEMDATA-> size; // init tempmem and fill it with file tmemorystream * TempMem = new tmemorystream (); tempmem-> position = 0; t empMem-> LoadFromFile (str); AnsiString strTemp; strTemp = ExtractFileName (str); FileHeader myHeader; StrCopy (myHeader.FileName, strTemp.c_str ()); myHeader.iSize = tempMem-> Size;

MemoryHeader MarkHeader; memdata-> position = 0; Memdata-> Readbuffer (& MarkHeader);

FileHeader Tmpfheader; // locate the position where the New file shouth be insert

for (int i = 0; i ReadBuffer (& tmpFHeader, sizeof (FileHeader)); memData-> Position = tmpFHeader.iSize;} // write header and file memData-> WriteBuffer (& myHeader); tempmem-> position = 0; MEMDATA-> CopyFrom (TempMem, TempMem-> size); // Write Increasement MarkHeader.iFileCount = 1; memdata-> position = 0; memdata-> WriteBuffer (& markHeader, sizeof (MemoryHeader)); / test // memData-> Position = 0; // MemoryHeader testmem; // memData-> ReadBuffer (& testmem, sizeof (MemoryHeader)); // int i = testmem.iFileCount; // delete tempmem tempmem-> free (); return bsuccess;} // --------------------------------- ----------------------------------------- Bool wxffilepackage :: savetofile (Ansistring StrsaveFolder ) {Bool bflag; bflag = true; if (this-> memdata-> size <76) {showMessage ("This package has no file !!"); return bflag;} // Store Single Solving from the package Document TMEMORYSTREAM * MEMTEMP = New TmemoryStream (); MemoryHeader Theheader; FileHeader Tmpfheader; Memdata -> Position = 0; try {memdata-> readbuffer (& theheader); for (int i = 0; i Readbuffer (& TMPFHEADER, SIZEOF (FileHeader); Memtemp-> Position = 0; Memtemp-> CopyFrom (Memdata, TmpFheader.isize); memtemp-> position = 0; Memtemp-> Savetofile (strpfheader "//" tmpfheader.filename);}

} Catch (...) {bflag = false;

Memtemp-> free (); return bflag;}

转载请注明原文地址:https://www.9cbs.com/read-86915.html

New Post(0)