read file with stream in C++

I want read file with stream, I use follow function :

std::vector<FileStruct> ReadFileStreaming(CString filePath, int startOffset)
{
std::vector<FileStruct> wholeFile;
std::ifstream stream;
stream.open(filePath);
stream.seekg(startOffset);
char buffer[STREAM_BUFFER];
if (stream.is_open())
{
    while (!stream.eof())
    {
        FileStruct fileStream;
        fileStream.startOffset = stream.tellg();
        stream >> buffer;
        fileStream.data.assign(buffer, buffer + strlen(buffer));
        fileStream.endOffset = stream.tellg();
        wholeFile.push_back(fileStream);
    }
}
  stream.close();
  return wholeFile;
}

and FileStruct is :

struct FileStream
{
 int startOffset;
 std::vector<byte> data;
 int endOffset;
};

my buffer is : STREAM_BUFFER = 1500;

this function for big file is not correct....

and when I changed buffer size, it does not change

what is wrong? can help me?

1 answer

  • answered 2018-01-14 11:22 Butuzov

    I am just aspiring C++ programmer but isn't assign can be used as fileStream.data.assign(strlen(buffer), buffer); here? Sizefor 1st argument and value for 2nd?