Click here to Skip to main content
15,890,399 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
In my application I am reading from the text file but i need to keep eye on the file pointer where I am reading or how much content of the file i already read. and where to start next. There are TWO situations where i need to do this.

CASE 1:: While 2 system A and B are accessing the file F at the same time where A is writing and B is reading the file F. So system A need to know how much i already read and from where to read further??

CASE 2:: While System A is reading from file F1 and after analyses writing FILE F1 line description into File F2. while reading the file F1 if some specific error code detected then I need to close the file F2 and skip the ERROR LINE open new file F3 to restart the F1 analysis from ERRORLINE+1 onward.

your suggestions to handle both cases would be appreciated.
Thanks for reading with patience
Posted

1 solution

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)
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900