I'm trying to read data from a file and write a set of x lines for y times until the end of the data file. For example:
This is my data file:
line 1
line 2
line 3
line 4
line 5
line 6
line 7
line 8
line 9
line 10
line 11
line 12
...
I want to read in line 1-9 and write this to a file 27 times, then read line 9-18 and write this to the same file for 27 times. My finished file should look something like:
line 1
line 2
line 3
line 4
line 5
line 6
line 7
line 8
line 9
...x27 times
line 10
...
line 18
...x27 times
My current code will write the first 9 lines for 27 times, but I'm not quite sure how to go about reading from 10-18 again. Please help! Any hint or suggestion is appreciated.
Loading
VulpesPosted Jan 14, 2012, 9:16 AM
C NewBeePosted Jan 16, 2012, 4:27 PM
VulpesPosted Jan 16, 2012, 2:50 PM
while(true) // infinite loop
{
// read next batch of 9 or whatever's left in file
while (counter < 9 && (line = sr.ReadLine()) != null)
{
temp[counter] = line;
counter++;
}
// write 27 times to file
for (int x = 0; x < 27; x++)
{
for (int i = 0; i < counter; i++)
{
sw.WriteLine(temp[i]);
}
if (counter < 9)
{
for (int i = counter; i < 9; i++)
{
sw.WriteLine();
}
}
}
if (counter < 9) break; // can't be any more lines left in file
Array.Clear(temp, 0, 9); // clear array for next batch of 9
counter = 0; // clear counter
}
sr.Close();
sw.Close();
}
C NewBeePosted Jan 16, 2012, 2:32 PM