c# - How can I edit numbers in my .txt file? -
i have code here , based on user input, i'd change line of choice have selected. however, can temporarily change line of text , when write out file again, text had not overwritten permanently.
here's code:
public struct classmates { public string first; public string last; public int id; } static classmates[] readclassmates(classmates[] classmateinfo) { streamreader sr = new streamreader(@"c:\class.txt"); int count = 0; while (!sr.endofstream) { classmateinfo[count].first = sr.readline(); classmateinfo[count].last = sr.readline(); string idtemp = sr.readline(); classmateinfo[count].id = convert.toint32(idtemp); count++; } sr.close(); return classmateinfo; } static void editclassmates(classmates[] classmateinfo) { console.write("who's number change? "); string classmateinput = console.readline(); (int = 0; < classmateinfo.length; i++) { if (classmateinfo[i].first.equals(classmateinput)) { console.write("enter new number: "); string temp = console.readline(); int classmatenumber = convert.toint32(temp); classmateinfo[i].id = classmatenumber; console.writeline("you have changed {0}'s number {1}.", classmateinfo[i].first,classmateinfo[i].id.tostring()); } } } static void main() { classmates[] classmateinfo = new classmates[43]; listclassmates(classmateinfo); editclassmates(classmateinfo); listclassmates(classmateinfo); }
i know meant use file.writealltext()
, don't know how utilize snippet code.
maybe you're expecting easy answer require custom code need think yourself. if you're linq , want use file.writealltext()
method can use oneliner wrote you, haven't tested:
file.writealltext(@"c:\class.txt", string.join(environment.newline, (from cm in classmateinfo select string.format("{1}{0}{2}{0}{3}", environment.newline, cm.first, cm.last, cm.id)).toarray()));
which creates string array classmateinfo, concatenates array using newline separator , write entire string specified file.
Comments
Post a Comment