How do I read from a file till a particular character is reached and then seek to the next character and continue reading in C++.
In my program I am using some HTML syntax and generating a .htm file... So in my C++ code I have added the tags.
But when I read from my .htm file I want it to not include the tags.
What I plan on doing is reading the file till '<'
is encountered then just seek to the point till '>'
is encountered and continue reading from there.
Makes it a bit easier to search. In general, to read a file until a particular character is reached you use std::getline and you set the second parameter to your terminator so if you are reading until a '<' character you can do In your case if you are parsing HTML then there are probably specific parsers for it already.
To read a character sequence from a text file, we’ll need to perform the following steps: Create a stream object. Connect it to a file on disk. Read the file’s contents into our stream object. Close the file. The steps that we examine in detail below, register under the action of “file handling.”
Read file character by character – File Handling: 1 Step 1: Creating an object to FileReader and BufferedReader. 2 Step 2: Reading and displaying the file charcater by character. More ...
We could achieve it by using skip method of FileInputStream. skip (n) method skips n number of bytes while reading the file. We can use skip (4) for skipping initial 4 characters. Well the answer is size of char is 2 bytes in java means that character can occupy up to 2 bytes, but bytes occupied by character depends on platform default encoding.
In general, to read a file until a particular character is reached you use std::getline
and you set the second parameter to your terminator so if you are reading until a '<' character you can do
std::getline( infile, str, '<' );
you can then do the same with a >
character
In your case if you are parsing HTML then there are probably specific parsers for it already. I think HTML1.1 is XML compliant but HTML1.0 isn't as it was not always necessary to close all your tags, so an XML parser will not necessarily work.
You would need to assume that open and close tags are not part of comments or quoted text and the methodology I described above would not promise you that so you'd need a full state machine.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With