Solarion
Honorary Master
- Joined
- Nov 14, 2012
- Messages
- 28,051
- Reaction score
- 17,804
I think I need someone with a more Einstein way of thinking here.
Problem: I have a large text file with x amount of thousands of rows. Management wants this text file split into smaller text files of 500 rows. The last file might only have 50 or 80 or even 499 rows depending on the remainder of what is left in the main file.
This is the basic block of logic I have so far.
I'll bet you can see the problem already. What when I only have say 100 rows or even 499 rows left. How is my logic going to compensate for that remainder of rows in the file?
Perhaps I'm going about this all wrong in splitting up a text file. At this point I'm dead in the water.
Problem: I have a large text file with x amount of thousands of rows. Management wants this text file split into smaller text files of 500 rows. The last file might only have 50 or 80 or even 499 rows depending on the remainder of what is left in the main file.
This is the basic block of logic I have so far.
C#:
public static void ReadFile(string file)
{
int y = 0;
var lines = File.ReadAllLines(file);
foreach (var line in lines)
{
y++;
if(y == 500)
{
y = 0; //each time this reaches 500 write to a new file and start the counter again.
}
}
}
I'll bet you can see the problem already. What when I only have say 100 rows or even 499 rows left. How is my logic going to compensate for that remainder of rows in the file?
Perhaps I'm going about this all wrong in splitting up a text file. At this point I'm dead in the water.
