c++ - modify typedef declaration within if..else -


the problem trying solve:

read binary file , write contents text file. format of contents within binary file specified user using option, e.g. bin2txt.exe [filename] [/f], /f denotes contents in binary file of float type.

my current algorithm:

declare:

typedef int datatype; 

use if...else or switch...case modify datatype float, unsigned int short etc. within main code.

the problem:

datatype modified within if...else, switches default (here, int) outside if...else/switch...case. means, read binary vector write vector text file within if...else statements. way, code becomes repetitive (every if block have vector declaration, initialization, reading vector , writing text file.). better avoid such repetition.

could please guide me write direction. ? thanks.

if find writing identical code except types involved, it's candidate template.

a simple (untested) variant no error checking or input verification:

template<typename t> void convert(std::istream& in, std::ostream& out) {     t data;     while (in.get(reinterpret_cast<char*>(&data), sizeof(data)))     {         out << data << std::endl;     } }  int main(int argc, char* argv[]) {     std::ifstream input(argv[1], std::ios::binary);     std::ostream& output = std::cout;     std::string format = argv[2];     if (format == "/f")     {         convert<float>(input, output);     }     else     {         convert<int>(input, output);     } } 

Comments

Popular posts from this blog

java - Oracle EBS .ClassNotFoundException: oracle.apps.fnd.formsClient.FormsLauncher.class ERROR -

c# - how to use buttonedit in devexpress gridcontrol -

nvd3.js - angularjs-nvd3-directives setting color in legend as well as in chart elements -