N/APosted on - 02/06/2013
I wanted to read & write same flat file (. CSV) For e.g. If I have flat file structure in this way 1'PL','ABC' 2 'PL','DEF' 3'PP','BGC' In this format, I am able to read only PL type of records. If it is PL type are like 'ABC' then I am doing some processing & writing 'PL','RIC' record is in the same.
After writing the record pointer will come at the end of file, but at the time of a second read my pointer should be at 'DEF'. How can I set my pointer at 2 different levels in the same flat file for reading & write.
I wanted to read & write same flat file
You can solve the problem in two ways.
First, open the file to read and then open another temporary file to use as a writing file. Read the data from the first file and then copy it to the second file. If you need to delete or insert or modify something, you have to output those data to the second file. After that, continue reading from the first file and write to the second file until you have processed the total input file. Then, close both files and delete the first one. After that, you have to rename the second file to the original name to locate the right ownership and authority.
Another option is to read the whole file into the memory. Then updates should be made in the memory. When finished, copy the memory drop out to the file.