-5

I'm trying to read in a picture file.

#include <iostream>
#include <fstream>

using namespace std;


int main()
{

    ifstream read("C://Users/Ben/Desktop/1.jpg");

    while (1){
        cout << read.get();
        cin.get();
    }


    return 0;
}

When I do this, I get a series of numbers ranging from 0 ~ 255. So I'm assuming it's reading in the byte values correctly, except for the fact that I hit -1 (eof) prematurely. After about 30 to 40 values, the -1 appears. It's a 3MB file. I don't expect the -1 to appear until way later. What's going on?

Benn
  • 61
  • 1
  • 1
  • 2
  • You probably need to specify binary mode when you open the file (`fopen(file, "rb")` in C; I don't know how to do it with streams). – melpomene Aug 07 '16 at 00:27
  • 2
    `ifstream::traits_type::eof()` won't necessarily evaluate to `-1`, but even if it does what do you think the output will be if there are any `0xFF` bytes in the file? – user657267 Aug 07 '16 at 00:28
  • 1
    You haven't opened the file in binary mode. Try giving a second argument to the constructor of the form `ios_base::in | ios_base::binary`. – Peter Aug 07 '16 at 00:38
  • This is definitely not the way to load and read jpg formatted images. The (expected) values you see are not necessarily the pixels (if that's what you thought). As much for the `-1` you get, it's probably an `unsigned char` 255 and you 're reading it as `signed` which is `-1`. – DimChtz Aug 07 '16 at 00:39
  • 3
    This variant of `get()` returns `int`, which is guaranteed able to represent a value of `255`. – Peter Aug 07 '16 at 00:40
  • @melpomene Check http://en.cppreference.com/w/cpp/io/ios_base/openmode – πάντα ῥεῖ Aug 07 '16 at 01:13

2 Answers2

0

As @melpomene mentioned in their comment there may be a difference for the results of std::ifstream::get() regarding the file was opened using the std::ios::binary mode or not (at least for the Windows OS it seems).

There's no evidence that a value of -1 as result of std::ifstream::get() indicates that the read stream is in std::ifstream::eof() state. You may read up in the std::ifstream::get() reference documentation, for more information.

Community
  • 1
  • 1
πάντα ῥεῖ
  • 83,259
  • 13
  • 96
  • 175
-3

Try this:

#include <iostream>
#include <fstream>

using namespace std;

int main()
{

    // Add the mode ios::binary to make the file load in binary format.

    ifstream read("C://Users/Ben/Desktop/1.jpg", ios::binary);

    // Declare data variable

    int data = 0;

    // Reading loop

    while (read.read((char*)&data, 4) && read.gcount() != 0) {

        // Output data

        cout << data << endl;
    }

    // Wait for user input before closing program

    cin.get();

    return 0;
}
Zopesconk
  • 50
  • 1
  • 4
  • 12
  • Same like here: http://stackoverflow.com/a/38809885/1413395 Unfortunalely you can't see :P – πάντα ῥεῖ Aug 07 '16 at 00:54
  • 2
    http://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-wrong – melpomene Aug 07 '16 at 00:55
  • Crimson Wife: Ty! I tried that and it worked. but i made a bit of a modification. i declared an int variable 'A', and then instantiated it inside the while loop as A = read.get(); but when i do that, that means A is reading in 4 bytes at a time, since int is of size 4bytes. But then why are the values I'm getting ranging from 0~255? shouldn't they be ranging from 0~2^32? – Benn Aug 07 '16 at 02:58
  • Sorry, but image handling is not my level of expertise. – Zopesconk Aug 07 '16 at 06:31
  • I'm just guessing here, but I would assume the output numbers (0~255) are the **rgb** colors of your image @Benn. – Zopesconk Sep 06 '16 at 08:53
  • @Benn, I fixed the code so that you receive numbers ranging from (I think) 0~2^32, just like you wanted. – Zopesconk Oct 09 '16 at 05:55