Read a byte from binary file then output it to another file

I want to read each byte from the source byte then write these bytes to the dest file under these conditions.

  1. If the read byte is 0x01, then write 0xFF and 0xFF to the destine file.
  2. 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.
  3. If the read byte is 0x09 then read the next byte and write it to the destine byte.
  4. 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.
  5. 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.
  6. 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.
  7. 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.
  8. 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.
  9. 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.

New contributor

Stoneboat is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

1

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật