Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to read from a file until a character is read then skip some characters and continue reading again

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.

like image 793
ark Avatar asked Apr 02 '12 15:04

ark


People also ask

How do I read a file until a particular character is reached?

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.

How do I read a character sequence from a text file?

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.”

How to read file character by character in C++?

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 ...

How to skip a character in a FileInputStream in Java?

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.


1 Answers

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.

like image 125
CashCow Avatar answered Nov 15 '22 09:11

CashCow