-1
ifstream fp;
fp.open(path, ios::in | ios::binary); //path is the path of the DICOM file I want to read

fstream output;

output.open("C:\\Users\\Z00\\dump.txt", ios::in | ios::out | ios::trunc | ios::binary);

if (fp.is_open())
{
    while (getline(fp, rbuffer))
        output << rbuffer;

    fp.close();
}

I used the above code to read from a DICOM file to a txt file in binary mode.

Now If I open the text file using notepad or any other document viewer, it shows exactly the same contents a hex editor shows when I open the DICOM file.

Now I want to manipulate the data inside of the text document. So I tried printing the contents of the text file into the console, But it prints complete gibberish.

Why?

And how should I go about if I want to access and manipulate the binary data?

  • Binary data is not text. It can *contain* text but is not in itself text. As such you can not use text-functions to handle the data. – Some programmer dude Jul 17 '17 at 10:31
  • @Someprogrammerdude, I do know that. My main question is why am I able to read it via notepad but not via c++ program the same way? – Bored Philosopher Jul 17 '17 at 10:34
  • .txt files are specifically formatted as ASCII, binary files can be of any format. – Roy Jul 17 '17 at 10:36
  • 2
    The Windows notepad program should probably also display "gibberish", as it reads the file *as text* (which the data isn't). Also, remember that binary data can contain embedded zeros which is the string terminator, or unexpected "newlines" which aren't really newlines since binary data doesn't have lines. In short, just don't treat binary data as text like you do. For binary files you *must* know the structure of the file, otherwise it will simply make no sense. – Some programmer dude Jul 17 '17 at 10:38

1 Answers1

6

You cannot handle meaningfully the content of any binary file if you don't know well the file format used.

So in your case, you need to study the DICOM specification. See this.

Of course, you probably need to use binary read operations, like std::istream::read, std::istream::get etc... To use them wisely, you need to spend weeks in studying the DICOM file format.

Perhaps the DICOM consortium provides some free software library to read such files. Look into GDCM (or develop your own, using common parsing techniques).

Read also about endianness and serialization.

BTW, using text oriented functions like getline has no much sense with binary files (which can contain null bytes and byte containing 10 -i.e. \n in UTF-8 or ASCII- at any place).

Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547
  • My main question is why can I read the data from text document using notepad but not using a c++ program? – Bored Philosopher Jul 17 '17 at 10:33
  • 2
    You certainly can read the data using a C++ program, but you need to spend time (perhaps months) understanding the file format. – Basile Starynkevitch Jul 17 '17 at 10:33
  • So, Notepad is parsing the binary data so as to make it readable? – Bored Philosopher Jul 17 '17 at 10:37
  • 1
    I guess so, but you''l better use free software tools to be able to study their source code. I have no idea how Notepad is coded (because it is not AFAIK free software and because I never used Windows). – Basile Starynkevitch Jul 17 '17 at 10:39
  • 1
    @SumukhNitundila: No; Notepad is not parsing the DICOM file. This answer explains well the steps to parse it correctly. You can open windows executable file in notepad; but that does not mean notepad parse it correctly. Please understand the difference between PARSING and DISPLAYING CONTESTS AS IS. – Amit Joshi Jul 17 '17 at 13:46