list - Inputting a text file, adding text to each line, writing to a new file C# -
for homework assignment have create simplified assembler mips code. take in input file of mips instruction output file associated binary input code. each line of code must mapped "memory" location hexadecimal value in front of line, add/assign "memory".
therefore, read in each line text file , @ front append value (starting memory address in hex + (line number * 4.) re-read file. if need read whole file in, create new file memory assigned, read file that's fine imagine unneccesary.
our professor suggested list, here's have far:
console.writeline("please enter path input file:"); string inp = console.readline(); console.writeline("please enter name of new file:"); string otp = console.readline(); streamreader inputfile = new streamreader(inp); streamwriter outputfile = new streamwriter(otp); list<string> filecontents = new list<string>(); while ((inp = inputfile.readline()) != null) filecontents.add(inp); so question is: how add string beginning of each item in list (filecontents)?
edit: followup on this: have managed of far, i've brought in whole document, mapped memory locations each line, etc. however, need further edit of lines in list "inputlines" deleting information them.
the format [0] memory address [1] label or, if no label in line registers, operations, etc. [2]-[?] registers, operations, etc. once i've mapped memory each line, line has label want put dictionary index label , memory address value contained, rid of label. - how delete information line contains it?
//go through each line, if line's first "token" label, //put in dictionary index memory address value //delete label line (int = 0; < inputlines.length; i++) { string[] token = inputlines[i].split(new char[] { ' ', ',', '(', ')', ':' }, stringsplitoptions.removeemptyentries); string possiblelabel = token[1]; if (opcodes.containskey(possiblelabel) == false) { labeltable.add(possiblelabel, token[0]); //at point want delete possiblelabel inputlines[i] , not deal anymore. } } that correctly map dictionary, not worried part.
you use stringbuilder optimization of faisal's code, otherwise perfect needs
console.writeline("please enter path input file:"); string inp = console.readline(); console.writeline("please enter name of new file:"); string otp = console.readline(); system.text.stringbuilder sb = new system.text.stringbuilder(); string inputlines = system.io.file.readalllines(inp); (int = 0; < inputlines.length; i++) sb.append("some text" + inputlines[i] + environment.newline); file.writealltext(otp, sb.tostring())
Comments
Post a Comment