Well, it would help to know where, approximately, this "video_id" will be. This way, you could read in chunks of a certain size knowing that you will cover the whole string "video_id" entirely within one of the chunks. You also could read the entire file into memory, depending on how big it is.
Is this a video file? If so, then you will need to account for the binary nature of the file, and, of course, its size.
Say you know that the file is arranged in 1 KB packets of information. Then you can read 1 KB at a time and be assured that you won't split the string "video_id=".
Code:
#include <fstream>
#include <sstream>
#include <string>
#include <iostream>
int main()
{
std::ifstream fi("file path",std::ios::binary);
const unsigned bfsz = 1024;
char* buf = new char[bfsz];
const std::string tag("video_id=");
while(fi.read(buf,bfsz))
{
std::string s(buf);
std::size_t vipos = s.find(tag);
if(vipos == std::string::npos)
continue;
for(unsigned i = 0; i < (bfsz - vipos+tag.size()); ++i)
fi.unget();
fi.read(buf,bfsz);
std::istringstream iss(std::string(buf));
std::getline(iss, s, ';'); //this line produces an error on my compiler, but I think it's standard...
std::cout << "This is the id: " << s << std::endl;
delete [] buf;
return 0;
}
std::cout << '\"' << tag << "\" not found in file." << std::endl;
delete [] buf;
}
This is just the idea:
Code:
while(read packet)
{
look for video_id
if not found, skip the rest of loop
find out where the '=' is, and "go back" in the file to the char right after '='
read until you encounter a ';' but discard the ';'
print what you read
exit program
}
getting here means we didn't exit program, so we didn't find video_id
exit program