I have large text files that I need to embed a code (time) into, part of the way through the file. I do this by iterating through an ifstream to the point the code needs to be inserted into, and then proceed to iterate through the rest of the file, continuously copying the data into a new ofstream file.
These files are large, and almost all of this simple copy and paste operation occurs after the code insertion. This takes a while to execute. I was wondering if there was a way to optimize copying the rest of the file in bulk (rather than word-by-word iteration for the rest of the file). This is the relevant code segment:
while (!in.eof())
{
in >> value;
if ((counter > 392) && (counter < 399) && (timePosition < 6))
{
rounded = floorf(value * 1000) / 1000;
value = rounded + (time[timePosition] * .00001);
timePosition++;
}
out << value << " ";
counter++;
}
Pete Becker's answer above was just what was needed.
out << in.rdbuf();
What used to execute in a minute now takes seconds using this buffer-pointer command. The new code:
while (counter < 399)
{
in >> value;
if ((counter > 392) && (counter < 399) && (timePosition < 6))
{
rounded = floorf(value * 1000) / 1000;
value = rounded + (time[timePosition] * .00001);
timePosition++;
}
out << value << " ";
counter++;
}
out << in.rdbuf();
Thank you to all of you who commented; you were very informative, and I now know a lot more than I did when I asked this question!