I want to read each byte from the source byte then write these bytes to the dest file under these conditions.
- If the read byte is 0x01, then write 0xFF and 0xFF to the destine file.
- If the read byte is not 0x09, or not 0x0A, or not 0x0B, or not 0x0C, or not 0x0D, or not 0x0E then write that read byte to the destine file.
- If the read byte is 0x09 then read the next byte and write it to the destine byte.
- If the read byte is 0x0A then read the next byte, write 0x0A before each read byte until the string reaches 0x09, or 0x0B, or 0x0C, or 0x0D, or 0x0E.
- If the read byte is 0x0B then read the next byte, write 0x0B before each read byte until the string reaches 0x09, or 0x0A, or 0x0C, or 0x0D, or 0x0E.
- If the read byte is 0x0C then read the next byte, write 0x0C before each read byte until the string reaches 0x09, or 0x0A, or 0x0B, or 0x0D, or 0x0E.
- If the read byte is 0x0D then read the next byte, write 0x0D before each read byte until the string reaches 0x09, or 0x0A, or 0x0B, or 0x0C, or 0x0E.
- If the read byte is 0x0E then read the next byte, write 0x0E before each read byte until the string reaches 0x09, or 0x0A, or 0x0B, or 0x0C, or 0x0D.
- If the read byte is not 0x09, or not 0x0A, or not 0x0B, or not 0x0C, or not 0x0D, or not 0x0E, or not 0x01 then write that byte to the destine file.
For example the dest file content is
00-FF-23-09-26-2A-3F-0A-14-34-8F-23-09-11-0B-23-CD-22-0C-01-02-0D-14-F8-0E-19-09-01
then the dest file must be
00-FF-23-26-2A-3F-0A-14-0A-34-0A-8F-0A-23-11-0B-23-0B-CD-0B-22-0C-01-0C-02-0D-14-0D-F8-0E-19-FF-FF
Then my program is as below, every thing works well except that in trigger byte 0x0B doesn’t work.
Can anyone explain why?
Thank you for reading.
#include <iostream>
#include <fstream>
void processFile(const std::string &inputFile, const std::string &outputFile) {
std::ifstream inFile(inputFile, std::ios::binary);
std::ofstream outFile(outputFile, std::ios::binary);
if (!inFile) {
std::cerr << "Error opening input file!" << std::endl;
return;
}
if (!outFile) {
std::cerr << "Error opening output file!" << std::endl;
return;
}
char byte;
bool triggerA = false;
bool triggerB = false;
bool triggerC = false;
bool triggerD = false;
bool triggerE = false;
while (inFile.get(byte)) {
if (byte == 0x09) {
if (inFile.get(byte)) { // Read the next byte
outFile.put(byte); // Write the next byte to output file
}
}
else if (byte == 0x01){
if (inFile.get(byte)) {
outFile.put(0xFF);
outFile.put(0xFF);
}
}
else if (byte == 0x0A) {
triggerA = true; // Start triggering for 0x0A
if (inFile.get(byte)) { // Read the next byte
outFile.put(0x0A); // Write 0x0A before the next byte
outFile.put(byte); // Write the next byte
while (byte != 0x09 && byte != 0x0B && byte != 0x0C && byte != 0x0D && byte != 0x0E) {
if (!inFile.get(byte)) break;
outFile.put(0x0A); // Write 0x0A before each byte until reaching 0x09, 0x0B, 0x0C, 0x0D, or 0x0E
outFile.put(byte); // Write the byte
}
continue; // Skip the following outFile.put(byte) as it's already written
}
}
else if (byte == 0x0B) {
triggerB = true;
if (inFile.get(byte)) {
outFile.put(0x0B);
outFile.put(byte);
while (byte != 0x09 && byte != 0x0A && byte != 0x0C && byte != 0x0D && byte != 0x0E) {
if (!inFile.get(byte)) break;
outFile.put(0x0B);
outFile.put(byte);
}
continue; // Skip the following outFile.put(byte) as it's already written
}
}
else if (byte == 0x0C) {
triggerC = true;
if (inFile.get(byte)) {
outFile.put(0x0C);
outFile.put(byte);
while (byte != 0x09 && byte != 0x0A && byte != 0x0B && byte != 0x0D && byte != 0x0E) {
if (!inFile.get(byte)) break;
outFile.put(0x0C);
outFile.put(byte);
}
continue; // Skip the following outFile.put(byte) as it's already written
}
}
else if (byte == 0x0D) {
triggerD = true;
if (inFile.get(byte)) {
outFile.put(0x0D);
outFile.put(byte);
while (byte != 0x09 && byte != 0x0A && byte != 0x0B && byte != 0x0C && byte != 0x0E) {
if (!inFile.get(byte)) break;
outFile.put(0x0D);
outFile.put(byte);
}
continue; // Skip the following outFile.put(byte) as it's already written
}
}
else if (byte == 0x0E) {
triggerE = true;
if (inFile.get(byte)) {
outFile.put(0x0E);
outFile.put(byte);
while (byte != 0x09 && byte != 0x0A && byte != 0x0B && byte != 0x0C && byte != 0x0D) {
if (!inFile.get(byte)) break;
outFile.put(0x0E);
outFile.put(byte);
}
continue; // Skip the following outFile.put(byte) as it's already written
}
}
else {
outFile.put(byte); // Write the read byte to output file
}
}
inFile.close();
outFile.close();
}
int main() {
processFile("dummy.bin", "kage.bin");
return 0;
}`
Please explain how to fix this. Thank you.
Stoneboat is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
1