-4

I was just thinking after reading about Java & C#, whether C++ can also read image & pdf files without the use of external libraries ? C++ doesn't have the byte type like Java & C#. Then how can we accomplish the task ( again without using an external library) ?

Can anyone give a small demonstration (ie a program or code to read or copy or write image or pdf files) ?

Anwesha
  • 638
  • 4
  • 16
  • typedef uint8_t Byte – Ben Crowhurst Nov 18 '15 at 13:10
  • Re. your edit: C++ code to "copy a file" will be all over SO/t'internet and will work just as well whether the file is an image, a PDF or random junk. Code to read _and understand_ (for any meaningful level of "understand") either an image or PDF file will not be short. That's _why_ people use external libraries. – TripeHound Nov 18 '15 at 13:33
  • can u give an answer to that @TripeHound because i have no problem in copying a file. my issue is with image n pdf files – Anwesha Nov 18 '15 at 13:40
  • Please do some minimal research of your own. – n. 'pronouns' m. Nov 18 '15 at 13:45
  • 1
    You asked for "short program or code" to read/copy/write image/PDF files. What I'm saying is that while code to just copy the bytes of a file would be short (and would work whatever the file represents), code to **read and understand** a PDF file (e.g. be able to look at the "contents" of the PDF) is **not** short. You _could_ develop this oneself (and many do so) but it's not a trivial job, and not something that would fit in an SO answer. _Some_ image file formats are simple enough that a short program could "read" them, but most are not. – TripeHound Nov 18 '15 at 13:48

3 Answers3

2

You can use unsigned char or char reinterpreted as some integer type to parse binary file formats like pdf, jpeg etc. You can create a buffer as std::vector<char> and read it as following:

std::vector<char> buffer((
            std::istreambuf_iterator<char>(infile)), // Ensure infile was opened with binary attribute
            (std::istreambuf_iterator<char>()));

Related questions: Reading and writing binary file

Community
  • 1
  • 1
Mohit Jain
  • 29,414
  • 8
  • 65
  • 93
1

It would be a strange world to have a system language like C and in this case C++ without a type byte :). Yeah, I take it, it has strange name, unsigned char, but it is still there:).

Really just think about the magnitude of re-development of all things to avoid byte:). Peripherals, many registers in CPU's and other chips, communication, data protocols. It would all have to be redone:).

ipavlu
  • 1,493
  • 12
  • 20
1

There is no difference what file you are reading opened in binary mode, there is only difference is how you should interpret the data you get from the file.

It's significantly better to take ready to use library like e.g. libjpeg or whatever. There are plenty of them. But If you really want to do this, at first you should define suitable structures and constants (see links below) to make code to be convinient and useable. Then you just read the data and try to interpret it step by step. The code below is just pseudo code, I didn't compile it.

#include <fstream>

// define header structure
struct jpeg_header 
{ 
  enum class marker: unsigned short { eoi = 0xffd8, sof0 = 0xffc0 ... };
  ...
};

bool is_eoi(unsigned short m) { return jpeg_header::eoi == m; }

jpeg_header read_jpeg_header(const std::string& fn)
{
    std::ifstream inf(fn, std::ifstream::binary);
    if (!inf) 
    {
        throw std::runtime_error("Can't open file: " + fn);
    }

    inf.exceptions(std::ifstream::failbit | std::ifstream::eofbit);

    unsigned short marker = inf.get() << 8;
    marker |= inf.get();
    if (!is_eoi(marker))
    {
        throw std::runtime_error("Invalid jpeg header");
    }
    ...

    jpeg_header header;
    // read further and fill header structure
    ...
    return header;
}

To read huge block of data use ifstream::read(), ifstream::readsome() methods. Here is the good example http://en.cppreference.com/w/cpp/io/basic_istream/read. Those functions also work faster then stream iterators. It's also better define your own exception classes derived from std::runtime_error.

For details on file formats you interested in look here

Community
  • 1
  • 1
4xy
  • 3,063
  • 2
  • 16
  • 28