Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Hi i need to separate 1 text fil

Status
Not open for further replies.

Motlatjo

Programmer
Oct 11, 2011
76
0
0
ZA
Hi

i need to separate 1 text file into 100 records in each file, so my logic create those text file, but the data is not complete in each text file, 5 records are missing.

In this code, first i load all records into a listbox, then try to separate them in 100s

private void WriteToFile()
{
int RowCount = listBox1.Items.Count;
string FileName = "C:\\Users\\bbdnet0986\\Documents\\MyExpotedQADATA";
StreamWriter sw = new StreamWriter(FileName + ".txt");
int inc = 0;
int counter = 0;
//StreamWriter sw = new StreamWriter(FileName+inc + ".txt");
for (int i = 0; i < listBox1.Items.Count; i++)
{
sw.WriteLine(listBox1.Items.ToString());
string me = listBox1.Items.ToString();

if (RowCount > 100)
{

listBox2.Items.Add(listBox1.Items[counter].ToString());
counter++;
if (counter == 100)
{

inc++;
sw = new StreamWriter(FileName + inc + ".txt");

//counter = counter - 100;
RowCount = RowCount - counter;
counter = 0;
//FileName = FileName + inc + ".txt";
}
}

else
{
sw.WriteLine(listBox1.Items.ToString());
}

}
sw.Close();
}
 
homework? okay, try to code this pseudo:
Code:
function main()

  let sourceStream = open source file
  let filecounter = 1

  while sourceStream NOT eof
    call copyLines(sourceStream, filecounter)
    increment filecounter
  loop

end function


function copyLines(sourceStream, filecounter)
  let linecounter = 1
  let file = "file" + filecounter
  open newbuffer

  while sourceStream NOT eof and linecounter not 100
    let line = read sourceStream
    write to newbuffer
    increment linecounter
  loop

end function
It's not a perfect pseudo but this should give you an idea.
[wink]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top