A text read and write question???
-
Hi, I have a file which contains 13726 names on a text file (my school's students' names). Every line contains one name. And I have a textbox on a form which gets the name of the file and when I press the button it has to make seperate files which has 300 names in it. I mean I have to seperate these 13726 names on different files which has must contain 300 names and the last one must have 276 names in it. I made my button's function like below:
private void button1_Click(object sender, System.EventArgs e) { int a=1; int b=1; int k; StreamReader re = File.OpenText(textBox1.Text.ToString()+".txt"); string input = null; ArrayList arrText = new ArrayList(); while ((input = re.ReadLine()) != null) { arrText.Add(input); if(a%300 == 0) { FileInfo t = new FileInfo(textBox1.Text.ToString()+"_"+ b + ".txt"); StreamWriter Tex = t.CreateText(); for(k=(b-1)*300;k<(300*b)+1;k++) { if(k==0) { Tex.WriteLine(arrText[k+1]); } else { Tex.WriteLine(arrText[k]); } } Tex.Write(Tex.NewLine); Tex.Close(); } a++; } re.Close(); }
but I couldn't get a result so any help would be greatly appreciated... Thank you, Cem Louis -
Hi, I have a file which contains 13726 names on a text file (my school's students' names). Every line contains one name. And I have a textbox on a form which gets the name of the file and when I press the button it has to make seperate files which has 300 names in it. I mean I have to seperate these 13726 names on different files which has must contain 300 names and the last one must have 276 names in it. I made my button's function like below:
private void button1_Click(object sender, System.EventArgs e) { int a=1; int b=1; int k; StreamReader re = File.OpenText(textBox1.Text.ToString()+".txt"); string input = null; ArrayList arrText = new ArrayList(); while ((input = re.ReadLine()) != null) { arrText.Add(input); if(a%300 == 0) { FileInfo t = new FileInfo(textBox1.Text.ToString()+"_"+ b + ".txt"); StreamWriter Tex = t.CreateText(); for(k=(b-1)*300;k<(300*b)+1;k++) { if(k==0) { Tex.WriteLine(arrText[k+1]); } else { Tex.WriteLine(arrText[k]); } } Tex.Write(Tex.NewLine); Tex.Close(); } a++; } re.Close(); }
but I couldn't get a result so any help would be greatly appreciated... Thank you, Cem LouisOk lets start right at the beginning - there were quite a few issues with this block of code. 1.) The reason why nothing gets written to begin with is that your test 'a%300 == 0' Will only do something every 300 items. The test you want to make is 'a%300 != 0' since for every item BELOW 300 you will always get a remainder (e.g. 1%300 = 1, 2%300 = 2 etc) 2.) Having got the code to move past this first check we then have a problem with the next for loop because you may not necessarily have 300 items in your array - in fact you'll only have one the first time through. 3.) Also I am curious why you are adding 1 to K for the first every entry cemlouis wrote: if(k==0) { Tex.WriteLine(arrText**[k+1]**); } Since again the first time through you will only have one item in the array and arrays are ZERO indexed. 4.) I would recommend reading in the entire array and then writing out the contents in 300 blocks and then whatever is left for the last block. I would also suggest writing out the new files as XML since it will be easier to PARSE later and also can be used to show the information with little extra work on your part. Hope that this helps a little Markgr :)