Just track the file position in a variable. File I/O functions return the number of items that has been read or written.
High level file I/O methods often provide functions that return the current position (e.g.
ftell
) than can be used too.
Special care must be taken when files are accessed by multiple processes at the same time. You should avoid to read from a file that is actually opened for writing by another process; especially when that is not only appending data but also inserting / overwriting. So you should check if your case 1 can be handled in another way.
Pseudo code for case 2:
n = 2
in = open(F1, read)
out = open(F2, create_write)
while (!end_of_file(in))
{
line = readline(in)
description = parseline(line)
if (error)
{
close(out)
n = n + 1
out = open(Fn)
}
else
write(out, description)
}
close(in)
close(out)