VC reads & read binary files for large data files

xiaoxiao2021-04-10  384

Regardless of the author, I have encountered a problem, and after solving the experience will be shared with you. The problem is this, there is a batch of data files, the data format is as follows:

Date, open, maximum, minimum, closed, transaction, turnover amount

On May 13, 1996, 636.96, 636.96, 636.96, 636.96, 0, 0,

On May 14, 1996, 641.61, 641.61, 641.61, 641.61, 0, 0,

On May 15, 1996, 637.83, 637.83, 637.83, 637.83, 0, 0,

...........

The data is required to fill in four tables in order to make a corresponding analysis. The author starts to read the parts with the CFILE and CSTDIOFILE class. The CFILE class provides file operation based on binary stream, and functions are similar to the FREAD () and fwrite () functions in the C language. CSTDIOFILE provides a string-based file operation, which is functionally similar to the FGETS () and FPUTS () functions in the C language. However, the author discovered that when using these two classes, the amount of data read and written for a file must be limited to 65535 bytes. The reason is that the HUGE type pointer is required in the VC in the VC, and in the CFILE and CSTDIOFILE class, the FAR type pointer is used. Since the FAR type pointer does not have a cross-segment addressing, the length of the read and write is limited to 65535 bytes. If the size of the data buffer passing to the member function of both CFILE and CSTDIOFILE is greater than 65535 bytes, the VC generates an Assert error.

For file format features, the author uses the CARCHIVE class to read as follows:

Cfile sourcefile; // data file

CSTRING SOURCEDATA; / / Defines a temporary variable to save a record

Sourcefile.Open (.....);

CARCHIVE AR (& SourceFile, Carchive :: Load);

While (null! = ar.readstring (sourcedata)) // loop read file until the end of the file

{

IF (SourceData == "date, opening, highest, lowest, closed, transaction volume, turnover amount" || SourceData == "")

Continue; // Skip the prompt information of the file header

// Analyze and populate //

}

When analyzing, the author takes a gradual analysis and modification, the process is as follows:

Int nyear;

CString Year = SourceData.Left (SourceData.find ("Year")); // Intercept the string of the previous year

NYEAR = ATOI (YEAR); // Type conversion

SourceData = sourceData.righ (SourceData.GetLength () - SourceData.find ("Year") -2); // Remove the year and the previous character.

Repeat the above analysis until the end of the record.

Through the above method, the author successfully reads and analyzes the files.

Read binary files in VC6.0

Zhou Zhijie · Computer World Daily

You need to read a binary document with VC (especially friends who need to share binary files with DOS), and the launch of VC6.0 will undoubtedly bring a surprise to everyone. You haven't found it yet? Let me give you Wake up:

The reading and writing of binary files is usually used to use structural (body) first constructing a structure in which a record in the file is used, and then read / write one by one. In VC5.0, there is always a small draw - if you Using MFC, it requires that the structure (body) member is aligned with 8 bits, and you can't make it easy to select "Project - $ # @ 62; setting - $ # @ 62; c / c " tab "Code" CODE "Strus MEMBER Alignment" is modified to make it 1 bit (even if it changed, the change will be ignored when you compile). Of course, if your binary is created under Windows, and only in Windows Reading and writing, this effect is not obvious. But if it is very unfortunate, when you need to read a binarily created under DOS, it will make you smile, especially in your structure. The data type of each member is not at the same time (not to mention the file after saving space "bit"), the length of the structure is no longer the value you expect, the original data is chaotic at the time of reading, and you Write a record, and you can only keep you staring at the screen when you read.

In order to reduce the alignment of structural members to 1, you may have done a lot of efforts, such as transferring all the two-pieces of documents to a API-based DLL, but only requires the DLL to return to execute, or simply use VC5 .0 Write an API-based Windows program, maybe you have succeeded, but the cost of paying is also admirable.

Now, the launch of VC6.0 makes the bitter have passed. In VC6.0, the structure of the structural member can be easily reduced to 1 !! Read and write the binary of the binary, finally passed !!!

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

New Post(0)