Reputation: 1637
I have a C++ code that will print text and images. Both are working okay when they are ran separately. Now, I want to merge the printing so I can embed the image anywhere I want.
data = "TEXT [LOGO] TEXT";
Suppose, I have the data above. For data like this what I wanted to do here, is to replace the [LOGO] with the actual data for the receipt printing. Image data are stored in a file (ESC Pos Commands Plus image data).
Sample Image Data in a text file..
1b40 1b61 011b 3308 1b2a 01f0 003f 7f7f
ffff ffff ffff ffff ffff ffff ffff ffff
ffff ffff ffff ffff ffff ffff ffff ffff
ffff ffff ffff ffff ffff fefe fcfc f8f8
f0f0 e0e1 c1c3 8707 0f0f 0000 0000 0000
I reused a search and replace function.. from this link...
For simplicity, let's just refer to a simple memcpy
call...
char *temp = (char*) calloc(dataLength,dataLength * sizeof(char*));
memcpy(temp, logoBuffer, logoSize);
Now the problem is that logoBuffer contains a lot of 'NULL characters'. And during the copy the data was cut.
Is there any workaround for this? ran out of ideas...
Upvotes: 0
Views: 1015
Reputation: 53016
The fundamental problem is that you want to store binary data in a text file. You can't generally do that because binary data is not text data1, although text data is binary data. You need to make your file binary and read/write to it accordingly by using the appropriate funcion or function parameters. Other than that you seem to be having problems with c or perhaps c++ languages which are different, read this link c to understand.
You for example, do the dynamic memory allocation wrong, you are just allocating more space than you apparently want to thus not causing any visible problem. But the fact that you allocate the wrong size means that you might easily do something that would have very tragic results, to understand why I say this read about the concept of undefined behavior.
1Not every byte has a textual representation, thus not every sequence of bytes can be used or thought of as text.
Upvotes: 1