Answer the question
In order to leave comments, you need to log in
How to read a section of a line (text file) in C++?
The task is this. There is a file from which it is necessary to read the words at the given positions [10;20], [23;40] (for example). Is it possible to somehow read them at specified intervals directly from the file using fstream, or with a string variable?
Answer the question
In order to leave comments, you need to log in
So ! You need to know the file size! (for 1 byte encoding)
int GetSizeFile(const char * filename)
{
std::ifstream file(filename, std::ios_base::binary);
int size = 0;
file.seekg(0, std::ios_base::end);
size = (int)file.tellg();
return size;
}
bool CopyFiles(const char * pathWithName, const char * outputPathAndName)
{
std::ifstream in(pathWithName, std::ios::in | std::ifstream::binary);
std::ofstream out(outputPathAndName, std::ios::out| std::ofstream::binary);
if (!in)
return false;
if (!out)
return false;
const int bufSize = 4096;
char * buffer = new char[bufSize];
while (!in.eof())
{
in.read(buffer, bufSize);
if (in.gcount())
out.write(buffer, in.gcount());
}
delete[] buffer;
return true;
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question