The only way to know the line number is by counting the carriage returns in the file.
I suggest you use the code you have written to count the carriage returns, and when it finds a CR, output the counter value to the second file before the rest of the line is output to the second file.
I hope...