HELP : writing DICOM metadata in C. - DICOM

This is a discussion on HELP : writing DICOM metadata in C. - DICOM ; Hi, i'm trying to write DICOM metadata in C language. here is my code : #include "stdio.h" main( ) { FILE *fp; unsigned long preamble = 0x00000000; char prefix[4] = "DICM"; int index; fp = fopen("DICOMTEST.dcm","w"); /* open for writing ...

+ Reply to Thread
Results 1 to 3 of 3

Thread: HELP : writing DICOM metadata in C.

  1. HELP : writing DICOM metadata in C.

    Hi, i'm trying to write DICOM metadata in C language. here is my
    code :

    #include "stdio.h"
    main( )
    {
    FILE *fp;
    unsigned long preamble = 0x00000000;
    char prefix[4] = "DICM";
    int index;
    fp = fopen("DICOMTEST.dcm","w"); /* open for writing */
    for (index = 1; index <= 128; index++)
    fprintf(fp,"%d", preamble);
    fprintf(fp,"%s", prefix);
    fclose(fp); /* close the file before ending program */
    }

    But if we see the result in hex-editor, it looks like :

    30 30 30 30 30 (128 times) ... 44 49 43 4d (DICM)

    I need it to be looks like :

    00 00 00 00 00 (128 times) ... 44 49 43 4d (DICM)

    So, what's wrong with my code ?

    Rgs,
    Budhi

  2. Re: HELP : writing DICOM metadata in C.

    The problem is that when you use fprintf like this:

    unsigned long preamble = 0x00000000;
    ....
    fprintf(fp,"%d", preamble);

    You are actually writing the character code for '0' out. You should
    use the fwrite function in this situation. Here is some documentation:
    http://www.cplusplus.com/reference/c...io/fwrite.html

  3. Re: HELP : writing DICOM metadata in C.

    On Jul 11, 3:43 am, josh.colema...@gmail.com wrote:
    > The problem is that when you use fprintf like this:
    >
    > unsigned long preamble = 0x00000000;
    > ....
    > fprintf(fp,"%d", preamble);
    >
    > You are actually writing the character code for '0' out. You should
    > use the fwrite function in this situation. Here is some documentation:http://www.cplusplus.com/reference/c...io/fwrite.html


    Thank you Josh. Problem solved.

    Rgs,
    Budhi

+ Reply to Thread