Home c++ C++ FWRITE () from the string is damaged picture or text document

C++ FWRITE () from the string is damaged picture or text document

Author

Date

Category

I’m going to transmit a picture on UDP, for this you really need to turn the read data into the string. Transform. But turning back to the line in the picture I can not, the picture is broken. Although I do not write any extra data (CRC-16 and the size of each fragment converges). Here is a pseudo example where this error is played (without UDP):

file * in_image_stream = fopen ("C: \\ users \\ Codeine \\ Source \\ Repos \\ MT_2_LAB_3 \\ Debug \\ MyFile.jpg "," Rb ");
size_t bytes_read = 0;
Char BUF [250];
While (BYTES_READ = Fread (& amp; Buf, Sizeof (Char), 250, in_image_stream)) & gt; 0)
{
  File * Out_image_stream = Fopen ("C: \\ Users \\ Codeine \\ Source \\ Repos \\ MT_2_LAB_3 \\ Debug \\ Result.jpg", "AB +");
  STRING STRING_BUF (BUF);
  FWRITE (STRING_BUF.C_STR (), 250, SIZEOF (CHAR), OUT_IMAGE_STREAM); // 10 String
  FClose (out_image_stream);
}
FClose (in_image_stream);

If you change 10 string on:

fwrite (& amp; buf, 250, sizeof (char), out_image_stream);

That’s all good, but it will not work with the client, it will receive data in the form of Const Char [].
Roughly speaking, I need a BUF [250] to turn into a string, and then write it to the file. And in this case, the image is not damaged.

The text file turns out about such a problem:
The source looks like this:

ABBG
a B C D
a B C D
a B C D
a B C D
a B C D
a B C D
a B C D
a B C D
1544.
9999.

and when reading- & gt; transformation into a string- & gt; transformation into Const Char [] – & gt; records in it:

ABBG
a B C D
a B C D
a B C D
a B C D
a B C D
a B C D
a B C D
a B C D
1544.
9999mmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmm

Answer 1, Authority 100%

You read the binary file in the buffer :

bytes_read = fread (& amp; buf, sizeof (char), 250, in_image_stream)

Among the read bytes may well be a zero byte, which when converting the buffer in Std :: String :

string string_buf (buf);

will be interpreted as the end of the string. On the other hand, in buf may not be zero bytes at all, so when designing a string, it will occur beyond the array buf : STRING Designer will read memory until as long as it does not appear on the zero symbol.

Transmit the buffer size buf in the designer String Obviously:

string string_buf (buf, bytes_read);

and the tenth line it is better to rewrite:

fwrite (string_buf.c_str (), sizeof (char), string_buf.size (), out_image_stream); // 10 String

and even when reading

bytes_read = fread (& amp; buf, sizeof (char), 250, in_image_stream)

You pass the address of the entire array, and you need to transmit the address of the first element of the array

bytes_read = fread (& amp; buf [0], sizeof (char), 250, in_image_stream) // so
Bytes_read = Fread (BUF, SIZEOF (CHAR), 250, IN_IMAGE_STREAM) // or so

Answer 2

The first thing I rushed into my eyes is that you are trying to write down all 250 bytes from the buffer, while the buffer itself you do not reset, which means that there is garbage.

Next to correct your situation, write to the file / socket, only that length that was read from the file, i.e. Your variable bytes_read .

file * in_image_stream =
   Fopen ("C: \\ Users \\ Codeine \\ Source \\ Repos \\ MT_2_LAB_3 \\ Debug \\ MyFile.jpg", "Rb");
size_t bytes_read = 0;
CHAR BUF [250] = {0};
While (BUF, SIZEOF (CHAR), 250, IN_IMAGE_STREAM)) & gt; 0)
{
   File * out_image_stream
     = Fopen ("C: \\ Users \\ Codeine \\ Source \\ Repos \\ MT_2_LAB_3 \\ Debug \\ Result.jpg", "AB +");
   // Do what you want, but do not write down the data into the file from here !!!
   STD :: STRING STRING_BUF (BUF);
   // write as much as read, no more!
   FWRITE (BUF, BYTES_READ, SIZEOF (CHAR), OUT_IMAGE_STREAM);
   FClose (out_image_stream);
}
FClose (in_image_stream);

offtop:
as well as it is not clear why you open and close the file every time in the cycle and close the file.

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions