Reading a file without reading the whole thing into memory
I am trying to read an extremely large text file. I want to write a program (C++) to read it line by line until I reach a certain set of characters, then begin to write the following text into a string until it reach开发者_如何学运维es another set of characters.
It is a XML file, so I'm looking at
<flag>info</flag>
I need my program to read the file until it reaches <flag>
, inputs "info" into a a string and notes that </flag>
is the point to stop putting stuff into the string. What tools could I utilize that can actually read the file. As far as detecting the <flag>
, I can do that.
Use an XML SAX parser such as Xerces; they will allow you to parse the XML file in a streaming fashion, so you don't need to load it into memory all at once. Reading line-by-line will not give you correct results on general XML files.
精彩评论