c++ - How to read a binary file given as input at terminal in c# -
i new c# , trying read binary file. in c++ :
int main(int argc, char * * argv) { file * fp; fp = fopen(argv[1], "rb"); //argv[1] because while executing @ terminal binary file read @ second postion "./filename.c binaryinutfile.bin" @ argv[0] have ./filename.c , @ argv[1] have binaryfile.bin ch = fgetc(fp); while (fread( & ch, sizeof(ch), 1, fp)) { add_symbol(ch); //this add_symbol()i use somewhere else, not important now. } fclose(fp); }
so want in writing equivalent c# code.thanks helpers. note: don't know size of file , name of file it's binary file, mean user can change binary file @ terminal should work number of binary files check output. , execute @ terminal "mono filename.exe binaryfile.bin" filename.exe file formed compiling filename.cs (by doing "gmcs filename.cs") contain code , binaryfile.cs bbianry file tested on code , "mono" have used because working on ubantu , compile using "gmcs filename.cs" give filename.exe , execute "mono filename.exe binaryfile.bin"
there lots of ways read file in c#. if want stream approach looks like:
public static void main(string[] args) { // command line arguments in args using (var stream = new binaryreader(system.io.file.openread(args[0]))) { while (stream.basestream.position < stream.basestream.length) { // sorts of functions reading here: byte processingvalue = stream.readbyte(); } } }
if processing smaller file, there option of reading entire file memory (string variable) following call:
string entirefile = system.io.file.readalltext(filenamepath);
either works - use going depend on situation. best of luck!
edit - edited example include main routine demonstrated. use example need create "console application" project , build using either visual studio or using command line (msbuild or equivalent). hope sets on right track!
Comments
Post a Comment