Thread: Reading specific data from TDMS files save it as a text File

  1. #1
    Registered User
    Join Date
    Dec 2018
    Posts
    3

    Reading specific data from TDMS files save it as a text File

    Hi everyone. it is almost end of the year!
    are you guys having a good time?

    I am trying to build a function that can read specific data that i want from TDMS files, and save the data in ASCII(.txt) file. By using C language.

    i read the technical information on the NI web. and even used a binary code reader(hexadecimal workshop, IDA-ware) to find the structure of TDMS files.

    Reading specific data from TDMS files save it as a text File-exel-jpg
    Reading specific data from TDMS files save it as a text File-data-jpg

    as you can see in the picture, i found the data through IDA Hex-viewer. In the Hex-viewer I change the 'DATA format' as Double float (64bit) instead of 1-byte integer. In that way i can found the specific data.

    The question is.... how can i print the specific data and save them as a text file?

    I tried multiple times but cannot reach the answer...

    Reading specific data from TDMS files save it as a text File-hmm-jpg
    Attached Files Attached Files

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Well assuming you know where to look in the fuzzy blobs you call screen shots, one could do.
    Code:
    // open the file in binary mode
    fp = fopen(filename,"rb");
    
    // position is some value you determine where to read from
    fseek(fp,position,SEEK_SET);
    
    union {
        double d;
        unsigned char c[8];
    } data;
    for ( int i = 0 ; i < 8 ; i++ ) {
        data.c[i] = (unsigned char)fgetc(fp);
        // If the data in the file is the reverse endian of your machine, do this instead.
        // data.c[7-i] = (unsigned char)fgetc(fp);
    }
    printf("Value=%f\n", data.d);
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Dec 2018
    Posts
    3
    Thank you Salem!

    I saw your 2016 reply including that thread your knowledge is very helpful!
    (Reading specific data from TDMS files)

    thanks to your advise I got a good idea. your advise reminds me study more and more.

    by the way. sorry for the bad quality of screenshots !

    again, Happy new year!

  4. #4
    Registered User
    Join Date
    Dec 2018
    Posts
    38
    The "bad quality of screenshots" is due to your bad choice of jpg (a "lossy" format) over png (a "lossless" format). jpg is for natural scenes. png is for images with lots of pixels of exactly the same color.

    Post the output of the following program after setting FILENAME to the name of your file. I'm assuming both the file and your machine are little endian, which is likely. If not, it will give evidence of that. Do the 10 values printed at the end match what you expect?
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <inttypes.h>
     
    #define FILENAME "test.tdms"
     
    #define kTocMetaData         (1L<<1)
    #define kTocRawData          (1L<<3)
    #define kTocDAQmxRawData     (1L<<7)
    #define kTocInterleavedData  (1L<<5)
    #define kTocBigEndian        (1L<<6)
    #define kTocNewObjList       (1L<<2)
     
    int main()
    {
        FILE *f = fopen(FILENAME, "rb");
        if (!f)
        {
            perror("Cannot open file");
            exit(EXIT_FAILURE);
        }
     
        fseek(f, 0, SEEK_END);
        printf("Filesize: %ld\n", ftell(f));
        rewind(f);
     
        char tag[5] = {0};
        fread(tag, 4, 1, f);
        if (strcmp(tag, "TDSm") != 0)
        {
            fprintf(stderr, "Not a TDMS file!\n");
            exit(EXIT_FAILURE);
        }
     
        uint32_t toc;
        fread(&toc, sizeof toc, 1, f);
        printf("ToC:\n");
        if (toc & kTocMetaData)        printf("  MetaData\n");
        if (toc & kTocRawData)         printf("  RawData\n");
        if (toc & kTocDAQmxRawData)    printf("  DAQmxRawData\n");
        if (toc & kTocInterleavedData) printf("  InterleavedData\n");
        if (toc & kTocBigEndian)       printf("  BigEndian\n");
        if (toc & kTocNewObjList)      printf("  NewObjList\n");
     
        uint32_t version;
        fread(&version, sizeof version, 1, f);
        printf("version: ");
        if      (version == 4713) printf("2.0\n");
        else if (version == 4712) printf("1.0\n");
        else                      printf("%" PRIu32 "\n", version);
     
        uint64_t segLen;
        fread(&segLen, sizeof segLen, 1, f);
        printf("segLen: %" PRIu64 "\n", segLen);
     
        uint64_t metaLen;
        fread(&metaLen, sizeof metaLen, 1, f);
        printf("metaLen: %" PRIu64 "\n", metaLen);
     
        uint32_t numObjs;
        fread(&numObjs, sizeof numObjs, 1, f);
        printf("numObjs: %" PRIu32 "\n", numObjs);
     
        fseek(f, metaLen - sizeof numObjs, SEEK_CUR);
     
        for (int i = 0; i < 10; ++i)
        {
            double d;
            fread(&d, sizeof d, 1, f);
            printf("%.8f\n", d);
        }
     
        return 0;
    }
    Last edited by bruteforce; 12-31-2018 at 11:51 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Reading specific data from TDMS files
    By Guyb in forum C Programming
    Replies: 2
    Last Post: 06-16-2016, 03:30 PM
  2. Reading from specific word in text file
    By Nurlana in forum C++ Programming
    Replies: 3
    Last Post: 12-15-2012, 09:24 PM
  3. Replies: 3
    Last Post: 05-25-2011, 05:54 PM
  4. How to save data to a file using FILES in C
    By yufa333 in forum C Programming
    Replies: 1
    Last Post: 03-15-2009, 01:26 AM
  5. Parsing specific data from one text file to another..
    By chops11 in forum C Programming
    Replies: 2
    Last Post: 09-13-2005, 10:50 AM

Tags for this Thread