Click here to Skip to main content
15,906,097 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Dear buddies:

I want to read from the end of a file that is serialize. my file has some shapes like below. I read the size of each object from the end of the file and then set the position of the cursor in the my favorite object.object 1:
a1
a2
a3
.
.
.
object 2:
b1
b2
b3
.
.
.


EOF% 10 30 EOF%


how can I read two fields from the end of file and then adjusting the position of the cursor?

Yours
Posted

Sounds like you need to read the entire file into memory, and then work from the back. From what it looks like, maybe can use File.ReadAllLines (http://msdn.microsoft.com/en-us/library/system.io.file.readalllines.aspx[^])

You can read the last line by finding the length of the array, and go from there.
 
Share this answer
 
Comments
Mmohmmad 17-May-12 12:29pm    
Thanks for your answer Mr. Nelson. I want to read all size of each object from the end and the user input the number that indicates the no. of the object and using these read size of objects I want to go directly in the right position. The size of the file is big and reading all lines is time consuming

Yours
Sergey Alexandrovich Kryukov 17-May-12 13:10pm    
Is it a text file or not?
--SA
Sergey Alexandrovich Kryukov 17-May-12 13:23pm    
My 5. As OP targets big file (you did not know about that while answering), I described the algorithm good for big files.
--SA
Mmohmmad 17-May-12 13:28pm    
Dear SAKryukov:

yes I have a big file with 1000 or more objects in which contain 10-15 field.
The problem with text files and line is that the lines have different length, so it could be considered as a "jagged array" of arrays of characters. You can never predict the position of last line if you did not scan all the file.

If the file is big, the only work-around I can see is this: at least once scan the input file from the beginning to the end using System.IO.StreamReader.ReadLine, http://msdn.microsoft.com/en-us/library/system.io.streamreader.readline.aspx[^]. Throw out the line contents, only remember position in the file and (optionally) the length of each line. Put this information in some array which will play a role of the file map. If this data is too big even for such a map, write this data in a binary file. As the file is binary, each record takes the same size, so you can use random-access read of this file. This way, reading a line from an original input file will be two-step: first, read the map record at required index. For this purpose, set Stream.Position to sizeof(metadataRecord) + index * sizeof(mapRecord), where metadataRecord could contain some fixed-size meta-data (for example, total number of lines), and mapRecord is the record about position/size of each line in the input file. On a second step, set Position in the input file from mapRecord and read from this point. I would advise to keep the input file open in binary mode. Use the class System.IO.BinaryReader and its property BaseStream needed to work with Stream.Position, http://msdn.microsoft.com/en-us/library/system.io.binaryreader.aspx[^].

—SA
 
Share this answer
 
Comments
Mmohmmad 19-May-12 15:29pm    
Dear SAKryukov:

Perfect answer.Thanks alot.
Sergey Alexandrovich Kryukov 19-May-12 21:45pm    
Mmohmmad, you are very welcome.
Good luck, call again.
--SA

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