Use C ++ to read and write TDM and TDMS files

Source: Internet
Author: User

TDM and TDMS are NI-based data storage file formats, with both speed and logical organization considerations. Third-party libraries provided by NI are also easy to use for data files in this format. For more information about TDM and TDMS, go to google, Baidu, or the official website. Here, we mainly provide the simple use of third-party libraries and the efficiency Conclusions About TDM and TDMS formats obtained through our own tests.

When using third-party libraries provided by NI, the configuration environment is very simple. Just set the header files and dynamic libraries. Examples of reading and writing files are as follows ):

// Write the TDM/TDMS Format File int CTDMSTestDlg: WriteDDCFile (const char * filePath, int channelNum, int dataLen, const char * fileType/* = "TDM "*/) {DWORD begin =: GetTickCount (); DDCFileHandle testFile; DDC_CreateFile (filePath, fileType, "", & testFile ); // create the specified format of the file DDCChannelGroupHandle group01; DDC_AddChannelGroup (testFile, "group01", "", & group01 ); // Add a channel group DDCChannelHandle * channels = new DDCChannelHandle [channelNum]; double * channelData = CreateRandomData (dataLen); for (int I = 0; I <channelNum; I ++) {char channelIdx [20]; itoa (I, channelIdx, 10); DDC_AddChannel (group01, DDC_Double, channelIdx, "", ", & channels [I]); // Add a channel. Note that the value of the third parameter must be different each time you add a channel. Otherwise, the value of DDC_SetDataValues (channels [I], channelData, dataLen) will be overwritten ); // set data for the new channel} DDC_SaveFile (testFile); DWORD end =: GetTickCount (); DDC_CloseFile (testFile); delete [] channels; delete [] channelData; return end-begin ;}
// Read the TDM/TDMS Format File int CTDMSTestDlg: ReadDDCFile (const char * filePath, const char * fileType/* = "TDM" */) {DWORD begin = :: getTickCount (); DDCFileHandle ddcFile; DDC_OpenFile (filePath, fileType, & ddcFile); // open the file unsigned int groupNum = 0; DDC_GetNumChannelGroups (ddcFile, & groupNum ); // obtain the number of channel groups DDCChannelGroupHandle * groups = new DDCChannelGroupHandle [groupNum]; for (int I = 0; I <groupNum; I ++) {unsigned int channelNum = 0; DDC_GetNumChannels (groups [I], & channelNum); // get the number of channels DDCChannelHandle * channels = new DDCChannelHandle [channelNum]; for (int j = 0; j <channelNum; j ++) {unsigned _ int64 dataLen = 0; DDC_GetNumDataValues (channels [j], & dataLen); // obtain the data Length double * data = new double [dataLen]; DDC_GetDataValuesDouble (channels [j], 0, dataLen, data); // obtain data of the double type for (int k = 0; k <dataLen; k ++) {data [k] ;}} delete [] groups; groups = NULL;} DWORD end =:: GetTickCount (); DDC_CloseFile (ddcFile); delete [] groups; return end-begin ;}

It can be seen that reading and writing TDM and TDMS files is very convenient.

But what is the difference between TDM and TDMS? I have checked a lot of data. The most direct difference is that TDM has an XML header, and other data is saved in binary format. TDM indexes and data are binary files. According to the introduction on the official website, I have always thought that the TDMS format is more advocating. Later, the experiment found that this was not the case. The following conclusions are not limited to the differences between the two, but also include other conclusions. They are not necessarily correct, but can be used as a reference, ):

1. When the number of channels is the same as the data length of each channel, the files generated in the TDM and TDMS formats are about the same size;

2. When the number of channels is the same as the data length of each channel and data is written in TDM and TDMS formats, data written in TDMS format is always nearly twice as long as the time spent in TDM format, the time for reading data is almost the same as that of the two indexes. When reading data multiple times, it takes less time than the first time, however, the read time of the TDMS format after multiple accesses is always a little longer than that of the TDM format );

3. When the number of channels increases to 10000, the time spent on writing data increases significantly, but the data length increases linearly with the time spent on writing data;

4. Make sure that you have enough space to write data! The intermediate files generated by this third-party library are much larger than the final data files. For example, a data file of about 12 GB may require at least 32 GB space. However, if the data volume is small, for example, 128 channels, the Data Length is 102400, and the final data size is 100 MB, which is not very large, so the space required for intermediate files is not very large, no worries.

Download the code by yourself and use VS2008. Http://down.51cto.com/data/919272 or http://pan.baidu.com/share/link? Consumer id = 112939018 & uk = 1007352492



Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.