3

I am having a problem with reading a jpg file. I want to send the plain text value of the jpg image through a socket, so I opened the file in binary mode, thinking that that would work but it doesn't. This is my code:

system("./imagesnap image.jpg");
ifstream image("image.jpg", ios::in | ios::binary);
char imageChar[1024];
string imageString;
while (getline(image, imageString))
{
    for (int h; imageString[h] != '\0'; h++) {
        imageChar[h] = imageString[h];
    }
    send(sock, imageChar, strlen(imageChar), 0);
    for (int k = 0; imageChar[k] != '\0'; k++) {
        imageChar[k] = '\0';
    }
}

And here is my output:

????

As you can see, the file is not being opened in binary mode, or it is but its not working.

Could anyone please help?

jamespick
  • 1,974
  • 3
  • 23
  • 45
  • 4
    Sorry, but what exactly are you trying to achieve? Using `getline` on a binary file is almost certainly going to be "bad". There are no "lines" in a JPG for one thing. – Mats Petersson Aug 05 '13 at 15:18
  • As Mats pointed out, there are no lines there (well there might be some '\n' bytes). Also, by reading a binary file to a string you read a '\0' here and there which terminates your string. – Kelm Aug 05 '13 at 15:20
  • possible duplicate of [Read an image file C++ and put it on a socket](http://stackoverflow.com/questions/8390748/read-an-image-file-c-and-put-it-on-a-socket) – Mgetz Aug 05 '13 at 15:24
  • I am trying to store the text value of the jpg file in a char array to send over a socket. – jamespick Aug 05 '13 at 15:50

1 Answers1

3

Use read() instead of getline(). Be sure to use its return value.

#include <sys/types.h>
#include <sys/socket.h>
#include <iostream>
#include <fstream>

void SendFile(int sock) {
  std::ifstream image("image.jpg", std::ifstream::binary);
  char buffer[1024];
  int flags = 0;
  while(!image.eof() ) {
    image.read(buffer, sizeof(buffer));
    if (send(sock, buffer, image.gcount(), flags)) {
      ; // handle error
    }
  }
}
chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256
  • What library should I import for this? – jamespick Aug 05 '13 at 15:45
  • Thanks for the update. When I compile this I get the following error: `no matching function for call to 'fread'`. – jamespick Aug 05 '13 at 15:52
  • Oh never mind I fixed that. It now outputs the whole file, but in gibberish, with special characters and such, not in text. – jamespick Aug 05 '13 at 15:58
  • @InsertNameHere you can't fix the fact that a JPEG is a binary file, if you want it to be something that is more "text" per se, you'll need to encode the bytes in base64. – Mgetz Aug 05 '13 at 16:04
  • @Insert Name Here, concerning gibberish: That is what is in the JPG file. It is filled with all sorts of binary data that does not print well. One could test each received byte as to if it is printable ASCII, `isprint()` and use escape mechanisms for the rest, but that's a question for your receiving code and how it displays such. – chux - Reinstate Monica Aug 05 '13 at 16:04
  • is there a way for it to send text through the socket that on the other side could be put into a file with jpg file and then opened with the image in it? – jamespick Aug 05 '13 at 16:11
  • @InsertNameHere any reason you can't just send the binary data? – Mgetz Aug 05 '13 at 16:14
  • @Mgetz formed a C++ solution. – chux - Reinstate Monica Aug 05 '13 at 16:33
  • 1
    @Insert Name Here reading, sending, receiving and saving the data as binary should accomplish "put into a file with jpg file and then opened with the image in it". One could encode the binary in some text only encoding, such as base64 before sending and then decode it back to binary after receiving, but that appear unnecessary. – chux - Reinstate Monica Aug 05 '13 at 16:37