Witam.
Próbuję napisać program zmieniający ciąg znaków (HEX) na inny o tej samej długości. Robię to w C++ i mam trochę problem (dopiero w sumie raczkuję w temacie). Znalazłem pewną funkcję która robi to dla jednego ciągu i to działa po modyfikacji, jednak zależy mi aby tych ciągów do zmiany było wiele (nieokreślona ilość z możliwością dopisywania). Kod działający z jednym ciągiem wygląda tak:
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <iostream>
#include <fstream>
using namespace std;
size_t replace(FILE *fi, FILE *fo, uint8_t *what, uint8_t *repl, size_t size){
size_t i, index = 0, count = 0;
int ch;
while(EOF!=(ch=fgetc(fi))){
if(ch == what[index]){
if(++index == size){
for(i = 0; i < size ; ++i){
fputc(repl[i], fo);
}
index = 0;
++count;
}
} else {
for(i = 0; i < index ; ++i){
fputc(what[i], fo);
}
index =0;
fputc(ch, fo);
}
}
for(i = 0; i < index ; ++i){
fputc(what[i], fo);
}
return count;
}
int main(void){
FILE *file,*fileout;
uint8_t what[] = {0xA8, 0x78, 0x0A, 0x21, 0x6A} ; uint8_t repl[] = {0xE8, 0x78, 0x0A, 0x21, 0x6A};
size_t count;
file=fopen("fw","rb");
fileout=fopen("fw_changed","wb");
count = replace(file, fileout, what, repl, sizeof(what));
//
// delete what;
// delete repl;
//
// uint8_t what[] = {0xA8, 0x78, 0xB0, 0xFB, 0xF1}; uint8_t repl[] = {0xE8, 0x78, 0xB0, 0xFB, 0xF1};
// file=fopen("fw","rb");
// fileout=fopen("fw_changed","wb");
// count = replace(file, fileout, what, repl, sizeof(what));
fclose(fileout);
fclose(file);
return 0;
}
chciałbym to zrobić tego typu:
uint8_t what[] = {0xA8, 0x78, 0x0A, 0x21, 0x6A} ; uint8_t repl[] = {0xE8, 0x78, 0x0A, 0x21, 0x6A};
uint8_t what[] = {0xA8, 0x78, 0x0A, 0x21, 0x6A} ; uint8_t repl[] = {0xE8, 0x78, 0x0A, 0x21, 0x6A};
uint8_t what[] = {0xA8, 0x78, 0x0A, 0x21, 0x6A} ; uint8_t repl[] = {0xE8, 0x78, 0x0A, 0x21, 0x6A};
uint8_t what[] = {0xA8, 0x78, 0x0A, 0x21, 0x6A} ; uint8_t repl[] = {0xE8, 0x78, 0x0A, 0x21, 0x6A};
uint8_t what[] = {0xA8, 0x78, 0x0A, 0x21, 0x6A} ; uint8_t repl[] = {0xE8, 0x78, 0x0A, 0x21, 0x6A};
uint8_t what[] = {0xA8, 0x78, 0x0A, 0x21, 0x6A} ; uint8_t repl[] = {0xE8, 0x78, 0x0A, 0x21, 0x6A};
uint8_t what[] = {0xA8, 0x78, 0x0A, 0x21, 0x6A} ; uint8_t repl[] = {0xE8, 0x78, 0x0A, 0x21, 0x6A};
uint8_t what[] = {0xA8, 0x78, 0x0A, 0x21, 0x6A} ; uint8_t repl[] = {0xE8, 0x78, 0x0A, 0x21, 0x6A};
uint8_t what[] = {0xA8, 0x78, 0x0A, 0x21, 0x6A} ; uint8_t repl[] = {0xE8, 0x78, 0x0A, 0x21, 0x6A};
uint8_t what[] = {0xA8, 0x78, 0x0A, 0x21, 0x6A} ; uint8_t repl[] = {0xE8, 0x78, 0x0A, 0x21, 0x6A};
....
bo tych ciągów będzie do zmiany.
Próbowałem powtórzyć:
uint8_t what[] = {0xA8, 0x78, 0x0A, 0x21, 0x6A} ; uint8_t repl[] = {0xE8, 0x78, 0x0A, 0x21, 0x6A};
size_t count;
file=fopen("fw","rb");
fileout=fopen("fw_changed","wb");
count = replace(file, fileout, what, repl, sizeof(what));
ale pojawia się problem (error) redeklaracji zmiennych i nie wygląda to na optymalnie. Możecie pomóc?